【发布时间】:2021-09-20 22:09:49
【问题描述】:
我正在使用 plotly (python) 绘制如下图所示的水平条形图。
import plotly.express as px
fig = px.bar(
x=prices,
y=['Average $' + str(round(avg_price, 2)), 'Lowest $' +
str(round(min_price, 2)), 'Last $' + str(round(last_price, 2)),
'Proposed $' + str(round(proposed_price, 2))],
color=['Last', 'Average', 'Lowest', 'Proposed'],
text=delta,
orientation='h',
height=400,
)
fig.add_vline(x=spend[-1], line_width=2, line_dash="dash",
line_color="red")
fig.add_vline(x=max(spend), line_width=2, line_dash="dash",
line_color="green")
fig.update_traces( textposition='outside')
fig.update_layout(
title="Saving/Loss diagram",
xaxis_title="",
yaxis_title="",
legend_title="Vendor names",
width=1000,
)
fig.show()
在此图中,我仅在每个条形图之外显示文本。但现在我要在每个条形图中显示另一个文本。如何在 plotly (python) 中做到这一点?
【问题讨论】:
-
这能回答你的问题吗? stackoverflow.com/a/66837165/16310106
-
试试这个:
fig.update_traces( textposition='inside') -
@Hamzawi,感谢您的评论,但实际上没有,因为该帖子与
matplotlib库有关,但我的问题是关于plotly库。 -
@r-beginners,如果我在我的代码中写
fig.update_traces( textposition='inside'),那么它将在每个图表中显示相同的内容(数字)。但我想在每个图表中显示完全不同的文本。 -
我理解得不够好。要添加一个新的,请使用 add_annotation()。比如你可以写
fig.add_annotation(text="text", xref="domain", yref="domain", x=180, y=1, showarrow=False)