在ipython中更改imshow的分辨率

Max*_*rch 15 python matplotlib ipython ipython-notebook

我正在使用ipython,其代码如下所示:

image = zeros(MAX_X, MAX_Y)

# do something complicated to get the pixel values...
# pixel values are now in [0, 1].

imshow(image)
Run Code Online (Sandbox Code Playgroud)

但是,生成的图像始终具有相同的分辨率,大约为(250x250).我认为图像的尺寸是(MAX_X x MAX_Y),但看起来并非如此.如何让ipython为我提供更高分辨率的图像?

Mol*_*lly 24

屏幕上显示图像的高度和宽度由图形尺寸和尺寸控制.

figure(figsize = (10,10)) # creates a figure 10 inches by 10 inches
Run Code Online (Sandbox Code Playgroud)

axes([0,0,0.7,0.6]) # add an axes with the position and size specified by 
                    # [left, bottom, width, height] in normalized units. 
Run Code Online (Sandbox Code Playgroud)

较大的数据阵列将以与较小阵列相同的大小显示,但单个元素的数量将更大,因此从这个意义上说它们具有更高的分辨率.可以使用savefig的dpi参数控制已保存图形的每英寸点数的分辨率.

这是一个可能使它更清晰的例子:

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
Run Code Online (Sandbox Code Playgroud)

不同大小的阵列的图像

# 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()
Run Code Online (Sandbox Code Playgroud)

更大的figuer

可以通过多种方式控制图中轴的大小.我在上面使用了子图.您也可以直接添加或带有gridspec的.