情节结合了子图和2 y轴

发布于 2025-02-05 19:43:12 字数 1320 浏览 1 评论 0原文

嗨,我使用以下代码使用条形图绘制成本与收入。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

歌入人心 2025-02-12 19:43:12

好的,我找到了答案:

fig = (
    make_subplots(rows=2, 
                  cols=1,
                  subplot_titles=('<b>cost  and revenue<b>',  '<b>cost and revenue 2<b'), 
                  specs=[[{"secondary_y": True}], [{"secondary_y": True}]])
)

Ok I found the answer :

fig = (
    make_subplots(rows=2, 
                  cols=1,
                  subplot_titles=('<b>cost  and revenue<b>',  '<b>cost and revenue 2<b'), 
                  specs=[[{"secondary_y": True}], [{"secondary_y": True}]])
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文