更改图例标签和Python中饼图的对齐

发布于 2025-02-13 03:31:56 字数 1116 浏览 0 评论 0原文

您能帮我帮助我吗?我有以下熊猫系列:

Male: 40%
Female: 60%

我正在构建一个饼图,如下面的代码所示。我想知道以下内容:

  1. 是否有任何自动方法可以像现在这样对齐图形?我正在用“ startangle”手动玩耍,以提出与“男性”和“女性”和“女性”

    的“水平”对准

  2. 有关的“水平”对齐方式。截至目前,我正在手动放置labels = ['emair','ama']'(首先,雌性第二-Ma),否则它们不匹配(即,如果我使用 labels = [[ “男性”,“女性”]'

  3. 我如何在图表上摆脱“男性”和“女性”(我想在传说中离开“男性”和“女性”)。

感谢您的帮助。

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:

  1. 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"

  2. 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 uselabels = ['Male', 'Female']'

  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

熊抱啵儿 2025-02-20 03:31:56

让我尝试回答您提出的问题...

  1. 要保持楔形水平旁边的标签,您需要删除行rotatelabels ='true'。这导致标签旋转。

pic删除rotatelabels字段

“在此处输入图像说明”

  1. 您正在使用列表,将提供标签。如果您创建一个带有数据,标签甚至颜色列的数据框,则可以避免。
  2. 要删除楔子旁边的标签并仅在传说中将其放置,您将需要在ax.pie中标记标签为空白,并使用标签= [ ','男性']

这两个都在下面的代码中进行了更新,以展示如何完成。

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()

输出图

”在此处输入图像描述”

let me try and answer the questions as you have posed them...

  1. To keep the labels next to the wedges horizontal, you need to remove the line rotatelabels = 'true'. This is causing the labels to rotate.

Pic after removing the rotatelabels field

enter image description here

  1. As you are using lists, the labels are to be supplied. If you create a dataframe with columns for data, labels and even colors, this can be avoided.
  2. To remove the labels next to the wedges and have them only in the legend, you will need to mark the labels within the 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.

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

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文