[英]Changing Legend Labels and the alignment of the pie chart in python
Can you please help me with the following.你能帮我解决以下问题吗? I have a following pandas series:我有以下熊猫系列:
Male: 40%
Female: 60%
I am building a pie chart as indicated in the code below.我正在构建一个饼图,如下面的代码所示。 I want to know the following:我想知道以下内容:
Is there any automatic way to align the graph as it is now?是否有任何自动方法可以像现在一样对齐图表? I was playing manually with `startangle' to come up with a "horizontal" alignment in relation to "male" and "female"我正在手动使用“startangle”来提出与“男性”和“女性”相关的“水平”对齐方式
How can I automatically link my legend to the labels?如何自动将我的图例链接到标签? As of now, I am manually placing labels = ['Female', 'Male']' (first, female 2nd - male) otherwise they don't match (ie, if I use
labels = ['Male', 'Female']'截至目前,我手动放置labels = ['Female', 'Male']' (first, female 2nd - male) otherwise they don't match (ie, if I use
标签 = ['Male', 'Female' ]'
How Can I get rid of 'Male' and 'Female' on the graph (I want to leave 'Male' and 'Female' just in the legend).我怎样才能摆脱图表上的“男性”和“女性”(我想在图例中留下“男性”和“女性”)。
Thank you for your help.谢谢您的帮助。
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
explode = [0.2,0.02]
fig = plt.figure(figsize=(10, 5), facecolor='White')
ax = fig.add_axes((1, 1, 1, 1))
labels = [‘Female’, 'Male']
colors = sns.color_palette('bright')
q_1 = ax.pie(my_df,
labels=labels,
colors = colors,
autopct = '%0.0f%%',
explode = explode,
shadow = 'True',
startangle = 79.5,
textprops = {'color': 'Black','fontsize':16},
wedgeprops = {'linewidth': 6},
center = (0.1,0.1),
rotatelabels = 'true'),
title = ax.set_title('aaa', fontsize=16)
legend = ax.legend(title='asd', loc=1)
plt.show(q_1)
let me try and answer the questions as you have posed them...让我试着回答你提出的问题......
rotatelabels = 'true'
.要使楔形旁边的标签保持水平,您需要删除行rotatelabels = 'true'
。 This is causing the labels to rotate.这导致标签旋转。Pic after removing the rotatelabels field删除旋转标签字段后的图片
ax.pie
as blanks and add them back in the legend using labels = ['Female', 'Male']要删除楔子旁边的标签并将它们仅放在图例中,您需要将ax.pie
中的标签标记为空白,然后使用 labels = ['Female', 'Male'] 将它们重新添加到图例中Both of these have been updated in the code below to showcase how it can be done.这两个都在下面的代码中进行了更新,以展示它是如何完成的。
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
explode = [0.2,0.02]
fig = plt.figure(figsize=(10, 5), facecolor='White')
ax = fig.add_axes((1, 1, 1, 1))
#labels = ['Female', 'Male']
df=pd.DataFrame({'my_df':[0.60, 0.40], 'labels':['Female', 'Male']})
colors = sns.color_palette('bright')
q_1 = ax.pie(df.my_df,
# labels=df.labels,
labels=['',''],
colors = colors,
autopct = '%0.0f%%',
explode = explode,
shadow = 'True',
startangle = 79.5,
textprops = {'color': 'Black','fontsize':16},
wedgeprops = {'linewidth': 6},
center = (0.1,0.1))
# rotatelabels = 'true'),
title = ax.set_title('aaa', fontsize=16)
#legend = ax.legend(title='asd', loc=1)
ax.legend(title='asd', loc=1, labels = df.labels)
plt.show()
Output plot输出图
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.