我试着用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粘贴回去,希望能完美地确定错误。
我该怎么补救呢?
1条答案
按热度按时间zz2j4svz1#
验证代码:
您应该使用
sizes
和colors
作为列表