簡體   English   中英

從 RGB colors 列表在 python 中創建顏色 map

[英]Creating a color map in python from a list of RGB colors

我想在 python 中創建類似於此圖像的顏色 map: 在此處輸入圖像描述

但我的 map 只包含 3 行和 4 列。 我想使用 RGB 值為每個正方形分配一個特定的顏色值。 我試過這段代碼

colors=np.array([[0.01, 0.08, 0.01], [0.01, 0.16, 0.01], [0.01, 0.165, 0.01], [0.01, 0.3, 0.01],
                 [0.01, 0.2, 0.01], [0.666, 0.333, 0.01], [0.01, 0.165, 0.01], [0.01, 0.3, 0.01],
                 [0.01, 0.2, 0.01], [0.666, 0.333, 0.01], [0.01, 0.165, 0.01], [0.01, 0.3, 0.01]])


fig, ax=plt.subplots()
ax.imshow(colors)
ax.set_aspect('equal')
plt.show()

但 output 不符合我的預期。 似乎使用這種方法我不能使用 RGB 值來表示正方形的顏色。 任何人都可以幫助我嗎? 謝謝!

您有一個(12,3) colors 陣列,而您需要一個(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.01],
            [0.01, 0.165, 0.01],
        ],
        [
            [0.01, 0.3, 0.01],
            [0.01, 0.2, 0.01],
            [0.666, 0.333, 0.01],
        ],
        [
            [0.01, 0.165, 0.01],
            [0.01, 0.3, 0.01],
            [0.01, 0.2, 0.01],
        ],
        # this is the fourth row
        [
            [0.666, 0.333, 0.01],
            [0.01, 0.165, 0.01],
            [0.01, 0.3, 0.01],
        ],
    ]
)
print(colors.shape)


fig, ax = plt.subplots()
ax.imshow(colors)
ax.set_aspect("equal")
plt.show()

根據需要在行/列中重新排列數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM