在条形图上添加水平线

2024-05-16 03:43:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在条形图上创建一条水平线,我可以这样做,使用散点图作为另一个轨迹。enter image description here

但是,由于红线没有完全延伸,我尝试在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}

非常感谢您的帮助


Tags: ofgodfvaluetypelinewidthmarker
1条回答
网友
1楼 · 发布于 2024-05-16 03:43:47

这在plotly分类数据中是一个问题。您需要对水平线使用绝对/纸张定位,例如

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,
    ),
             )

相关问题 更多 >