更改图例标签和Python中饼图的对齐
您能帮我帮助我吗?我有以下熊猫系列:
Male: 40%
Female: 60%
我正在构建一个饼图,如下面的代码所示。我想知道以下内容:
是否有任何自动方法可以像现在这样对齐图形?我正在用“ startangle”手动玩耍,以提出与“男性”和“女性”和“女性”
的“水平”对准有关的“水平”对齐方式。截至目前,我正在手动放置
labels = ['emair','ama']'(首先,雌性第二-Ma),否则它们不匹配(即,如果我使用
labels = [[ “男性”,“女性”]'我如何在图表上摆脱“男性”和“女性”(我想在传说中离开“男性”和“女性”)。
感谢您的帮助。
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)
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"
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 (i.e., if I use
labels = ['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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让我尝试回答您提出的问题...
rotatelabels ='true'
。这导致标签旋转。pic删除rotatelabels字段
ax.pie
中标记标签为空白,并使用标签= [ ','男性']这两个都在下面的代码中进行了更新,以展示如何完成。
输出图
let me try and answer the questions as you have posed them...
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']Both of these have been updated in the code below to showcase how it can be done.
Output plot