Python 有条件删除Matplotlib饼图中的标签

Python 有条件删除Matplotlib饼图中的标签,python,matplotlib,Python,Matplotlib,我有以下代码: import matplotlib.pyplot as plt import numpy as np np.random.seed(123456) import pandas as pd df = pd.DataFrame(3 * np.random.rand(4, 4), index=['a', 'b', 'c', 'd'], columns=['x', 'y','z','w']) plt.style.use('ggplot') col

我有以下代码:

import matplotlib.pyplot as plt
import numpy as np
np.random.seed(123456)
import pandas as pd

df = pd.DataFrame(3 * np.random.rand(4, 4), index=['a', 'b', 'c', 'd'], 
                  columns=['x', 'y','z','w'])

plt.style.use('ggplot')
colors = plt.rcParams['axes.color_cycle']

fig, axes = plt.subplots(nrows=2, ncols=3)
for ax in axes.flat:
    ax.axis('off')

for ax, col in zip(axes.flat, df.columns):
    ax.pie(df[col], labels=df.index, autopct='%.2f', colors=colors)
    ax.set(ylabel='', title=col, aspect='equal')

axes[0, 0].legend(bbox_to_anchor=(0, 0.5))

fig.savefig('your_file.png') # Or whichever format you'd like
plt.show()
产生以下结果:


我的问题是,如何根据条件移除标签。例如,我只想显示百分比>20%的标签。这样
a、c、d的标签和值就不会显示在X等中。

中的
autoct
参数可以是可调用的,它将接收当前的百分比。因此,您只需要提供一个函数,该函数为要忽略百分比的值返回一个空字符串

def my_autopct(pct):
    return ('%.2f' % pct) if pct > 20 else ''

ax.pie(df[col], labels=df.index, autopct=my_autopct, colors=colors)
如果需要参数化
autoct
参数上的值,则需要一个返回函数的函数,如:

def autopct_generator(limit):
    def inner_autopct(pct):
        return ('%.2f' % pct) if pct > limit else ''
    return inner_autopct

ax.pie(df[col], labels=df.index, autopct=autopct_generator(20), colors=colors)
对于标签,我能想到的最好办法是使用列表理解:

for ax, col in zip(axes.flat, df.columns):                                                             
    data = df[col]                                                                                     
    labels = [n if v > data.sum() * 0.2 else ''
              for n, v in zip(df.index, data)]                       

    ax.pie(data, autopct=my_autopct, colors=colors, labels=labels)
def my_autopct(pct):
    return ('%1.1f' % pct) if pct > 1 else ''

def get_new_labels(sizes, labels):
    new_labels = [label if size > 1 else '' for size, label in zip(sizes, labels)]
    return new_labels

fig, ax = plt.subplots()
_,_,_ = ax.pie(sizes, labels=get_new_labels(sizes, labels), colors=colors, autopct=my_autopct, startangle=90, rotatelabels=False)
但是请注意,默认情况下,图例是从第一个传递的标签生成的,因此需要显式传递所有值以保持其完整性

axes[0, 0].legend(df.index, bbox_to_anchor=(0, 0.5))

对于我使用过的标签:

def my_level_list(data):
   list = []
   for i in range(len(data)):
       if (data[i]*100/np.sum(data)) > 2 : #2%
           list.append('Label '+str(i+1))
       else:
           list.append('')
return list


patches, texts, autotexts = plt.pie(data, radius = 1, labels=my_level_list(data), autopct=my_autopct, shadow=True)

您可以使用列表理解功能将标签功能缩短一点:

for ax, col in zip(axes.flat, df.columns):                                                             
    data = df[col]                                                                                     
    labels = [n if v > data.sum() * 0.2 else ''
              for n, v in zip(df.index, data)]                       

    ax.pie(data, autopct=my_autopct, colors=colors, labels=labels)
def my_autopct(pct):
    return ('%1.1f' % pct) if pct > 1 else ''

def get_new_labels(sizes, labels):
    new_labels = [label if size > 1 else '' for size, label in zip(sizes, labels)]
    return new_labels

fig, ax = plt.subplots()
_,_,_ = ax.pie(sizes, labels=get_new_labels(sizes, labels), colors=colors, autopct=my_autopct, startangle=90, rotatelabels=False)

谢谢但是我还想删除相应的
a、c、d
标签。@别担心,我对
pandas
了解不多,但这是我能想到的最好的了。非常感谢。有没有办法让'my_autoct(pct)`接受限制参数,例如
mylim=20
?我发现你的方法会影响图例。我们想保持传奇完整。我该怎么做?@neversaint,我也注意到了传说的问题,答案已经改进了。