【问题标题】:AttributeError: 'tuple' object has no attribute 'sum'AttributeError: \'tuple\' 对象没有属性 \'sum\'
【发布时间】:2023-01-26 16:49:36
【问题描述】:

我正在尝试使用 matplotlib 中的饼图来可视化一些数据。

但是我遇到了这种类型的错误并且无法修复。

我尝试了以下代码:

import matplotlib.pyplot as plt
import numpy

labels = 'Updated', 'Coverage (overall)', 'last 24 hours', 'Deleted', 'Deleted last 24 hours', 'Banned', 'Banned last 24 hours'
sizes = (6, 5.04, 0, 12, 0, 7, 7)
colors = ['yellowgreen', 'gold', 'lightskyblue', 'green', 'black', 'red', 'grey']

def absolute_value(val):
    a  = numpy.round(val/100.*sizes.sum(), 0)
    return a

plt.pie(sizes, labels=labels, colors=colors,
        autopct=absolute_value)

plt.axis('equal')
plt.show()

现在输出:


AttributeError                            Traceback (most recent call last)
Cell In[46], line 12
      9     a  = numpy.round(val/100.*sizes.sum(), 0)
     10     return a
---> 12 plt.pie(sizes, labels=labels, colors=colors,
     13         autopct=absolute_value)
     15 plt.axis('equal')
     16 plt.show()

File ~\AppData\Roaming\Python\Python39\site-packages\matplotlib\pyplot.py:2715, in pie(x, explode, labels, colors, autopct, pctdistance, shadow, labeldistance, startangle, radius, counterclock, wedgeprops, textprops, center, frame, rotatelabels, normalize, data)
   2708 @_copy_docstring_and_deprecators(Axes.pie)
   2709 def pie(
   2710         x, explode=None, labels=None, colors=None, autopct=None,
   (...)
   2713         textprops=None, center=(0, 0), frame=False,
   2714         rotatelabels=False, *, normalize=True, data=None):
-> 2715     return gca().pie(
   2716         x, explode=explode, labels=labels, colors=colors,
   2717         autopct=autopct, pctdistance=pctdistance, shadow=shadow,
   2718         labeldistance=labeldistance, startangle=startangle,
   2719         radius=radius, counterclock=counterclock,
   2720         wedgeprops=wedgeprops, textprops=textprops, center=center,
   2721         frame=frame, rotatelabels=rotatelabels, normalize=normalize,
   2722         **({"data": data} if data is not None else {}))

File ~\AppData\Roaming\Python\Python39\site-packages\matplotlib\__init__.py:1423, in _preprocess_data.<locals>.inner(ax, data, *args, **kwargs)
   1420 @functools.wraps(func)
   1421 def inner(ax, *args, data=None, **kwargs):
   1422     if data is None:
-> 1423         return func(ax, *map(sanitize_sequence, args), **kwargs)
   1425     bound = new_sig.bind(ax, *args, **kwargs)
   1426     auto_label = (bound.arguments.get(label_namer)
   1427                   or bound.kwargs.get(label_namer))

File ~\AppData\Roaming\Python\Python39\site-packages\matplotlib\axes\_axes.py:3236, in Axes.pie(self, x, explode, labels, colors, autopct, pctdistance, shadow, labeldistance, startangle, radius, counterclock, wedgeprops, textprops, center, frame, rotatelabels, normalize)
   3234     s = autopct % (100. * frac)
   3235 elif callable(autopct):
-> 3236     s = autopct(100. * frac)
   3237 else:
   3238     raise TypeError(
   3239         'autopct must be callable or a format string')

Cell In[46], line 9, in absolute_value(val)
      8 def absolute_value(val):
----> 9     a  = numpy.round(val/100.*sizes.sum(), 0)
     10     return a

AttributeError: 'tuple' object has no attribute 'sum'

我将整个 tracecall 粘贴回去,希望能够完美地确定错误。

我该如何解决?

【问题讨论】:

  • sizes 是一个元组,它没有 sum() 方法。什么不清楚?你想用这个100.*sizes.sum()做什么?
  • 试试sum(sizes)
  • 我想显示饼图内的值(在饼图上)和饼图外的标签。 @布兰
  • 成功了!但是 5.04 显示为 5.0。正常数字显示为另一个零 (5 -> 5.0)。我怎么能把他们当作他们的现实来展示呢? @盖伊

标签: python python-3.x pandas dataframe matplotlib


【解决方案1】:

试试这段代码:

import matplotlib.pyplot as plt
import numpy

labels = ['Updated', 'Coverage (overall)', 'last 24 hours', 'Deleted', 'Deleted last 24 hours', 'Banned', 'Banned last 24 hours']
sizes = [6, 5.04, 0, 12, 0, 7, 7]
colors = ['yellowgreen', 'gold', 'lightskyblue', 'green', 'black', 'red', 'grey']

def absolute_value(val):
    a  = numpy.round(val/100*sizes.sum(), 0)
    return a

plt.pie(sizes, labels=labels, colors=colors)

plt.axis('equal')
plt.show()

您应该使用 sizescolors 作为列表

【讨论】:

    猜你喜欢
    • 2021-03-18
    • 1970-01-01
    • 2015-06-22
    • 2020-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    • 1970-01-01
    相关资源
    最近更新 更多