【发布时间】:2021-09-29 06:25:41
【问题描述】:
这是我的数据系列: df =
count
17 83396.142857
18 35970.000000
19 54082.428571
20 21759.714286
21 16899.571429
22 19870.571429
23 32491.285714
24 40425.285714
25 30780.285714
26 11923.428571
27 13698.571429
28 28028.000000
29 52575.000000
首先将其转换为 int 以避免任何问题:
df['count'] = df['count'].astype(int)
df.index = df.index.astype(int)
我正在尝试使用以下方法绘制数据:
_, ax = plt.subplots(1,2)
df.plot.pie(ax = ax[1], y = df['count'])
plt.show()
但它一直抛出异常错误:
Type:
TypeError
Message:
loop of ufunc does not support argument 0 of type float which has no callable rint method
Stacktrace:
File "/Users/eyshikaagarwal/.virtualenvs/env-hss-ml/lib/python3.8/site-packages/matplotlib/backends/backend_macosx.py", line 61, in _draw
self.figure.draw(renderer)
File "/Users/eyshikaagarwal/.virtualenvs/env-hss-ml/lib/python3.8/site-packages/matplotlib/artist.py", line 41, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/Users/eyshikaagarwal/.virtualenvs/env-hss-ml/lib/python3.8/site-packages/matplotlib/figure.py", line 1863, in draw
mimage._draw_list_compositing_images(
File "/Users/eyshikaagarwal/.virtualenvs/env-hss-ml/lib/python3.8/site-packages/matplotlib/image.py", line 131, in _draw_list_compositing_images
a.draw(renderer)
File "/Users/eyshikaagarwal/.virtualenvs/env-hss-ml/lib/python3.8/site-packages/matplotlib/artist.py", line 41, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/Users/eyshikaagarwal/.virtualenvs/env-hss-ml/lib/python3.8/site-packages/matplotlib/cbook/deprecation.py", line 411, in wrapper
return func(*inner_args, **inner_kwargs)
File "/Users/eyshikaagarwal/.virtualenvs/env-hss-ml/lib/python3.8/site-packages/matplotlib/axes/_base.py", line 2747, in draw
mimage._draw_list_compositing_images(renderer, self, artists)
File "/Users/eyshikaagarwal/.virtualenvs/env-hss-ml/lib/python3.8/site-packages/matplotlib/image.py", line 131, in _draw_list_compositing_images
a.draw(renderer)
File "/Users/eyshikaagarwal/.virtualenvs/env-hss-ml/lib/python3.8/site-packages/matplotlib/artist.py", line 41, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/Users/eyshikaagarwal/.virtualenvs/env-hss-ml/lib/python3.8/site-packages/matplotlib/axis.py", line 1164, in draw
ticks_to_draw = self._update_ticks()
File "/Users/eyshikaagarwal/.virtualenvs/env-hss-ml/lib/python3.8/site-packages/matplotlib/axis.py", line 1022, in _update_ticks
major_labels = self.major.formatter.format_ticks(major_locs)
File "/Users/eyshikaagarwal/.virtualenvs/env-hss-ml/lib/python3.8/site-packages/matplotlib/ticker.py", line 249, in format_ticks
self.set_locs(values)
File "/Users/eyshikaagarwal/.virtualenvs/env-hss-ml/lib/python3.8/site-packages/matplotlib/ticker.py", line 782, in set_locs
self._set_format()
File "/Users/eyshikaagarwal/.virtualenvs/env-hss-ml/lib/python3.8/site-packages/matplotlib/ticker.py", line 884, in _set_format
if np.abs(locs - np.round(locs, decimals=sigfigs)).max() < thresh:
File "<__array_function__ internals>", line 5, in round_
File "/Users/eyshikaagarwal/.virtualenvs/env-hss-ml/lib/python3.8/site-packages/numpy/core/fromnumeric.py", line 3739, in round_
return around(a, decimals=decimals, out=out)
File "<__array_function__ internals>", line 5, in around
File "/Users/eyshikaagarwal/.virtualenvs/env-hss-ml/lib/python3.8/site-packages/numpy/core/fromnumeric.py", line 3314, in around
return _wrapfunc(a, 'round', decimals=decimals, out=out)
File "/Users/eyshikaagarwal/.virtualenvs/env-hss-ml/lib/python3.8/site-packages/numpy/core/fromnumeric.py", line 66, in _wrapfunc
return _wrapit(obj, method, *args, **kwds)
File "/Users/eyshikaagarwal/.virtualenvs/env-hss-ml/lib/python3.8/site-packages/numpy/core/fromnumeric.py", line 43, in _wrapit
result = getattr(asarray(obj), method)(*args, **kwds)
[0m
任何建议..这里有什么问题? 我已经花了几个小时来理解和修复它,但还没有运气。 任何帮助都会很棒。
更新:
感谢@ehsan 为饼图提供的答案,但是当我使用以下方法进行简单线图时,我仍然遇到同样的错误:
plot_kwargs = {'xticks': df.index,
'grid': True,
'color': 'Red',
'title' : "Average "}
df.plot(ylabel = 'Average No. of tracks ', **plot_kwargs)
我在这段代码中遇到的错误完全相同,我不明白为什么。我什至在这里也使用了y='count',只是想看看是否有任何变化,但它是同样的错误。
任何见解都会有所帮助
谢谢!
【问题讨论】:
-
一个问题,为什么
df.plot.pie(ax = ax[1], y = df['count'])而不是直接df.plot.pie(y='count')?来源:pandas.pydata.org/pandas-docs/stable/reference/api/… -
我正在使用子图,这就是我使用 ax[1] 的原因
标签: python-3.x pandas numpy matplotlib plot