【问题标题】:How to plot grouped bar chart with multiple y axes in python plotly [duplicate]如何在python中绘制具有多个y轴的分组条形图
【发布时间】:2018-08-15 13:37:56
【问题描述】:

我需要绘制一个带有两个 y 轴和一个 x 轴的分组条形图。 如果在 matplotlib 中绘制,该图如下所示

我想通过 python plotly 但我找不到解决方案。两年前好像也有类似的问题

https://stackoverflow.com/questions/29046057/plotly-grouped-bar-chart-with-multiple-axes

但当我尝试以类似方式进行时却没有成功。

这是我测试的代码sn-p:

import plotly
import plotly.graph_objs as go
import numpy as np
import pandas as pd
from io import StringIO

s = StringIO("""     amount     price
A     40929   4066443
B     93904   9611272
C    188349  19360005
D    248438  24335536
E    205622  18888604
F    140173  12580900
G     76243   6751731
H     36859   3418329
I     29304   2758928
J     39768   3201269
K     30350   2867059""")

df = pd.read_csv(s, index_col=0, delimiter=' ', skipinitialspace=True)

plotly.offline.plot({
    "data": [go.Bar(x=df.index, y=df.price, name="price"),
             go.Bar(x=df.index, y=df.amount, name="amount", yaxis='y2'),
             ],
    "layout": go.Layout(title="Amount and price", 
                        yaxis=dict(
                            title='price',
                            autorange = True,
                            range = [0, max(df.price)],
                        ),
                        yaxis2=dict(
                            title='amount',
                            autorange = True,
                            range = [0, max(df.amount)],
                                overlaying='y',
                                side='right'                                
                        ),           
                        barmode='group',
                        autosize=True),
})

请注意我给了 barmode='group' 但这并没有生效。

【问题讨论】:

    标签: python bar-chart plotly multiple-axes


    【解决方案1】:

    使用secondary_y:

    df.plot.bar(secondary_y=['price'])
    

    输出:

    【讨论】:

    • 感谢您的回复。但我希望使用 Plotly 而不是 matplotlib 来完成。
    猜你喜欢
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-16
    • 2022-01-09
    • 1970-01-01
    相关资源
    最近更新 更多