【发布时间】:2017-11-13 13:47:15
【问题描述】:
您如何使用 pandas 数据框plot 方法仅绘制不同颜色的条形图?
如果我有这个数据框:
df = pd.DataFrame({'count': {0: 3372, 1: 68855, 2: 17948, 3: 708, 4: 9117}}).reset_index()
index count
0 0 3372
1 1 68855
2 2 17948
3 3 708
4 4 9117
我需要设置哪些df.plot() 参数,以便图中的每个条形:
- 使用“配对”颜色图
- 用不同的颜色绘制每个条形
我正在尝试什么:
df.plot(x='index', y='count', kind='bar', label='index', colormap='Paired', use_index=False)
结果:
我已经知道的(是的,这行得通,但我的目的是要弄清楚如何仅使用df.plot 来做到这一点。肯定有可能吗?):
def f(df):
groups = df.groupby('index')
for name,group in groups:
plt.bar(name, group['count'], label=name, align='center')
plt.legend()
plt.show()
【问题讨论】:
标签: python pandas matplotlib plot