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

时间:2022-12-03 17:05:25

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

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

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

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.pie but 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')

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

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

1 个解决方案

#1


You can turn off the labels in the chart, and then define them within the call to 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 ...

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

#1


You can turn off the labels in the chart, and then define them within the call to 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 ...

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