如何设置显示比例 [英] How to set imshow scale
问题描述
我对matplotlib感到厌烦,因为要绘制指定大小的图像非常困难.
I'm fed up with matplotlib in that it's so hard to plot images in specified size.
我有两张32 * 32、20 * 20尺寸的图像.我只想以原始大小或与原始大小成比例绘制它们.
I've two images in 32*32, 20*20 sizes. I just want to plot them in its original size, or in proportion to its original size.
在SO阅读了很多帖子后,我现在使用的代码是
After reading quite a few posts at SO, the code I'm using now is
plt.autoscale(False)
plt.subplot(1, 2, 1);
plt.imshow(img_blob[0, 0, :, :], cmap='gray',
interpolation='nearest', extent=[0, 32, 0, 32])
plt.subplot(1, 2, 2);
plt.imshow(output_blob[0, 0, :, :], cmap='gray',
interpolation='nearest', extent=[0, 20, 0, 20])
plt.show()
但是两张图片仍然以相同的尺寸显示.
But the two images are still displayed in the same size.
我尝试了figsize
I've tried figsize
plt.subplot(1, 2, 2, figsize=(2.0, 2.0));
但是显然没有figsize属性.
But apparently there's no figsize attribute.
是否可以制作不同大小的两个子图?
Is it possible to make the two subplots in different size?
推荐答案
如果图像比例非常重要,您还可以使用 imshow
的替代方案......有一种叫做 figimage
会在您的图形上显示一个完全未重采样的图像.使用它有点费劲-它在幕后并没有做很多事情,以使您的生活更轻松-但在某些情况下它可能会很有用.这是您的示例
There is also an alternative to imshow
you could use if the image proportions are very important ... there is something called figimage
which displays a completely non-resampled image to your figure. Working with it is somewhat painstaking - it doesn't do much behind the scenes to make your life easier - but it can be useful for certain circumstances. Here is something using your example
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,1,20)
z1 = x[:,np.newaxis] * x
y = np.linspace(0,1,32)
z2 = y[:,np.newaxis] * y
fig = plt.figure(figsize=(2,1))
plt.figimage(z1, 25, 25, cmap='gray') # X, Y offsets are in pixels
plt.figimage(z2, 75, 25, cmap='gray')
plt.show()
这篇关于如何设置显示比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!