gpt4 book ai didi

python - 使用 matplotlib 绘制 RGB 值

转载 作者:行者123 更新时间:2023-12-02 00:08:52 38 4
gpt4 key购买 nike

enter image description here

我在数组 rgb_array 中有一组 RGB 值

[255.000, 56,026, 0.000]
[246.100, 60,000, 0.000]
...

>>> print(rbg_array)
1000, 3

我想绘制类似于上面显示的颜色渐变。

我怎样才能最好地使用 matpotlib 的 imshow 来实现这一点?

最佳答案

假设您的数组有 N 行,每行包含 3 个介于 0 和 255 之间的 float ,您可以按如下方式创建图像。首先将其转换为 numpy 整数数组,并将其 reshape 为 (1, N, 3)。这将使它成为一个 1xN 图像。然后,使用 imshow 显示图像。您需要设置一个范围以获取示例中的 x 和 y 轴,或者将它们设置为 [0、1、0、1]。还需要控制纵横比,否则像素将被视为“正方形”。

import numpy as np
import matplotlib.pyplot as plt

rgb_array = [[255.000, 56.026 + (255 - 56.026) * i / 400, 255 * i / 400] for i in range(400)]
rgb_array += [[255 - 255 * i / 600, 255 - 255 * i / 600, 255] for i in range(600)]
img = np.array(rgb_array, dtype=int).reshape((1, len(rgb_array), 3))
plt.imshow(img, extent=[0, 16000, 0, 1], aspect='auto')
plt.show()

example image

关于python - 使用 matplotlib 绘制 RGB 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59702884/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com