Mad*_*dy6 5 python plotly plotly-python
我正在尝试使用 plotly 绘制条形图,我想添加标题和副标题。(在这里,您可以选择添加标题和副标题的任何示例)
我绘制条形图的代码:
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Bar(x=["Apple", 'Mango', 'Banana'], y=[400, 300, 500]))
fig.show()
Run Code Online (Sandbox Code Playgroud)
小智 62
Plotly 获取您的字符串并将其作为 HTML 传递。在标题字符串或 X 轴字符串中添加 HTML 可以让您在绘图图形对象和绘图表达中添加一些快速字幕/说明文字。
<br>是换行符, 是<sup>上标,可让您快速制作较小的副标题或说明文字。
图形对象:
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Bar(x=["Apple", 'Mango', 'Banana'], y=[400, 300, 500]))
fig.update_layout(
title=go.layout.Title(
text="Plot Title <br><sup>Plot Subtitle</sup>",
xref="paper",
x=0
),
xaxis=go.layout.XAxis(
title=go.layout.xaxis.Title(
text="Fruits<br><sup>Fruit sales in the month of January</sup>"
)
)
)
fig.show()
Run Code Online (Sandbox Code Playgroud)
情节地表达:
import plotly.express as px
fig = px.bar(
x=["Apple", 'Mango', 'Banana'],
y=[400, 300, 500],
title = "Plot Title <br><sup>Plot Subtitle</sup>",
labels = {'x':"Fruits<br><sup>Fruit sales in the month of January</sup>",
'y':'count'}
)
fig.show()
Run Code Online (Sandbox Code Playgroud)
数字:
使用fig.update_layout(title_text='Your title')您的标题。字幕没有内置选项。但是您可以通过将 x 轴标签移动到顶部并同时在右下角插入注释来获得所需的效果。我也尝试过使用其他 y 值,但似乎没有办法在情节本身之外获得注释。您还可以更改标题和副标题的字体,使它们从其他标签中脱颖而出。
阴谋:
代码:
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Bar(x=["Apple", 'Mango', 'Banana'], y=[400, 300, 500]))
fig.update_layout(title=go.layout.Title(text="Caption", font=dict(
family="Courier New, monospace",
size=22,
color="#0000FF"
)))
fig.update_layout(annotations=[
go.layout.Annotation(
showarrow=False,
text='Subtitle',
xanchor='right',
x=1,
xshift=275,
yanchor='top',
y=0.05,
font=dict(
family="Courier New, monospace",
size=22,
color="#0000FF"
)
)])
fig['layout']['xaxis'].update(side='top')
fig.show()
Run Code Online (Sandbox Code Playgroud)