pandas 熊猫在图表上显示多个条形图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31304131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pandas show multiple bar graphs on a chart
提问by jxn
my data looks like this:
我的数据是这样的:
term date change1 change2
aaa 2010-03-01 23.00 24.31
bbb 2010-03-01 25.00 0.00
ccc 2012-05-01 100.00 100.00
The date column can have repeated dates. I want to plot for each term, what the change1 and change2 is. I was thinking to have the term as the x-axis and change1 and change2 share the same y axis but be plotted as bar charts side by side. I know how to do the y axis part but dont know how to set the x axis. I would also like each term to somehow show the date as well if possible otherwise this is not a priority.
日期列可以有重复的日期。我想为每个术语绘制,change1 和 change2 是什么。我想将术语作为 x 轴,change1 和 change2 共享相同的 y 轴,但并排绘制为条形图。我知道如何做 y 轴部分,但不知道如何设置 x 轴。如果可能的话,我还希望每个学期都以某种方式显示日期,否则这不是优先事项。
Heres what i have:
这是我所拥有的:
fig = plt.figure()
ax = fig.add_subplot(111)
ax2 = ax.twinx()
df.change1.plot(kind = 'bar', color = 'red', ax = ax , position = 1)
df.change2.plot(kind = 'bar', color = 'blue', ax = ax2, position = 2)
ax.set_ylabel= ('change1')
ax2.set_ylabel=('change2')
plt.show()
Thanks,
谢谢,
回答by unutbu
One way to make the labels along the x-axisto be terms is to set termas the index:
使标签x-axis成为terms 的一种方法是设置term为索引:
df = df.set_index(['term'])
For example,
例如,
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({'change1': [23.0, 25.0, 100.0],
'change2': [24.309999999999999, 0.0, 100.0],
'date': ['2010-03-01', '2010-03-01', '2012-05-01'],
'term': ['aaa', 'bbb', 'ccc']})
df = df.set_index(['term'])
fig = plt.figure()
ax = fig.add_subplot(111)
ax2 = ax.twinx()
df['change1'].plot(kind='bar', color='red', ax=ax, position=0, width=0.25)
df['change2'].plot(kind='bar', color='blue', ax=ax2, position=1, width=0.25)
ax.set_ylabel = ('change1')
ax2.set_ylabel = ('change2')
plt.show()


Or, instead of setting the index, you could set the xticklabels explicitly:
或者,您可以显式设置 xticklabels,而不是设置索引:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({'change1': [23.0, 25.0, 100.0],
'change2': [24.309999999999999, 0.0, 100.0],
'date': ['2010-03-01', '2010-03-01', '2012-05-01'],
'term': ['aaa', 'bbb', 'ccc']})
fig = plt.figure()
ax = fig.add_subplot(111)
ax2 = ax.twinx()
df['change1'].plot(kind='bar', color='red', ax=ax, position=0, width=0.25)
df['change2'].plot(kind='bar', color='blue', ax=ax2, position=1, width=0.25)
ax.set_ylabel = 'change1'
ax2.set_ylabel = 'change2'
labels = ['{}\n{}'.format(date, term) for date, term in zip(df['date'], df['term'])]
ax.set_xticklabels(labels, minor=False)
fig.autofmt_xdate()
plt.show()


Per the question in the comments,
to create a new plot for each date, you could iterate over the groups in
df.groupby(['date']):
根据评论中的问题,要为 each 创建一个新图date,您可以遍历 中的组
df.groupby(['date']):
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({'change1': [23.0, 25.0, 100.0],
'change2': [24.309999999999999, 0.0, 100.0],
'date': ['2010-03-01', '2010-03-01', '2012-05-01'],
'term': ['aaa', 'bbb', 'ccc']})
groups = df.groupby(['date'])
fig, axs = plt.subplots(nrows=groups.ngroups)
for groupi, ax in zip(groups,axs):
index, grp = groupi
ax2 = ax.twinx()
grp['change1'].plot(kind='bar', color='red', ax=ax, position=0, width=0.25)
grp['change2'].plot(kind='bar', color='blue', ax=ax2, position=1, width=0.25)
ax.set_ylabel = 'change1'
ax2.set_ylabel = 'change2'
ax.set_title(index)
ax.set_xticklabels(grp['term'].tolist(), minor=False, rotation=0)
fig.tight_layout()
plt.show()



