简体   繁体   English

Plotly 组合子图和 2 y 轴

[英]Plotly Combine Subplots and 2 y axis

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.我想做同样的事情,但有 2 个子图。 I found the code below but it didn't work when I tried to use a double Y axis chart我找到了下面的代码,但是当我尝试使用双 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()

Thank you for your help.谢谢您的帮助。

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}]])
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM