我有一个熊猫数据框:
import pandas as pd
data1 = {'Date':['03-19-2019'],
'Total':[35],
'Solved':[19],
'Arrived':[23],
}
df1 = pd.DataFrame(data1)
我想绘制这样的条形图:
使用
df1.plot(kind='barh',x='Date',y='Total', ax=ax0, color='#C0C0C0',
width=0.5)
df1.plot(kind='barh',x='Date',y='Arrived', ax=ax0, color='#C0FFFF',
width=0.5)
df1.plot(kind='barh',x='Date',y='Solved', ax=ax0, color='#C0C0FF',
width=0.5)
但是,为了避免重叠,我必须考虑到每一列的值都较大(总和大于已到达大于已解决)来绘制每一列。
如何避免这样做并轻松实现此过程的自动化?
答案 0 :(得分:1)
在Pandas中必须有一种简单直接的方法,但是我只是想出了这种快速的解决方法。想法如下:
Date
,然后对其余的列进行排序。fig, ax0 = plt.subplots()
ids = np.argsort(df1.values[0][1:])[::-1]
colors = {'Total': '#C0C0C0', 'Arrived': '#C0FFFF', 'Solved':'#C0C0FF'}
for col in np.array(df1.columns[1:].tolist())[ids]:
df1.plot(kind='barh',x='Date',y=col, ax=ax0, color=colors[col], width=0.1)
答案 1 :(得分:0)