我想創建一個簡單的 2D 正方形圖形,其中包含基于 RGB 值的 2 個顏色漸變,例如:
1) the bottom left corner of the square (0, 0) is green [0, 255, 0]
2) the bottom right corner of the square (255, 0) is red [255, 0, 0]
3) the top left corner of the square (0, 255) is blue [0, 0, 255]
4) the top right corner of the square (255, 255) is purple [255, 0, 255]
我認為應該有一種快速簡便的方法來使用 numpy 和 matplotlib 來做到這一點,但我沒有看到它。
uj5u.com熱心網友回復:
在 numpy 中,我們可以np.linspace()在 2D 中使用來計算紅色、綠色和藍色通道:
import matplotlib.pyplot as plt
import numpy as np
def arr_creat(upperleft, upperright, lowerleft, lowerright):
arr = np.linspace(np.linspace(lowerleft, lowerright, arrwidth),
np.linspace(upperleft, upperright, arrwidth), arrheight, dtype=int)
return arr[:, :, None]
arrwidth = 256
arrheight = 256
r = arr_creat(0, 255, 0, 255)
g = arr_creat(0, 0, 255, 0)
b = arr_creat(255, 255, 0, 0)
img = np.concatenate([r, g, b], axis=2)
plt.imshow(img, origin="lower")
plt.axis("off")
plt.show()
輸出:

uj5u.com熱心網友回復:
在沒有 numpy 的情況下弄清楚了,這就是我想要的(以防有人感興趣):
from __future__ import division
import matplotlib.pyplot as plt
xlist = []
ylist = []
colorlist = []
for i in range(0, 256):
for j in range(0, 256):
xlist.append(i)
ylist.append(j)
if i > j:
colorlist.append(((float(i/255), float((255-i)/255), float(j/255))))
else:
colorlist.append(((float(i/255), float((255-j)/255), float(j/255))))
fig = plt.scatter(xlist, ylist, c=colorlist, edgecolor='none', marker='s')
plt.axis('off')
plt.show()

uj5u.com熱心網友回復:
將 numpy 匯入為 np
從 numpy.random 匯入隨機
將 matplotlib.pyplot 匯入為 plt
T = 隨機((3,2))
列印(T.shape)
plt.imshow(T)
輸出:
[回傳隨機 RGB 顏色]
https://i.stack.imgur.com/MRXy4.png
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/438509.html
標籤:Python 麻木的 matplotlib
