pandas 熊猫饼图图删除楔形上的标签文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30059862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:39:20  来源:igfitidea点击:

pandas pie chart plot remove the label text on the wedge

pandasmatplotliblegendpie-chart

提问by fast tooth

the pie chart example on pandas plotting tutorial http://pandas.pydata.org/pandas-docs/version/0.15.0/visualization.htmlgenerates the following figure:

pandas 绘图教程http://pandas.pydata.org/pandas-docs/version/0.15.0/visualization.html上的饼图示例生成下图:

enter image description here

在此处输入图片说明

with this code:

使用此代码:

import matplotlib.pyplot as plt
plt.style.use('ggplot')
import numpy as np
np.random.seed(123456)


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

f, axes = plt.subplots(1,2, figsize=(10,5))
for ax, col in zip(axes, df.columns):
    df[col].plot(kind='pie', autopct='%.2f', labels=df.index,  ax=ax, title=col, fontsize=10)
    ax.legend(loc=3)

plt.show()

I want to remove the text label (a,b,c,d) from both subplots, because for my application those label are long, so I only want to show them in legend.

我想从两个子图中删除文本标签 (a,b,c,d),因为对于我的应用程序,这些标签很长,所以我只想在图例中显示它们。

After read this: How to add a legend to matplotlib pie chart?, I figure out an way with matplotlib.pyplot.piebut the figure is not as fancy even if i am still using ggplot.

阅读本文后:如何向 matplotlib 饼图添加图例?,我想出了一个方法,matplotlib.pyplot.pie但即使我仍在使用 ggplot,这个数字也不那么花哨。

f, axes = plt.subplots(1,2, figsize=(10,5))
for ax, col in zip(axes, df.columns):
    patches, text, _ = ax.pie(df[col].values, autopct='%.2f')
    ax.legend(patches, labels=df.index, loc='best')

enter image description here

在此处输入图片说明

My question is, is there a way that can combine the things I want from both side? to be clear, I want the fanciness from pandas, but remove the text from the wedges.

我的问题是,有没有一种方法可以将我想要的东西从双方结合起来?明确地说,我想要熊猫的幻想,但从楔子中删除文本。

Thank you

谢谢

回答by iayork

You can turn off the labels in the chart, and then define them within the call to legend:

您可以关闭图表中的标签,然后在调用中定义它们legend

df[col].plot(kind='pie', autopct='%.2f', labels=['','','',''],  ax=ax, title=col, fontsize=10)
ax.legend(loc=3, labels=df.index)

or

或者

... labels=None ...

enter image description here

在此处输入图片说明

回答by Hedscan

Using pandas you can still use the matplotlib.pyplot.pie keyword labeldistanceto remove the wedge labels.
eg. df.plot.pie(subplots=True, labeldistance=None, legend=True)

使用熊猫,您仍然可以使用 matplotlib.pyplot.pie 关键字labeldistance来删除楔形标签。
例如。df.plot.pie(子图=真,标签距离=无,图例=真)

From the docs:
labeldistance: floator None, optional, default: 1.1
The radial distance at which the pie labels are drawn. If set to None, label are not drawn, but are stored for use in legend()

文档
labeldistancefloatNone,可选的,默认:1.1
在其中馅饼标签画出的径向距离。如果设置为None,则不绘制标签,而是存储以供在legend()

In context:

在上下文中:

import matplotlib.pyplot as plt
plt.style.use('ggplot')
import numpy as np
np.random.seed(123456)


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

df.plot.pie(subplots=True, figsize=(10,5), autopct='%.2f', fontsize=10, labeldistance=None);

plt.show()

Output:

输出: