繁体   English   中英

饼图的label按特定方式排序

[英]Sort label of a pie chart in a specific way

我想制作饼图 plot 来显示调查结果。 我会尽量保持简单。

在我调查的一个问题上,有几种响应类型字符串,就像这个例子:

问题:“你练习魔法吗”

可能的回答:“是”、“不确定”、“不是故意的”、“不是”

然后我制作了一个饼图,其中包含每种类型的响应比例。 但是在饼图上,响应以我不希望的方式排序:

在此处输入图像描述

我想要的,按顺时针方向,从 0°(如果我是对的,0° 度在圆圈的“右边”):“不”、“不确定”、“不是故意的”、“是的”。 对我来说,它需要一种我无法理解的特定索引排序,因为它不是按字母顺序排列的,不是升序的,什么都不是,只是“我的”方式。

有关信息,请拨打 dataframe:

q1 : do you pratice magic ?
no                   6
not intentionally    2
not sure             1
yes                  3
dtype: int64

如果我列出索引:

In : df2.index
Out: Index(['no', 'not intentionally', 'not sure', 'yes'], dtype='object', name='q1 : do you pratice magic ?')

这是我的 plot 代码:

fig = plt.figure(figsize=(8, 6),dpi=60)
fig.suptitle(('Some title'),fontsize=20, fontweight='bold', **csfont)

#############################
ax = fig.add_subplot()
colors = iter(cm.rainbow(np.linspace(0, 4)))
label_font=18
startang=0
ax.pie(df2, 
        labels=df2.index,
        autopct='%1.0f%%',
        wedgeprops=dict(width=0.4, edgecolor="w",linewidth= 1, linestyle= '-', antialiased= True), 
        colors=colors, 
        startangle=startang, 
        textprops={'fontsize': label_font, **csfont}, 
        pctdistance=0.8, 
        labeldistance=1.15,
        )

ax.axis('equal')
##################

任何的想法?

哦,亲爱的,我刚找到它!

只需创建您的特定订单的列表:

reorderindex = ['no', 'not sure', 'not intentionally', 'yes'.]
# the old one tocompare : ['no', 'not intentionally', 'not sure', 'yes']

并用它重新索引:

df2 = df2.reindex(reorderindex)

你好!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM