[英]Plotly Dash - Adding Horizontal Line on Bar Plot
我正在嘗試在我的條形圖上創建一條水平線,我可以使用散點圖作為另一個跟蹤來做到這一點。
但是,由於紅線沒有完全延伸,我試圖在 go.Layout 中使用 Shapes 參數,但它給了我這個錯誤:
為布局的“shapes”屬性接收到的“builtins.dict”類型的無效值接收到的值:{'type':'line','x0':0,'y0':2,'x1':4,'y1 ': 2, 'line': {'color': 'red', 'width': 4, 'dash': 'dashdot'}} 'shapes' 屬性是 Shape 實例的元組,可以指定為: - plotly.graph_objs.layout.Shape 實例的列表或元組 - 將傳遞給 Shape 構造函數的字符串/值屬性的列表或元組
這是代碼
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}
任何幫助深表感謝!
這是 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,
),
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.