Python Plotly:如何在组合图表中注释图形对象条形图?
我有一个折线图和条形图的组合图。Plotly允许我对条形图或折线图进行注释。如何对两者进行注释Python Plotly:如何在组合图表中注释图形对象条形图?,python,plotly,plotly-dash,Python,Plotly,Plotly Dash,我有一个折线图和条形图的组合图。Plotly允许我对条形图或折线图进行注释。如何对两者进行注释 fig_time_series = go.Figure( go.Scatter( x=errors_df["date"], y=errors_df["count_cumsum"], mode="lines+markers+text&q
fig_time_series = go.Figure(
go.Scatter(
x=errors_df["date"],
y=errors_df["count_cumsum"],
mode="lines+markers+text",
text=errors_df["count_cumsum"],
textposition="top center",
name="Cumulative errors"
)
)
fig_time_series.update_layout(title="Error Trend")
fig_time_series.add_trace(
go.Bar(
x=errors_df["date"],
y=errors_df["count"],
text=errors_df["count"],
name="Monthly errors",
)
)
这给了我-
也请您帮助注释条形图。通过将
go.bar中的textposition
设置为['inside'、'outside'、'auto'、'none']中的一个,我能够获得条形图显示的文本值。使用示例代码,它看起来是这样的:
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(
go.Scatter(
x=[0, 1, 2, 3, 4, 5],
y=[1.5, 1, 1.3, 0.7, 0.8, 0.9],
mode="lines+markers+text",
text = ['a','a','a','a','a','a'],
textposition= 'top center'
))
fig.add_trace(
go.Bar(
x=[0, 1, 2, 3, 4, 5],
y=[1, 0.5, 0.7, -1.2, 0.3, 0.4],
text = ['b','b','b','b','b', 'b'],
textposition = 'auto' # it must be one of: ['inside', 'outside', 'auto', 'none']
))
fig.show()
实际图形如下所示:
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(
go.Scatter(
x=[0, 1, 2, 3, 4, 5],
y=[1.5, 1, 1.3, 0.7, 0.8, 0.9],
mode="lines+markers+text",
text = ['a','a','a','a','a','a'],
textposition= 'top center'
))
fig.add_trace(
go.Bar(
x=[0, 1, 2, 3, 4, 5],
y=[1, 0.5, 0.7, -1.2, 0.3, 0.4],
text = ['b','b','b','b','b', 'b'],
textposition = 'auto' # it must be one of: ['inside', 'outside', 'auto', 'none']
))
fig.show()
通过将go.Bar
中的textposition
设置为['inside'、'outside'、'auto'、'none']中的一个,我能够获得条形图显示的文本值。使用示例代码,它看起来是这样的:
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(
go.Scatter(
x=[0, 1, 2, 3, 4, 5],
y=[1.5, 1, 1.3, 0.7, 0.8, 0.9],
mode="lines+markers+text",
text = ['a','a','a','a','a','a'],
textposition= 'top center'
))
fig.add_trace(
go.Bar(
x=[0, 1, 2, 3, 4, 5],
y=[1, 0.5, 0.7, -1.2, 0.3, 0.4],
text = ['b','b','b','b','b', 'b'],
textposition = 'auto' # it must be one of: ['inside', 'outside', 'auto', 'none']
))
fig.show()
实际图形如下所示:
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(
go.Scatter(
x=[0, 1, 2, 3, 4, 5],
y=[1.5, 1, 1.3, 0.7, 0.8, 0.9],
mode="lines+markers+text",
text = ['a','a','a','a','a','a'],
textposition= 'top center'
))
fig.add_trace(
go.Bar(
x=[0, 1, 2, 3, 4, 5],
y=[1, 0.5, 0.7, -1.2, 0.3, 0.4],
text = ['b','b','b','b','b', 'b'],
textposition = 'auto' # it must be one of: ['inside', 'outside', 'auto', 'none']
))
fig.show()
只需在go.Bar
中包含text
和textposition
:
fig.add_traces(go.Bar(x=df['x'], y = df['y1'],
text = df['y2'],
textposition="outside"
))
textposition
的其他选项包括:
['inside', 'auto', 'none']
默认值是none
,正如您已经发现的那样
完整代码:
只需将text
和textposition
包含在go.Bar
中即可:
fig.add_traces(go.Bar(x=df['x'], y = df['y1'],
text = df['y2'],
textposition="outside"
))
textposition
的其他选项包括:
['inside', 'auto', 'none']
默认值是none
,正如您已经发现的那样
完整代码:
我根据一些随机数提出了一个建议。下一次你发布一个问题,请考虑包括你的数据样本,如伟大的信息,谢谢。我对细节投了赞成票。我根据一些随机数提出了一个建议。下一次你发布一个问题,请考虑包括你的数据样本,如伟大的信息,谢谢。投票赞成细节,理解错误。谢谢,我理解这个错误。非常感谢。