基于 matplotlib 中的 RGB 值创建二维颜色渐变
Creating a 2D color gradient based on RGB values in matplotlib
我想创建一个简单的二维方形图形,其中包含基于 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 来执行此操作,但我没有看到它。
将 numpy 导入为 np
从numpy.random导入随机
将 matplotlib.pyplot 导入为 plt
T = 随机((3,2))
打印(T.shape)
plt.imshow(T)
输出:
[Returns 随机 RGB 颜色]
https://i.stack.imgur.com/MRXy4.png
不用 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()
在numpy中,我们可以在2D中使用np.linspace()
来计算红绿蓝通道:
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()
输出:
我想创建一个简单的二维方形图形,其中包含基于 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 来执行此操作,但我没有看到它。
将 numpy 导入为 np
从numpy.random导入随机
将 matplotlib.pyplot 导入为 plt
T = 随机((3,2))
打印(T.shape)
plt.imshow(T)
输出:
[Returns 随机 RGB 颜色]
https://i.stack.imgur.com/MRXy4.png
不用 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()
在numpy中,我们可以在2D中使用np.linspace()
来计算红绿蓝通道:
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()
输出: