【问题标题】:Python/Matplotlib -> to_rgba: Invalid rgba argPython/Matplotlib -> to_rgba: rgba arg 无效
【发布时间】:2015-03-13 03:05:36
【问题描述】:

我正在尝试绘制轮廓,并且以前使用 RGB 元组来指定颜色(所有轮廓只有一个) - 但是,现在我从 to_rgba 得到一个 ValueError:

ValueError: to_rgba: Invalid rgba arg "1"
to_rgb: Invalid rgb arg "1"
cannot convert argument to rgb sequence

这是一个例子:

import numpy as np
import matplotlib.pyplot as plt
grid = np.random.random((10,10))
contours = np.linspace(0, 1, 10)

现在可以了!

plt.contour(grid, levels = contours, colors = 'r')
plt.show()

但这不起作用!

plt.contour(grid, levels = contours, colors = (1,0,0))
plt.show()

我做错了什么还是 Matplotlib 中的这个错误(/新功能)?谢谢。

【问题讨论】:

  • 这不是您所期望的吗? colors 采用一系列颜色。在您的情况下,它会尝试将 1 而不是 (1,0,0) 解释为 rgb 颜色。 colors="#ff0000" 似乎按预期工作。

标签: python matplotlib colors contour


【解决方案1】:

正如 cmets 中所指出的,plt.contour() 需要一系列颜色。如果要指定 RGB 元组,请将其设为此类序列的第一个元素。

plt.contour(grid, levels = contours, colors = ((1,0,0),) )

plt.contour(grid, levels = contours, colors = [(1,0,0),] )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-01
    • 2015-11-26
    • 2018-08-05
    • 2018-02-16
    • 2018-11-20
    相关资源
    最近更新 更多