使用 matplotlib imshow 将 NxMx3 ndarray 绘制为图像
Draw NxMx3 ndarray as image using matplotlib imshow
我的代码旨在使用 matplotlib.imshow():
以 RGB 形式绘制 HSL W x H 图像
import pylab as plt
import numpy as np
import colorsys
W = 512
H = 256
hsls = np.ndarray(shape=(H, W, 3), dtype=float)
hsls[:] = .0
baseCol = np.linspace(0, 1, H)[np.newaxis, :].transpose()
print baseCol.shape, hsls.shape # Gives: (256, 1) (256, 512, 3)
hsls[:, :, 0] = np.tile(baseCol, (1, W))
hsls[:, :, 1:3] = 0.5 # Use constant saturation and lightness.
vfunc = np.vectorize(colorsys.hsv_to_rgb)
rgbs = np.array(vfunc(hsls[:, :, 0], hsls[:, :, 1], hsls[:, :, 2])).transpose()
im = plt.imshow(rgbs, interpolation='nearest', aspect='auto')
plt.colorbar(im, orientation='horizontal')
plt.show()
结果:
这看起来几乎是正确的,只是它被转置了。我希望它看起来逆时针旋转。但是当我 transpose/reshape 这样的时候:
rgbs = rgbs.reshape((H, W, 3))
我得到了一个水平级联的图像:
我也玩过 reshape() 的顺序参数,但是 'F' 给了我第一个结果,而 'C' 给了我第二个。
如何实现我想要的逆时针旋转?
我自己想出来了。我可以首先创建正确的形状。
import pylab as plt
import numpy as np
import colorsys
W = 512
H = 256
hsls = np.ndarray(shape=(W, H, 3), dtype=float)
hsls[:] = .0
baseCol = np.linspace(0, 1, H)[np.newaxis, :]
print baseCol.shape, hsls.shape # Gives: (256, 1) (256, 512, 3)
hsls[:, :, 0] = np.tile(baseCol, (W, 1))
hsls[:, :, 1:3] = 0.5 # Use constant saturation and lightness.
vfunc = np.vectorize(colorsys.hsv_to_rgb)
rgbs = np.flipud(np.array(vfunc(hsls[:, :, 0], hsls[:, :, 1], hsls[:, :, 2])).transpose())
im = plt.imshow(rgbs, interpolation='nearest', aspect='auto')
plt.colorbar(im, orientation='horizontal')
plt.show()
结果:
形状为 (W, H)
的 NumPy 数组有 W
行和 H
列。如果您想要 H
行和 W
列,则数组的形状应为 (H, W)
.
所以使用
hsls = np.zeros((H, W, 3), dtype=float)
我认为如果
您从具有正确形状的数组 hsls
开始:
import matplotlib.pyplot as plt
import numpy as np
import colorsys
W = 512
H = 256
hsls = np.zeros((H, W, 3), dtype=float)
baseCol = np.linspace(0, 1, H)
print baseCol.shape, hsls.shape
# (256,) (256, 512, 3)
hsls[:, :, 0] = baseCol[:, np.newaxis]
hsls[:, :, 1:3] = 0.5 # Use constant saturation and lightness.
vfunc = np.vectorize(colorsys.hsv_to_rgb)
rgbs = np.dstack(vfunc(hsls[:, :, 0], hsls[:, :, 1], hsls[:, :, 2]))
im = plt.imshow(rgbs, interpolation='nearest', aspect='auto')
plt.colorbar(im, orientation='horizontal')
plt.show()
产量
请注意,此结果是您发布的结果的垂直镜像。但
因为这张图片没有翻转或转置,我想也许它可能
实际上是你想要的结果。 (您看到的图像与
如果您要打印 hsls
,您会看到这些数字。左上角
对应于 hsls[0,0]
,而在您的图像中 hsls[0,0]
对应于
不管刻度线怎么说,左下角。您可以通过将 hsls[:10,:10,:] = 0
放在 rgbs
的定义之前来测试此断言。你会在 hsls[0,0]
所在的角落看到一个黑色的小方块。)
要生成垂直镜像,调用ax.invert_yaxis()
。
import matplotlib.pyplot as plt
import numpy as np
import colorsys
W = 512
H = 256
hsls = np.zeros((H, W, 3), dtype=float)
baseCol = np.linspace(0, 1, H)
hsls[:, :, 0] = baseCol[:, np.newaxis]
hsls[:, :, 1:3] = 0.5
vfunc = np.vectorize(colorsys.hsv_to_rgb)
rgbs = np.dstack(vfunc(hsls[:, :, 0], hsls[:, :, 1], hsls[:, :, 2]))
fig, ax = plt.subplots()
im = ax.imshow(rgbs, interpolation='nearest', aspect='auto')
plt.colorbar(im, orientation='horizontal')
ax.invert_yaxis()
plt.show()
产量
请注意,这也会反转刻度标签,显示 0 从左下角开始。
我的代码旨在使用 matplotlib.imshow():
以 RGB 形式绘制 HSL W x H 图像import pylab as plt
import numpy as np
import colorsys
W = 512
H = 256
hsls = np.ndarray(shape=(H, W, 3), dtype=float)
hsls[:] = .0
baseCol = np.linspace(0, 1, H)[np.newaxis, :].transpose()
print baseCol.shape, hsls.shape # Gives: (256, 1) (256, 512, 3)
hsls[:, :, 0] = np.tile(baseCol, (1, W))
hsls[:, :, 1:3] = 0.5 # Use constant saturation and lightness.
vfunc = np.vectorize(colorsys.hsv_to_rgb)
rgbs = np.array(vfunc(hsls[:, :, 0], hsls[:, :, 1], hsls[:, :, 2])).transpose()
im = plt.imshow(rgbs, interpolation='nearest', aspect='auto')
plt.colorbar(im, orientation='horizontal')
plt.show()
结果:
这看起来几乎是正确的,只是它被转置了。我希望它看起来逆时针旋转。但是当我 transpose/reshape 这样的时候:
rgbs = rgbs.reshape((H, W, 3))
我得到了一个水平级联的图像:
我也玩过 reshape() 的顺序参数,但是 'F' 给了我第一个结果,而 'C' 给了我第二个。
如何实现我想要的逆时针旋转?
我自己想出来了。我可以首先创建正确的形状。
import pylab as plt
import numpy as np
import colorsys
W = 512
H = 256
hsls = np.ndarray(shape=(W, H, 3), dtype=float)
hsls[:] = .0
baseCol = np.linspace(0, 1, H)[np.newaxis, :]
print baseCol.shape, hsls.shape # Gives: (256, 1) (256, 512, 3)
hsls[:, :, 0] = np.tile(baseCol, (W, 1))
hsls[:, :, 1:3] = 0.5 # Use constant saturation and lightness.
vfunc = np.vectorize(colorsys.hsv_to_rgb)
rgbs = np.flipud(np.array(vfunc(hsls[:, :, 0], hsls[:, :, 1], hsls[:, :, 2])).transpose())
im = plt.imshow(rgbs, interpolation='nearest', aspect='auto')
plt.colorbar(im, orientation='horizontal')
plt.show()
结果:
形状为 (W, H)
的 NumPy 数组有 W
行和 H
列。如果您想要 H
行和 W
列,则数组的形状应为 (H, W)
.
所以使用
hsls = np.zeros((H, W, 3), dtype=float)
我认为如果
您从具有正确形状的数组 hsls
开始:
import matplotlib.pyplot as plt
import numpy as np
import colorsys
W = 512
H = 256
hsls = np.zeros((H, W, 3), dtype=float)
baseCol = np.linspace(0, 1, H)
print baseCol.shape, hsls.shape
# (256,) (256, 512, 3)
hsls[:, :, 0] = baseCol[:, np.newaxis]
hsls[:, :, 1:3] = 0.5 # Use constant saturation and lightness.
vfunc = np.vectorize(colorsys.hsv_to_rgb)
rgbs = np.dstack(vfunc(hsls[:, :, 0], hsls[:, :, 1], hsls[:, :, 2]))
im = plt.imshow(rgbs, interpolation='nearest', aspect='auto')
plt.colorbar(im, orientation='horizontal')
plt.show()
产量
请注意,此结果是您发布的结果的垂直镜像。但
因为这张图片没有翻转或转置,我想也许它可能
实际上是你想要的结果。 (您看到的图像与
如果您要打印 hsls
,您会看到这些数字。左上角
对应于 hsls[0,0]
,而在您的图像中 hsls[0,0]
对应于
不管刻度线怎么说,左下角。您可以通过将 hsls[:10,:10,:] = 0
放在 rgbs
的定义之前来测试此断言。你会在 hsls[0,0]
所在的角落看到一个黑色的小方块。)
要生成垂直镜像,调用ax.invert_yaxis()
。
import matplotlib.pyplot as plt
import numpy as np
import colorsys
W = 512
H = 256
hsls = np.zeros((H, W, 3), dtype=float)
baseCol = np.linspace(0, 1, H)
hsls[:, :, 0] = baseCol[:, np.newaxis]
hsls[:, :, 1:3] = 0.5
vfunc = np.vectorize(colorsys.hsv_to_rgb)
rgbs = np.dstack(vfunc(hsls[:, :, 0], hsls[:, :, 1], hsls[:, :, 2]))
fig, ax = plt.subplots()
im = ax.imshow(rgbs, interpolation='nearest', aspect='auto')
plt.colorbar(im, orientation='horizontal')
ax.invert_yaxis()
plt.show()
产量
请注意,这也会反转刻度标签,显示 0 从左下角开始。