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
Python matplotlib.pyplot pie charts: How to remove the label on the left side?
提问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:
结果:
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 ylabel
by 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 plt
in your code, so plt
doesn't exist.
在您的示例中,plt.axes().set_ylabel('')
将不起作用,因为您import matplotlib.pyplot as plt
的代码plt
中没有,所以不存在。
Alternatively, the groups.plot
command returns the Axes
instance, so you could use that to set the ylabel
:
或者,该groups.plot
命令返回Axes
实例,因此您可以使用它来设置ylabel
:
ax=groups.plot(kind='pie', shadow=True)
ax.set_ylabel('')