【问题标题】:Missing labels in matplotlib pie chartmatplotlib 饼图中缺少标签
【发布时间】:2019-03-06 09:46:03
【问题描述】:

我尝试在 Ubuntu 18.10 上使用 Python 3 Matplotlib v2.2.2-4build1 绘制饼图。一切似乎都很好,除了标签——它们不见了。尝试根据官方文档添加(https://matplotlib.org/api/_as_gen/matplotlib.pyplot.pie.html),尝试使用网络上的示例(https://pythonspot.com/matplotlib-pie-chart/) - 结果相同,没有标签。

这是我的代码的简化版本:

import numpy as np
import matplotlib.pyplot as plt
import sys

headers = ['a', 'b', 'c', 'd', 'e']
values = [5, 4, 3, 2, 1]
sum = sum(values)
labels = []
for v in values:
    labels.append('{:.1f}%'.format(100 * v / sum))
fig, ax = plt.subplots(figsize=(6, 3), subplot_kw=dict(aspect="equal"))
wedges, texts = ax.pie(values, labels=labels, textprops=dict(color="w"))
plt.show()

这是我看到的 - 没有标签:

尝试使用元组而不是列表 - 同样的事情。

有人可以帮我吗?

【问题讨论】:

    标签: python matplotlib plot


    【解决方案1】:

    您可能希望在白色背景上将标签的颜色设为非白色 :)

    同时使用 sum 作为变量名会覆盖函数,所以你最好选择其他的。

    import numpy as np
    import matplotlib.pyplot as plt
    import sys
    
    headers = ['a', 'b', 'c', 'd', 'e']
    values = [5, 4, 3, 2, 1]
    sumT = sum(values)
    labels = []
    for v in values:
        labels.append('{:.1f}%'.format(100 * v / sumT))
    fig, ax = plt.subplots(figsize=(6, 3), subplot_kw=dict(aspect="equal"))
    wedges, texts = ax.pie(values, labels=labels, textprops=dict(color="k"))
    plt.show()
    

    或者如果你希望标签在里面:

    import numpy as np
    import matplotlib.pyplot as plt
    import sys
    
    def func(pct, allvals):
        absolute = int(pct/100.*sum(allvals))
        return "{:.1f}%)".format(pct)
    
    headers = ['a', 'b', 'c', 'd', 'e']
    values = [5, 4, 3, 2, 1]
    sumT = sum(values)
    labels = []
    for v in values:
        labels.append('{:.1f}%'.format(100 * v / sumT))
    fig, ax = plt.subplots(figsize=(6, 3), subplot_kw=dict(aspect="equal"))
    wedges, texts = ax.pie(values, autopct=lambda pct: func(pct, 
    values), textprops=dict(color="w"))
    plt.show()
    

    【讨论】:

    • 该死,这么简单 :) 非常感谢。
    【解决方案2】:
    import numpy as np
    import matplotlib.pyplot as plt
    import sys
    
    headers = ['a', 'b', 'c', 'd', 'e']
    values = [5, 4, 3, 2, 1]
    colors=['yellow','blue','red','pink','green']
    
    plt.pie(values,labels=headers,
            colors=colors,autopct='%1.2f%%',
            shadow=True,startangle=90)
    plt.title('pie chart')
    plt.show()
    

    【讨论】:

      【解决方案3】:

      在 plt.show() 之前添加 plt.legend() 语句就可以了。

      import numpy as np
      import matplotlib.pyplot as plt
      import sys
      
      headers = ['a', 'b', 'c', 'd', 'e']
      values = [5, 4, 3, 2, 1]
      labels = []
      for v in values:
          labels.append('{:.1f}%'.format(100 * v / sum))
      fig, ax = plt.subplots(figsize=(6, 3), subplot_kw=dict(aspect="equal"))
      wedges, texts = ax.pie(values, labels=labels, textprops=dict(color="w"))
      plt.legend()
      plt.show()
      

      【讨论】:

      猜你喜欢
      • 2020-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多