在 ipython 中更改 imshow 的分辨率
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24185083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change resolution of imshow in ipython
提问by Max Beikirch
I am using ipython, with a code that looks like this:
我正在使用 ipython,其代码如下所示:
image = zeros(MAX_X, MAX_Y)
# do something complicated to get the pixel values...
# pixel values are now in [0, 1].
imshow(image)
However, the resulting image always has the same resolution, around (250x250). I thought that the image's dimensions would be (MAX_X x MAX_Y), but that is seemingly not the case. How can I make ipython give me an image with a greater resolution?
但是,生成的图像始终具有相同的分辨率,大约 (250x250)。我认为图像的尺寸是 (MAX_X x MAX_Y),但似乎并非如此。我怎样才能让 ipython 给我一个更高分辨率的图像?
采纳答案by Molly
The height and width of the displayed image on the screen is controlled by the figuresize and the axessize.
figure(figsize = (10,10)) # creates a figure 10 inches by 10 inches
Axes
轴
axes([0,0,0.7,0.6]) # add an axes with the position and size specified by
# [left, bottom, width, height] in normalized units.
Larger arrays of data will be displayed at the same size as smaller arrays but the number of individual elements will be greater so in that sense they do have higher resolution. The resolution in dots per inch of a saved figure can be be controlled with the the dpi argument to savefig.
较大的数据数组将以与较小数组相同的大小显示,但单个元素的数量会更多,因此从这个意义上讲,它们确实具有更高的分辨率。可以使用savefig的 dpi 参数控制保存图形的每英寸点数分辨率。
Here's an example that might make it clearer:
下面是一个可能更清楚的例子:
import matplotlib.pyplot as plt
import numpy as np
fig1 = plt.figure() # create a figure with the default size
im1 = np.random.rand(5,5)
ax1 = fig1.add_subplot(2,2,1)
ax1.imshow(im1, interpolation='none')
ax1.set_title('5 X 5')
im2 = np.random.rand(100,100)
ax2 = fig1.add_subplot(2,2,2)
ax2.imshow(im2, interpolation='none')
ax2.set_title('100 X 100')
fig1.savefig('example.png', dpi = 1000) # change the resolution of the saved image
# change the figure size
fig2 = plt.figure(figsize = (5,5)) # create a 5 x 5 figure
ax3 = fig2.add_subplot(111)
ax3.imshow(im1, interpolation='none')
ax3.set_title('larger figure')
plt.show()
The size of the axes within a figure can be controlled in several ways. I used subplotabove. You can also directly add an axes with axesor with gridspec.
可以通过多种方式控制图形中轴的大小。我在上面使用了子图。您还可以直接添加带有轴或带有gridspec 的轴。
回答by Bzazz
You're probably looking for pcolormesh
rather than imshow
. The former has the purpose of plotting data into space, pixel-by-pixel, rather than showing an image.
您可能正在寻找pcolormesh
而不是imshow
. 前者的目的是将数据逐个像素地绘制到空间中,而不是显示图像。