【Python】绘图时报错ValueError: Invalid RGBA argument: (0.0, 0.5, 0.0, array([[0.42116073]]))

通常不会报出这种错误,但还是有奇人能搞出来。这种错误通过断点调试就可以发现错误

在利用Python进行绘图时可能报出这种错误。

Traceback (most recent call last):
  File "E:\Anaconda3\envs\python27\Lib\lib-tk\Tkinter.py", line 1542, in __call__
    return self.func(*args)
  File "E:\Anaconda3\envs\python27\lib\site-packages\matplotlib\backends\_backend_tk.py", line 227, in resize
    self.draw()
  File "E:\Anaconda3\envs\python27\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 12, in draw
    super(FigureCanvasTkAgg, self).draw()
  File "E:\Anaconda3\envs\python27\lib\site-packages\matplotlib\backends\backend_agg.py", line 437, in draw
    self.figure.draw(self.renderer)
  File "E:\Anaconda3\envs\python27\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "E:\Anaconda3\envs\python27\lib\site-packages\matplotlib\figure.py", line 1493, in draw
    renderer, self, artists, self.suppressComposite)
  File "E:\Anaconda3\envs\python27\lib\site-packages\matplotlib\image.py", line 141, in _draw_list_compositing_images
    a.draw(renderer)
  File "E:\Anaconda3\envs\python27\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", line 281, in draw
    reverse=True)):
  File "E:\Anaconda3\envs\python27\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", line 280, in <lambda>
    key=lambda col: col.do_3d_projection(renderer),
  File "E:\Anaconda3\envs\python27\lib\site-packages\mpl_toolkits\mplot3d\art3d.py", line 459, in do_3d_projection
    fcs = mcolors.to_rgba_array(fcs, self._alpha)
  File "E:\Anaconda3\envs\python27\lib\site-packages\matplotlib\colors.py", line 270, in to_rgba_array
    result[i] = to_rgba(cc, alpha)
  File "E:\Anaconda3\envs\python27\lib\site-packages\matplotlib\colors.py", line 171, in to_rgba
    rgba = _to_rgba_no_colorcycle(c, alpha)
  File "E:\Anaconda3\envs\python27\lib\site-packages\matplotlib\colors.py", line 222, in _to_rgba_no_colorcycle
    raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
ValueError: Invalid RGBA argument: (0.0, 0.0, 1.0, array([[0.61773639]]))

有Python代码如下:

fig = plt.figure()  # 使用Axes3D()创建3D图形对象
ax = Axes3D(fig)
ax.scatter(datatest_train[:, 0], datatest_train[:, 1], y_t, c='r')

正常来看,这段代码是正确的。但却报出如上错误。
这时就需要利用断点进行调试。
正常情况下,函数scatter中的参数都应该是数值型,但此时可以发现函数scatter中的参数 y_t 中为‘str’型,错误就出现在这里。

在这里插入图片描述

需要将参数转化为数值型才可以。

你可能感兴趣的:(Python,plot,scatter,python)