如何解决从 RGB 颜色列表在 python 中创建颜色映射
我想在 python 中创建一个类似于此图像的颜色映射:
但我的地图只包含 3 行和 4 列。我想使用 RGB 值为每个方块分配一个特定的颜色值。我试过这个代码
colors=np.array([[0.01,0.08,0.01],[0.01,0.16,0.165,0.3,0.2,[0.666,0.333,0.01]])
fig,ax=plt.subplots()
ax.imshow(colors)
ax.set_aspect('equal')
plt.show()
但输出不符合我的期望。似乎使用这种方法我不能使用 RGB 值来表示正方形的颜色。任何人都可以帮助我吗?谢谢!
解决方法
您有一个 (12,3)
颜色数组,而您需要一个 (4,3,3)
图像,每个像素一种 RGB 颜色。
import numpy as np # type: ignore
import matplotlib.pyplot as plt # type: ignore
colors = np.array(
[
# this is the first row
[
# these are the 3 pixels in the first row
[0.01,0.08,0.01],[0.01,0.16,0.165,],[
[0.01,0.3,0.2,[0.666,0.333,# this is the fourth row
[
[0.666,]
)
print(colors.shape)
fig,ax = plt.subplots()
ax.imshow(colors)
ax.set_aspect("equal")
plt.show()
根据需要在行/列中重新排列数据。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。