python - 在 matplotlib 中仅绘制一种 rgb 颜色

标签 python matplotlib rgb

我正在运行 matplotlib,我想绘制一种颜色,例如:

import matplotlib.pyplot as plt
plt.imshow([(100, 100, 200)])

但这显示了渐变。什么问题?

最佳答案

这只给出一种颜色:

import matplotlib.pyplot as plt
plt.imshow([[(0, 0, 1)]])

enter image description here

plt.imshow([[(0.5, 0.5, 0.5)]])

enter image description here

你需要一个 MxNx3 的形状:

>>> import numpy as np
>>> np.array([[(0.5, 0.5, 0.5)]]).shape
(1, 1, 3)

这里M和N都是1。

plt.imshow(X, ...)

X : array_like, shape (n, m) or (n, m, 3) or (n, m, 4) Display the image in X to current axes. X may be a float array, a uint8 array or a PIL image. If X is an array, it can have the following shapes:

  • MxN -- luminance (grayscale, float array only)
  • MxNx3 -- RGB (float or uint8 array)
  • MxNx4 -- RGBA (float or uint8 array)

    The value for each component of MxNx3 and MxNx4 float arrays should be in the range 0.0 to 1.0; MxN float arrays may be normalized.

您需要将 01 之间的 int 转换为 float:

from __future__ import division  # needed for Python 2 only

plt.imshow([[(100 / 255, 100 / 255, 200 / 255)]])

enter image description here

关于python - 在 matplotlib 中仅绘制一种 rgb 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41643189/

相关文章:

python - 计算集合中的交集

python - 绘制多面体的 3D 曲面

c++ - 将 RGB 图像转换为灰度时,我的输出是黑色图像

python - 如何轻松地在列表中找到具有给定类属性的类实例?

python - sys.stdout.write 对于 Windows 上的二进制文件无法正常工作

python - 为什么该图的底部有额外的空间?

c# - 从 BitMap 提取原始 RGB 数据并将其写入文件的最简单方法是什么?

c++ - 从 bmp 文件中读取 RGB 像素

python - 如何在时域计算基音基频f(0))?

python - Pandas 饼图子图标签与切片标签重叠