【发布时间】:2015-11-07 10:35:09
【问题描述】:
我有一个熊猫数据框,我想将其绘制为条形图,数据具有以下形式;
Year ISO Value Color
2007 GBR 500 0.303
DEU 444 0.875
FRA 987 0.777
2008 GBR 658 0.303
USA 432 0.588
DEU 564 0.875
2009 ... etc
我尝试按以下方式迭代数据;
import matplotlib.pyplot as plt
import matplotlib.cm as cm
conditions=np.unique[df['Color']]
plt.figure()
ax=plt.gca()
for i,cond in enumerate(conditions):
print 'cond: ',cond
df['Value'].plot(kind='bar', ax=ax, color=cm.Accent(float(i)/n))
minor_XT=ax.get_xaxis().get_majorticklocs()
df['ISO']=minor_XT
major_XT=df.groupby(by=df.index.get_level_values(0)).first()['ISO'].tolist()
df.__delitem__('ISO')
plt.xticks(rotation=70)
ax.set_xticks(minor_XT, minor=True)
ax.set_xticklabels(df.index.get_level_values(1), minor=True, rotation=70)
ax.tick_params(which='major', pad=45)
_=plt.xticks(major_XT, (df.index.get_level_values(0)).unique(), rotation=0)
plt.tight_layout()
plt.show()
但这一切都以一种颜色显示,关于我做错了什么有什么建议吗?
【问题讨论】:
-
您的代码中的
n是什么? -
对不起,n = len(条件)
-
plot(kind='bar')不是为df['Value']中的每个值绘制条形图吗? -
和...?这就是我想要的,我只是希望它们具有与其 ISO 代码相对应的独特颜色
-
这意味着你不需要你的 for 循环。看我的回答
标签: python pandas matplotlib plot bar-chart