英文:
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()
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?
答案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())
输出:
英文:
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 :
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论