Bar-Plot with two bars and two y-axis(柱状图,有两个柱子和两个y轴)
Bar-Plot with two bars and two y-axis(柱状图,有两个柱子和两个y轴)
我有一个看起来像这样的DataFrame:
amount price
age
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
现在我想绘制一个柱状图,横轴为年龄的标签。对于每个x轴刻度,应该有两根柱子,一根代表数量,一根代表价格。我可以通过简单地使用以下代码实现:
df.plot(kind='bar')
问题在于比例。价格高得多,以至于我无法在图中正确辨认数量,如下图所示:
[图片链接](https://i.stack.imgur.com/8PZSi.png)
因此,我想要一个第二个y轴。我尝试使用以下代码实现:
df.loc[:,'amount'].plot(kind='bar')
df.loc[:,'price'].plot(kind='bar',secondary_y=True)
但是这只会覆盖柱子,而不是将它们放在一起。
有没有办法在不使用低级别matplotlib(显然可以手动将柱子并排放置)的情况下实现这一点?
目前,我正在使用子图中的两个单独的绘图:
df.plot(kind='bar',grid=True,subplots=True,sharex=True);
结果如下图所示:
[图片链接](https://i.stack.imgur.com/oSz6a.png)