pandas Python matplotlib.pyplot 饼图:如何去掉左边的标签?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34094596/
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-14 00:19:49  来源:igfitidea点击:

Python matplotlib.pyplot pie charts: How to remove the label on the left side?

pythonpandasmatplotlibchartspie-chart

提问by Marc

I plot a piechart using pyplot.

我使用 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()

The result:

结果:

enter image description here

在此处输入图片说明

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

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

plt.axes().set_xlabel('')

and

plt.axes().set_ylabel('')

but that did not work.

但这没有用。

采纳答案by tmdavison

You could just set the ylabelby calling pylab.ylabel:

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

pylab.ylabel('')

or

或者

pylab.axes().set_ylabel('')

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

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

Alternatively, the groups.plotcommand returns the Axesinstance, so you could use that to set the ylabel:

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

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