Python 熊猫在图表上显示多个条形图
我的数据如下所示:Python 熊猫在图表上显示多个条形图,python,pandas,matplotlib,Python,Pandas,Matplotlib,我的数据如下所示: 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 日期列可以有重复的日期。我想画出每个术语的变化1和变化2。我想让术语作为x轴,change1和change2共享相同的y轴,但可以并排绘制为条形图。 我知道如何做y轴部分,但不知道如何设置x轴。我还希望每个学期都能以某种方
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
日期列可以有重复的日期。我想画出每个术语的变化1和变化2。我想让术语作为x轴,change1和change2共享相同的y轴,但可以并排绘制为条形图。
我知道如何做y轴部分,但不知道如何设置x轴。我还希望每个学期都能以某种方式显示日期,如果可能的话,否则这不是一个优先事项
以下是我所拥有的:
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()
谢谢,将沿
x轴的标签设置为term
s的一种方法是将term
设置为索引:
df = df.set_index(['term'])
比如说,
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()
或者,您可以显式设置Xticklabel,而不是设置索引:
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()
根据评论中的问题,
要为每个日期创建新的绘图,您可以在
df.groupby(['date'])
:
谢谢,我刚刚意识到我有大约200个术语,这让图表变得非常混乱。是否可以显示每个期间的条款及其变化?因此,对于每个期间,都有一个属于该期间的术语图表?是一个期间
是一个日期
?是的,它是一个日期,但如果日期不重复,则它们之间的唯一区别是年份和月份。
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()