如何在饼图中旋转autpct,如果标签已经旋转?

huangapple go评论57阅读模式
英文:

How can I rotate the autpct in a pie plot if labels are already rotated?

问题

I plotted a pie chart and rotate the labels:

我绘制了一个饼图并旋转了标签:

import pandas as pd
import matplotlib.pyplot as plt
data = { 'T1': [1,1,1,1,1,2,2,2,2,2, 1,1,1,1,1, 2,2,2,2,2, 1,1,1,1,1, 2,2,2,2],
         'T2':['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
               'B','B', 'B', 'B', 'B', 'B',
               'C', 'C', 'C', 'C','C', 'C', 'C',
              'D', 'D', 'D', 'D']}
df = pd.DataFrame(data)
df['T1']=df['T1'].astype(str)
fig, ax=plt.subplots()
values2=df['T2'].value_counts().sort_index()
pie1=ax.pie(values2, radius=1.3, autopct='%1.1f%%', labeldistance=0.4,   
       labels=['one', 'two', 'three', 'four'], rotatelabels=True, startangle=223.5, wedgeprops=dict(width=1, edgecolor='w'));
for tx in pie1[1]:
    rot = tx.get_rotation()
    tx.set_rotation(rot+90+(1-rot//180)*180)
hole = plt.Circle((0, 0), 0.5, facecolor='white')
plt.gcf().gca().add_artist(hole)
plt.show()

Now I tried to use:

现在我尝试使用:

for tx in pie1[2]:
    rotate = tx.get_rotation()
    tx.set_rotation(rotate+90+(1-rotate//180)*180)

Because I understood that the 2 in pie12 refers to the autopct (am I right?). But then the percentages are rotated in a different way than the labels. Has anyone an idea why?

因为我理解pie12中的2是指autopct(我理解正确吗?)。但是百分比的旋转方式与标签不同。有人知道为什么吗?

英文:

I plotted a pie chart and rotate the labels:

import pandas as pd
import matplotlib.pyplot as plt
data = { 'T1': [1,1,1,1,1,2,2,2,2,2, 1,1,1,1,1, 2,2,2,2,2, 1,1,1,1,1, 2,2,2,2],
         'T2':['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
               'B','B', 'B', 'B', 'B', 'B',
               'C', 'C', 'C', 'C','C', 'C', 'C',
              'D', 'D', 'D', 'D']}
df = pd.DataFrame(data)
df['T1']=df['T1'].astype(str)
fig, ax=plt.subplots()
values2=df['T2'].value_counts().sort_index()
pie1=ax.pie(values2, radius=1.3, autopct='%1.1f%%', labeldistance=0.4,   
       labels=['one', 'two', 'three', 'four'], rotatelabels=True, startangle=223.5, wedgeprops=dict(width=1, edgecolor='w'));
for tx in pie1[1]:
    rot = tx.get_rotation()
    tx.set_rotation(rot+90+(1-rot//180)*180)
hole = plt.Circle((0, 0), 0.5, facecolor='white')
plt.gcf().gca().add_artist(hole)
plt.show()

如何在饼图中旋转autpct,如果标签已经旋转?

I can't use startangle a 2nd time to rotate the percentages. How can I do this to give the pie chart a better look?

Now I tried to use:

for tx in pie1[2]:
    rotate = tx.get_rotation()
    tx.set_rotation(rotate+90+(1-rotate//180)*180)

Because I understood that the 2 in pie2 refers to the autopct (am I right?). But then the percentages are rotated in a different way than the labels. Has anyone an idea why?
如何在饼图中旋转autpct,如果标签已经旋转?

答案1

得分: 1

pie 返回一个列表的元组:

patches列表
matplotlib.patches.Wedge 实例的序列

texts列表
标签 Text 实例的列表

autotexts列表
数值标签的 Text 实例的列表。这将仅返回..

因此,您可以解包它并在 // 中旋转 标签/百分比

p, t, a = ax.pie(
    values2, radius=1.3, autopct='%1.1f%%', labeldistance=0.4,
    labels=['one', 'two', 'three', 'four'], rotatelabels=True, startangle=223.5,
    wedgeprops=dict(width=1, edgecolor='w')
)

for tx, pc in zip(t, a):
    rot = tx.get_rotation()
    tx.set_rotation(rot+90+(1-rot//180)*180)
    pc.set_rotation(tx.get_rotation())

输出:

如何在饼图中旋转autpct,如果标签已经旋转?

英文:

The pie returns a tuple of lists :

> patches : list
> A sequence of matplotlib.patches.Wedge instances
>
> texts : list
> A list of the label Text instances.
>
> autotexts : list
> A list of Text instances for the numeric labels. This
> will only be returned ..

So you can unpack it and rotate the labels/percentages in // :

p, t, a = ax.pie(
    values2, radius=1.3, autopct='%1.1f%%', labeldistance=0.4,
    labels=['one', 'two', 'three', 'four'], rotatelabels=True, startangle=223.5,
    wedgeprops=dict(width=1, edgecolor='w')
)

for tx, pc in zip(t, a):
    rot = tx.get_rotation()
    tx.set_rotation(rot+90+(1-rot//180)*180)
    pc.set_rotation(tx.get_rotation())

Output :

如何在饼图中旋转autpct,如果标签已经旋转?

huangapple
  • 本文由 发表于 2023年5月24日 19:24:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323008.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定