熊猫在图表上显示多个条形图

jxn*_*jxn 3 python matplotlib pandas

我的数据是这样的:

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
Run Code Online (Sandbox Code Playgroud)

日期列可以有重复的日期。我想为每个术语绘制,change1 和 change2 是什么。我想将术语作为 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()
Run Code Online (Sandbox Code Playgroud)

谢谢,

unu*_*tbu 5

使标签x-axis成为terms 的一种方法是设置term为索引:

df = df.set_index(['term'])
Run Code Online (Sandbox Code Playgroud)

例如,

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()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


或者,您可以显式设置 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()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


根据评论中的问题,要为 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()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明