Python:如何覆盖pandas图中的2条图

时间:2018-04-24 22:09:22

标签: python pandas matplotlib seaborn

我想用相同的x-xis叠加2条图。 我有一个包含这些数据的数据框:

df = pd.DataFrame({'a':[1,2,3,1,2,2,2],
             'b':[1,1,1,3,2,2,2]})

我可以为a和b绘制2个直方图:

df['a'].value_counts().plot(kind = 'bar')
df['b'].value_counts().plot(kind = 'bar')

enter image description here enter image description here

如何将其中一个叠加在另一个上?

我正在寻找的无关图表格式的示例: enter image description here

1 个答案:

答案 0 :(得分:1)

使用widthax参数:

ax = df['a'].value_counts().plot(kind='bar', color='blue', width=.75, legend=True, alpha=0.8)
df['b'].value_counts().plot(kind='bar', color='maroon', width=.5, alpha=1, legend=True)

输出:

enter image description here

相关问题