Python 如何在熊猫上绘制多行?
假设我有以下Python 如何在熊猫上绘制多行?,python,pandas,bar-chart,categories,Python,Pandas,Bar Chart,Categories,假设我有以下pandasdataframe: In[114]: df Out[114]: 0-10% 11-20% 21-30% 31-40% 41-50% 51-60% 61-70% 71-80% 81-90% \ f 0.186 3.268 3.793 4.554 6.421 6.345 7.383 8.476 8.968 l 1.752 2.205 2.508 2.866 3.132 3.1
pandas
dataframe:
In[114]: df
Out[114]:
0-10% 11-20% 21-30% 31-40% 41-50% 51-60% 61-70% 71-80% 81-90% \
f 0.186 3.268 3.793 4.554 6.421 6.345 7.383 8.476 8.968
l 1.752 2.205 2.508 2.866 3.132 3.157 3.724 4.073 4.905
91-100%
f 12.447
l 8.522
假设我想生成一个条形图,其中列作为x轴上的类别,对于每个类别,两个条形图,一个用于f
,一个用于l
,以便进行比较
如何避免钢筋堆积?
我的尝试将生成堆叠的条形图和x标签的偏移:
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
labels = ['0-10%','11-20%','21-30%','31-40%','41-50%','51-60%','61-70%','71-80%','81-90%','91-100%']
row1 = df.iloc[0]
row1.plot(kind='bar',title='Binned comparison', color='r',stacked=False)
row2 = df.iloc[1]
row2.plot(kind='bar',title='Binned comparison', color='k',stacked=False)
plt.xticks(x,labels, rotation='horizontal',fontsize=8)
您可以在转置上
plot.bar
:
df.T.plot.bar()
谢谢!你如何获得这些条的颜色和x标签的方向?@CF84方向我不知道从我的头顶。颜色,试试:
df.T.plot.bar(颜色=['red','aqua'])