Python/Matplotlib -> to_rgba: 无效的rgba参数

6

我正在尝试绘制轮廓,并曾经使用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有这个bug(/新特性)?谢谢。

这不是你所期望的吗?colors 接受一系列颜色。在你的情况下,它试图将 1 而不是 (1,0,0) 解释为 RGB 颜色。colors="#ff0000" 看起来可以正常工作。 - cel
1个回答

2

正如评论中指出的那样,plt.contour()需要一系列颜色。如果你想指定一个RGB元组,把它作为这个序列的第一个元素。

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

或者

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接