情节结合了子图和2 y轴
嗨,我使用以下代码使用条形图绘制成本与收入。
import plotly.graph_objects as go
from plotly.subplots import make_subplots
fig = make_subplots(specs=[[{"secondary_y": True}]])
fig.add_trace(
go.Bar(
name="cost",
x=c1["country"],
y=c1["cost"],
offsetgroup=1),
secondary_y=False,
)
fig.add_trace(
go.Bar(
name="revenue",
x=c2["country"],
y=c2["revenue"],
offsetgroup=2),
secondary_y=True,
)
# Add figure title
fig.update_layout(
title_text="<b>title<b>"
)
fig.update_xaxes(title_text="<b>Country</b>")
fig.update_yaxes(title_text="<b>cost</b>", secondary_y=False)
fig.update_yaxes(title_text="<b>revenue</b>", secondary_y=True)
fig.show()
我想做同样的事情,但有2个子图。我在下面找到了代码,但是当我尝试使用双Y轴图时,它无效,
fig = make_subplots(rows=2, cols=1,subplot_titles=('Cost in % of Total', 'title'))
fig.add_trace(
go.Bar(
name="cost",
x=con_pie["product"],
y=con_pie["cost"],
),
row=1,
col=1,
)
fig.add_trace(
go.Bar(
name="revenue",
x=con_pie2["product"],
y=con_pie2["revenue"],
),
row=2,
col=1,
)
for i in fig['layout']['annotations']:
i['font'] = dict(size=14)
fig.show()
谢谢您的帮助。
Hi I used the code below to plot the cost vs revenue using bar charts.
import plotly.graph_objects as go
from plotly.subplots import make_subplots
fig = make_subplots(specs=[[{"secondary_y": True}]])
fig.add_trace(
go.Bar(
name="cost",
x=c1["country"],
y=c1["cost"],
offsetgroup=1),
secondary_y=False,
)
fig.add_trace(
go.Bar(
name="revenue",
x=c2["country"],
y=c2["revenue"],
offsetgroup=2),
secondary_y=True,
)
# Add figure title
fig.update_layout(
title_text="<b>title<b>"
)
fig.update_xaxes(title_text="<b>Country</b>")
fig.update_yaxes(title_text="<b>cost</b>", secondary_y=False)
fig.update_yaxes(title_text="<b>revenue</b>", secondary_y=True)
fig.show()
I would like to do the same but with 2 subplots. I found the code below but it didn't work when I tried to use a double Y axis chart
fig = make_subplots(rows=2, cols=1,subplot_titles=('Cost in % of Total', 'title'))
fig.add_trace(
go.Bar(
name="cost",
x=con_pie["product"],
y=con_pie["cost"],
),
row=1,
col=1,
)
fig.add_trace(
go.Bar(
name="revenue",
x=con_pie2["product"],
y=con_pie2["revenue"],
),
row=2,
col=1,
)
for i in fig['layout']['annotations']:
i['font'] = dict(size=14)
fig.show()
Thank you for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我找到了答案:
Ok I found the answer :