首页 > 解决方案 > 使用 plot() 函数时 matplotlib 中的 RGBA 颜色“ro”无效

问题描述

我一直在尝试使用Figure.figure.plot()matplotlib 中的函数

figplot = fig.add_subplot(111)
print(lines[2].get_xdata()[0])
print(lines[2].get_ydata()[0])
figplot.plot(lines[2].get_xdata()[0], lines[2].get_ydata()[0], c='ro')

但是在尝试执行此操作时,我收到以下错误消息:

Traceback (most recent call last):
  File "/usr/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
    return self.func(*args)
  File "/usr/local/lib/python3.7/dist-packages/matplotlib/backends/_backend_tk.py", line 259, in resize
    self.draw()
  File "/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_tkagg.py", line 9, in draw
    super(FigureCanvasTkAgg, self).draw()
  File "/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py", line 388, in draw
    self.figure.draw(self.renderer)
  File "/usr/local/lib/python3.7/dist-packages/matplotlib/artist.py", line 38, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/matplotlib/figure.py", line 1709, in draw
    renderer, self, artists, self.suppressComposite)
  File "/usr/local/lib/python3.7/dist-packages/matplotlib/image.py", line 135, in _draw_list_compositing_images
    a.draw(renderer)
  File "/usr/local/lib/python3.7/dist-packages/matplotlib/artist.py", line 38, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/matplotlib/axes/_base.py", line 2647, in draw
    mimage._draw_list_compositing_images(renderer, self, artists)
  File "/usr/local/lib/python3.7/dist-packages/matplotlib/image.py", line 135, in _draw_list_compositing_images
    a.draw(renderer)
  File "/usr/local/lib/python3.7/dist-packages/matplotlib/artist.py", line 38, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/matplotlib/lines.py", line 783, in draw
    lc_rgba = mcolors.to_rgba(self._color, self._alpha)
  File "/usr/local/lib/python3.7/dist-packages/matplotlib/colors.py", line 177, in to_rgba
    rgba = _to_rgba_no_colorcycle(c, alpha)
  File "/usr/local/lib/python3.7/dist-packages/matplotlib/colors.py", line 233, in _to_rgba_no_colorcycle
    raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
ValueError: Invalid RGBA argument: 'ro'

我注意到对于散点图,颜色必须是一个数组,但这不是散点图。lines[2].get_xdata()[0]和的值 lines[2].get_ydata()[0]如下:

0.5766199490353112
1648.0609161647387

有没有办法找出问题所在?我正在使用 tkinter 和 matplotlib

标签: pythonmatplotlib

解决方案


ro是颜色标记代码。你应该删除c=

figplot.plot(lines[2].get_xdata()[0], lines[2].get_ydata()[0], 'ro')

或者也许可以在没有标记的情况下指定颜色o

figplot.plot(lines[2].get_xdata()[0], lines[2].get_ydata()[0], c='r')

推荐阅读