Python matplotlib.pyplot饼图:如何删除左侧的标签?

问题描述:

我用pyplot绘制了一个饼图.

I plot a piechart using pyplot.

import pylab
import pandas as pd
test = pd.Series(['male', 'male', 'male', 'male', 'female'], name="Sex")
test = test.astype("category")
groups = test.groupby([test]).agg(len)
groups.plot(kind='pie', shadow=True)
pylab.show()

结果:

但是,我无法删除左侧的标签(图片中标记为红色).我已经尝试过了

However, I'm unable to remove the label on the left (marked red in the picture). I already tried

plt.axes().set_xlabel('')

plt.axes().set_ylabel('')

但是那没用.

您可以通过调用pylab.ylabel来设置ylabel:

You could just set the ylabel by calling pylab.ylabel:

pylab.ylabel('')

pylab.axes().set_ylabel('')

在您的示例中,plt.axes().set_ylabel('')将不起作用,因为您的代码中没有import matplotlib.pyplot as plt,因此plt不存在.

In your example, plt.axes().set_ylabel('') will not work because you dont have import matplotlib.pyplot as plt in your code, so plt doesn't exist.

或者,groups.plot命令返回Axes实例,因此您可以使用它来设置ylabel:

Alternatively, the groups.plot command returns the Axes instance, so you could use that to set the ylabel:

ax=groups.plot(kind='pie', shadow=True)
ax.set_ylabel('')