Pandas:带有两个条形和两个 y 轴的条形图 [英] Pandas: Bar-Plot with two bars and two y-axis

查看:35
本文介绍了Pandas:带有两个条形和两个 y 轴的条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的 DataFrame:

I have a DataFrame looking like this:

     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 轴上的年龄作为标签绘制条形图.对于每个 x-tick,应该有两根柱线,一根柱线表示数量,一根柱线表示价格.我可以简单地使用:

Now I'd like to plot a bar-plot with the age on the x-axis as labels. For each x-tick there should be two bars, one bar for the amount, and one for the price. I can get this working by using simply:

df.plot(kind='bar')

问题在于缩放.价格太高了,我无法真正确定该图中的金额,请参阅:

The problem is the scaling. The prices are so much higher that I can not really identify the amount in that graph, see:

因此我想要第二个 y 轴.我尝试使用:

Thus I'd like a second y-axis. I tried it using:

df.loc[:,'amount'].plot(kind='bar')
df.loc[:,'price'].plot(kind='bar',secondary_y=True)

但这只会覆盖条形而不是并排放置它们.有没有什么方法可以做到这一点而不必访问较低级别的 matplotlib(显然可以通过手动并排放置条形图)?

but this just overwrites the bars and does NOT place them side-by-side. Is there any way to do this without having to access the lower-level matplotlib (which would be possible obviously by placing the bars side by side manually)?

现在,我在子图中使用两个单图:

For now, I'm using two single plots within subplots:

df.plot(kind='bar',grid=True,subplots=True,sharex=True); 

导致:

推荐答案

使用新的 pandas 版本(0.14.0 或更高版本),以下代码将起作用.为了创建两个轴,我手动创建了两个 matplotlib 轴对象(axax2),它们将用于两个条形图.

Using the new pandas release (0.14.0 or later) the below code will work. To create the two axis I have manually created two matplotlib axes objects (ax and ax2) which will serve for both bar plots.

绘制数据框时,您可以使用 ax=... 选择坐标区对象.同样为了防止两个图重叠,我修改了它们与 position 关键字参数对齐的位置,默认为 0.5 但这意味着两个条形图重叠.

When plotting a Dataframe you can choose the axes object using ax=.... Also in order to prevent the two plots from overlapping I have modified where they align with the position keyword argument, this defaults to 0.5 but that would mean the two bar plots overlapping.

import matplotlib.pyplot as plt
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)

fig = plt.figure() # Create matplotlib figure

ax = fig.add_subplot(111) # Create matplotlib axes
ax2 = ax.twinx() # Create another axes that shares the same x-axis as ax.

width = 0.4

df.amount.plot(kind='bar', color='red', ax=ax, width=width, position=1)
df.price.plot(kind='bar', color='blue', ax=ax2, width=width, position=0)

ax.set_ylabel('Amount')
ax2.set_ylabel('Price')

plt.show()

这篇关于Pandas:带有两个条形和两个 y 轴的条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆