Plotly Dash - 在条形图上添加水平线
Plotly Dash - Adding Horizontal Line on Bar Plot
我正在尝试在我的条形图上创建一条水平线,我可以使用散点图作为另一条轨迹来做到这一点。
但是,由于红线没有完全延伸,我试图在 go.Layout 中使用 Shapes 参数,但它给我这个错误:
Invalid value of type 'builtins.dict' received for the 'shapes' property of layout
Received value: {'type': 'line', 'x0': 0, 'y0': 2, 'x1': 4, 'y1': 2, 'line': {'color': 'red', 'width': 4, 'dash': 'dashdot'}}
The 'shapes' property is a tuple of instances of
Shape that may be specified as:
- A list or tuple of instances of plotly.graph_objs.layout.Shape
- A list or tuple of dicts of string/value properties that
will be passed to the Shape constructor
这是代码
bar_chart = [go.Bar(x=competitor_df.index,
y=competitor_df['competitor_cost'],
marker_color=colors,
marker_line_width=1.5,
marker_line_color='#505461',
textposition='outside',
texttemplate="$%{y}")]
layout = go.Layout(yaxis=dict(range=[0, competitor_df.max]),
xaxis=dict(range=[0, 4]),
shapes=dict(
type='line',
x0=0,
y0=2,
x1=4,
y1=2,
line=dict(color='red', width=4, dash='dashdot')))
fig = {'data':bar_chart, 'layout':layout}
非常感谢任何帮助!
这是分类数据的一个问题。您将需要对水平线使用 absolute/paper 定位,例如
fig.add_shape(type="line",
xref="paper", yref="paper",
x0=0, y0=0.8,
x1=1, y1=0.8,
line=dict(
color="magenta",
width=3,
),
)
我正在尝试在我的条形图上创建一条水平线,我可以使用散点图作为另一条轨迹来做到这一点。
但是,由于红线没有完全延伸,我试图在 go.Layout 中使用 Shapes 参数,但它给我这个错误:
Invalid value of type 'builtins.dict' received for the 'shapes' property of layout Received value: {'type': 'line', 'x0': 0, 'y0': 2, 'x1': 4, 'y1': 2, 'line': {'color': 'red', 'width': 4, 'dash': 'dashdot'}} The 'shapes' property is a tuple of instances of Shape that may be specified as: - A list or tuple of instances of plotly.graph_objs.layout.Shape - A list or tuple of dicts of string/value properties that will be passed to the Shape constructor
这是代码
bar_chart = [go.Bar(x=competitor_df.index,
y=competitor_df['competitor_cost'],
marker_color=colors,
marker_line_width=1.5,
marker_line_color='#505461',
textposition='outside',
texttemplate="$%{y}")]
layout = go.Layout(yaxis=dict(range=[0, competitor_df.max]),
xaxis=dict(range=[0, 4]),
shapes=dict(
type='line',
x0=0,
y0=2,
x1=4,
y1=2,
line=dict(color='red', width=4, dash='dashdot')))
fig = {'data':bar_chart, 'layout':layout}
非常感谢任何帮助!
这是分类数据的一个问题。您将需要对水平线使用 absolute/paper 定位,例如
fig.add_shape(type="line",
xref="paper", yref="paper",
x0=0, y0=0.8,
x1=1, y1=0.8,
line=dict(
color="magenta",
width=3,
),
)