绘制一个系列饼图

时间:2017-12-22 13:22:33

标签: python pandas matplotlib plot pie-chart

我有一个系列df2,我想用饼图

绘制
kubectl patch configmap nginx-config -p '{"LogFormat":\'$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $request_time <"$request_body" >"$resp_body"\'}'

我试过了

 Index
Friday       2
Thursday     2
Wednesday    3

ValueError:无法将字符串转换为float:'Wednesday'

2 个答案:

答案 0 :(得分:2)

考虑pd.Series -

s

Index
Monday       2
Tuesday      4
Wednesday    5
Thursday     2
Friday       1
Saturday     6
Sunday       3
Name: counts, dtype: int64

现在,在pd.Series.plot.pie列上调用counts -

s.plot.pie(y=df.index,
           shadow=False,
           colors=colors, 
           explode=(0, 0, 0, 0, 0.15, 0, 0),   # exploding 'Friday'
           startangle=90,
           autopct='%1.1f%%')

plt.axis('equal')
plt.tight_layout()
plt.show()

别打电话给plot()!在pie上致电plot

enter image description here

如果您需要按工作日排序索引,请务必将其转换为pd.Categorical -

cat = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday', 'Sunday']

df.index = pd.Categorical(df.index, categories=cat, ordered=True)
df = df.sort_index()

然后,您可以如上图所示进行绘图。

答案 1 :(得分:0)

让我们试试这个:

colors =  ["#E13F29", "#D69A80", "#D63B59", "#AE5552", "#CB5C3B", "#EB8076", "#96624E"]
df.groupby(df.Index.dt.strftime('%A'))['Index'].count().plot.pie(figsize=(5,5),colors=colors,
                                                                 explode=(0,0,0,0.1),
                                                                 startangle=90,
                                                                 autopct='%1.1f%%')

输出:

enter image description here