【问题标题】:matplotlib error : loop of ufunc does not support argument 0 of type float which has no callable rint methodmatplotlib 错误:ufunc 循环不支持浮点类型的参数 0,它没有可调用的 rint 方法
【发布时间】: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',只是想看看是否有任何变化,但它是同样的错误。 任何见解都会有所帮助 谢谢!

【问题讨论】:

标签: python-3.x pandas numpy matplotlib plot


【解决方案1】:

你想要这个:

_, ax = plt.subplots(1,2)
df.plot.pie(ax = ax[1], y = 'count')
plt.show()

错误是您使用y=df['count'] 而不是简单的y='count'。您正在使用熊猫绘图,无需发送列值,只需发送列名。此外,您不需要将 dtype 转换为 int,除非您想这样做。

输出:

【讨论】:

  • 感谢您的回复。它确实可以将y='count' 用于饼图,但是当我为相同数据帧的简单线图执行此操作时,它再次给出了相同的错误。我不明白为什么。这次我尝试了有/没有转换为 int 以及有/没有y='count'。您可以在更新下检查有关更新代码的问题。谢谢!
  • @Eyshika 欢迎您。您的更新将是一个不同的问题。对于初学者,您尚未在代码中定义所需的绘图类型,x 是未知的。此外,您的示例数据框没有 'Average No. of tracks ' 列。我建议查看文档pandas.pydata.org/docs/reference/api/pandas.DataFrame.plot.html,了解如何在 pandas 中使用绘图。您还可以提供一个最小示例并指定要绘制的内容,以便我们提供更好的帮助。如果答案解决了您提出的问题,那么始终接受答案可以很好地结束问题。谢谢。
  • 'Average Number of tracks' 是标签,即用于 y 轴上的 ylabel 的名称,而不是提到的数据 y 。同样的代码也适用于正在使用的其他数据帧,看起来完全相同,只是数字不同,但不适用于这个。此外,如果我们不提及kind,它会将其视为线图。正如我所说,它确实使用相同格式的其他数据帧的相同代码绘制线图,但不是这个。我不知道为什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-14
  • 1970-01-01
  • 2021-12-13
  • 1970-01-01
  • 1970-01-01
  • 2020-12-09
相关资源
最近更新 更多