【问题标题】:Python matplotlib.pyplot pie charts: How to remove the label on the left side?Python matplotlib.pyplot饼图:如何去掉左边的标签?
【发布时间】:2016-03-09 18:07:27
【问题描述】:

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

结果:

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

plt.axes().set_xlabel('')

plt.axes().set_ylabel('')

但这没有用。

【问题讨论】:

  • 我现在看到 Pandas 默认执行此操作,请参阅 documentation/examples。但我不知道如何抑制...
  • 只是为了记录,“左侧的标签”是系列名称。从代码中可能很明显,但我想留下这个评论可能有助于谷歌帮助我们所有人和其他人在未来找到这个答案。

标签: python pandas matplotlib charts pie-chart


【解决方案1】:

使用绘图功能时添加 label="" 参数

groups.plot(kind='pie', shadow=True,label="")

【讨论】:

    【解决方案2】:

    或者:

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

    【讨论】:

      【解决方案3】:

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

      pylab.ylabel('')
      

      pylab.axes().set_ylabel('')
      

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

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

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

      【讨论】:

        猜你喜欢
        • 2016-09-23
        • 2012-01-24
        • 1970-01-01
        • 2017-12-01
        • 1970-01-01
        • 2019-02-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多