1 回答

TA貢獻1946條經驗 獲得超3個贊
垂直距離可以很容易地通過以下方式獲得:
diffs = [max(values) - v for v in values]
唯一真正的挑戰(zhàn)是讓每個參數(shù)都fig.add_annotations()
正確。yanchor
和ax
的正確組合ayref
將為您提供此圖:
您可以在下面的代碼片段中仔細查看詳細信息。如果您不喜歡數(shù)字的位置,我們也可以解決。
完整代碼:
import plotly.graph_objects as go
labels = ['a','b','c']
values = [30,20,10]
diffs = [max(values) - v for v in values]
diff_labels = dict(zip(labels, diffs))
#print(diff_labels)
fig = go.Figure( data= go.Bar(x=labels,y=values,width = [0.5,0.5,0.5] ) )
for k, v in diff_labels.items():
if v > 0:
print(v)
fig.add_annotation(x=k, y=max(values)-v, ax=0,
yanchor='bottom',
ay=max(values), ayref='y',
showarrow=True, arrowsize=2,
arrowhead=1,
text = v
)
fig.add_traces(go.Scatter(x=labels, y=[max(values)]*3, mode = 'lines',
line=dict(color='black', width=1)))
fig.show()
添加回答
舉報