简体   繁体   English

如何从饼图中删除一些标签

[英]How to remove some labels from a pie chart

I have a dataframe that looks like this, but larger:我有一个看起来像这样的数据框,但更大:

title_of_the_novel                author          publishing_year   mentioned_cities   
0   Beasts and creatures        Bruno Ivory             1850           London 
0   Monsters                    Renata Mcniar           1866           New York 
0   At risk                     Charles Dobi            1870           New York   
0   Manuela and Ricardo         Lucas Zacci             1889           Rio de Janeiro
0   War against the machine     Angelina Trotter        1854           Paris


df_1880_1890 = pd.DataFrame({'title_of_the_novel': [Beasts and creatures, Monsters],
                   'author': [Bruno Ivory, Renata Mcniar]},
                   'publishing_year': ['1850','1866'] 
                   'mentioned_cities': ['London','New York']


          

I have successfully plotted it on a pie chart using the following code:我已使用以下代码成功地将其绘制在饼图上:

1880s_data = result[df_1880_1890].groupby(['mentioned_cities']).sum().plot(
    kind='pie', y='publishing_year', autopct='%1.1f%%', radius=12, ylabel='', shadow=True)

1880s_data.legend().remove()

1880s_data_image = 1880s_data.get_figure()
1880s_data_image.savefig("1880s_pie_chart.pdf", bbox_inches='tight')

However, as my dataframe has many values, some of the labels on the pie chart represent only 0,5% or 1%.但是,由于我的数据框有很多值,饼图上的一些标签仅代表 0.5% 或 1%。 My objective is to remove all percentages below 4% from this pie chart.我的目标是从这个饼图中删除所有低于 4% 的百分比。 Can someone help me, please?有人能帮助我吗?

The picture of the output: enter link description here输出图片: 在此处输入链接描述

I would very much like all these labels and percentages under 4% removed.我非常希望删除所有这些标签和低于 4% 的百分比。

You should be able to pass a function to autopct within your call to .plot您应该能够在对autopct的调用中将函数传递给.plot

autopct=lambda pct: '{:1.1f}%'.format(pct) if pct > 5 else ''

This will return a formatted string if the percentage of a slice is > 5 or else it will return an empty string (no label)如果切片的百分比> 5 ,这将返回格式化字符串,否则将返回空字符串(无标签)


To also remove the labels, it will be easiest to dip into pure matplotlib for this.要同时删除标签,最容易为此使用纯matplotlib Using the data you provided as df , you can create a pie chart and access the returned text objects for the labels and the percentage labels.使用您作为df提供的数据,您可以创建一个饼图并访问标签和百分比标签的返回文本对象。

All you need to do from there is iterate over them, extract the value underlying the percentage label and update as needed.您需要做的就是迭代它们,提取百分比标签下的值并根据需要进行更新。

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

totals_df = df.groupby(['mentioned_cities']).sum()
wedges, texts, autotexts = ax.pie(totals_df['publishing_year'], labels=totals_df.index, autopct='%1.1f%%')

threshold = 20
for label, pct_label in zip(texts, autotexts):
    pct_value = pct_label.get_text().rstrip('%')
    if float(pct_value) < threshold:
        label.set_text('')
        pct_label.set_text('')

ax.legend(bbox_to_anchor=(1.2, 1))

在此处输入图像描述


to fully remove wedges from a pie chart based on their percentage, we can add 2 lines to our previous code to iterate over the wedges at the same time when we iterate over the text labels and percentage labels.要根据百分比从饼图中完全删除楔形,我们可以在之前的代码中添加 2 行,以便在迭代文本标签和百分比标签的同时迭代楔形。 In our filtering condition we simply make the wedge itself invisible and remove its label so its not added to the legend.在我们的过滤条件中,我们只是使楔形本身不可见并删除其标签,因此它不会添加到图例中。

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

totals_df = df.groupby(['mentioned_cities']).sum()
wedges, texts, autotexts = ax.pie(totals_df['publishing_year'], labels=totals_df.index, autopct='%1.1f%%')

threshold = 20
for wedge, label, pct_label in zip(wedges, texts, autotexts):
    pct_value = pct_label.get_text().rstrip('%')
    if float(pct_value) < threshold:
        label.set_text('')       # remove text label
        pct_label.set_text('')   # remove percentage label
        wedge.set_visible(False) # remove wedge from pie
        wedge.set_label('')      # ensure wedge label does not go into legend

ax.legend(bbox_to_anchor=(1.2, 1))

在此处输入图像描述


To fix the layout of the pie, this turns back into a little bit of a data problem.为了修复饼图的布局,这又变成了一点数据问题。 We need to group all of the below threshold cities together and then remove them from the pie post-hoc.我们需要将所有低于阈值的城市组合在一起,然后将它们从饼图中删除。

totals = df.groupby(['mentioned_cities'])['publishing_year'].sum()
proportions = totals_df / totals_df.sum()

threshold = 0.2
below_thresh_mask = proportions < threshold
plot_data = proportions[~below_thresh_mask]
plot_data.loc[''] = proportions[below_thresh_mask].sum()

fig, ax = plt.subplots()
wedges, texts, autotexts = ax.pie(
    plot_data, labels=plot_data.index, autopct='%1.1f%%'
)

for w, alab in zip(wedges, autotexts):
    if w.get_label() == '':
        w.set_visible(False)
        alab.set_visible(False)

ax.legend(bbox_to_anchor=(1.2, 1))

在此处输入图像描述


Though it may be better to simply group those cities into an "other" category.尽管将这些城市简单地归入“其他”类别可能会更好。

totals = df.groupby(['mentioned_cities'])['publishing_year'].sum()
proportions = totals_df / totals_df.sum()

threshold = 0.2
below_thresh_mask = proportions < threshold
plot_data = proportions[~below_thresh_mask]
plot_data.loc['other'] = proportions[below_thresh_mask].sum()

fig, ax = plt.subplots()
wedges, texts, autotexts = ax.pie(
    plot_data, labels=plot_data.index, autopct='%1.1f%%'
)

ax.legend(bbox_to_anchor=(1.2, 1))

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM