我有以下代码显示高斯金字塔的结果 -
fig = plt.figure(1)
plt.figimage(gp[0], cmap=plt.cm.gray)
plt.figimage(gp[1], 512, cmap=plt.cm.gray)
plt.figimage(gp[2], 512 + 256, cmap=plt.cm.gray)
plt.figimage(gp[3], 512 + 256 + 128, cmap=plt.cm.gray)
plt.figimage(gp[4], 512 + 256 + 128 + 64, cmap=plt.cm.gray)
plt.figimage(gp[5], 512 + 256 + + 128 + 64 + 32, cmap=plt.cm.gray)
plt.show()
gp
是np.array
类型的图像列表,是指尺寸为512X512,256X256等的数组。
此代码生成以下图像:
(原始图片来自skimage.data.camera()
)
我的问题是:如何让图片显示在图片的顶部而不是底部?
答案 0 :(得分:1)
plt.figimage
本身的文档提出了一种更好的方法:
figimage补充了轴图像(imshow()),它将被重新采样以适合当前轴。如果您希望重新采样的图像填充整个图形,则可以定义大小为[0,1,0,1]的Axes。
链接实际上存在于在线文档中。另外,我刚刚提交了PR来修复[0, 1, 0, 1]
到[0, 0, 1, 1]
。
from matplotlib import pyplot as plt
fig = plt.figure()
# Create axis stretched across the entire figure. anchor='N' will
# keep it anchored to the top and left when it gets resized to fit
# the image bounds (default is the center).
ax = fig.add_axes([0,0,1,1], anchor='NW', frameon=False)
# Remove the spines and ticks of the axis (frameon=False in the
# line above made it transparent)
ax.set_axis_off()
offset = -0.5
for im in gp:
height, width = im.shape
# Plot the images upside-down with origin=lower
ax.imshow(im, extent=[offset, offset + width, -0.5, height], origin='lower', cmap='gray')
offset += width
ax.set_xlim([-0.5, offset])
# Flip the y-axis
ax.set_ylim([gp[0].shape[0] - 0.5, -0.5])
plt.show()
与figure.figimage
的一个关键区别是,图像会随着您更改图形的大小而缩放,而不是逐像素地映射到屏幕。
我使用gp
的以下定义进行了测试:
from skimage.data import camera
g = camera()
gp = [g[::2**x, ::2**x] for x in range(6)]
结果如下:
以下是anchor
未明确设置fig.add_axes
的示例:
<强>更新强>
PR #7659已被接受,因此matplotlib的更正文档将为:
figimage补充了轴图像(imshow()),它将被重新采样以适合当前轴。如果您希望重新采样的图像填充整个图形,则可以使用范围[0,0,1,1] 定义Axes。