【问题标题】:Change resolution of imshow in ipython在 ipython 中更改 imshow 的分辨率
【发布时间】:2014-08-02 19:38:51
【问题描述】:

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

image = zeros(MAX_X, MAX_Y)

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

imshow(image)

但是,生成的图像始终具有相同的分辨率,大约为 (250x250)。我认为图像的尺寸是(MAX_X x MAX_Y),但似乎并非如此。如何让 ipython 给我一个分辨率更高的图像?

【问题讨论】:

    标签: python matplotlib ipython ipython-notebook


    【解决方案1】:

    您可能正在寻找pcolormesh 而不是imshow。前者的目的是将数据逐个像素地绘制到空间中,而不是显示图像。

    【讨论】:

    【解决方案2】:

    屏幕上显示图像的高度和宽度由figure 大小和axes 大小控制。

    figure(figsize = (10,10)) # creates a figure 10 inches by 10 inches
    

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

    较大的数据数组将以与较小的数组相同的大小显示,但单个元素的数量会更多,因此从这个意义上说,它们确实具有更高的分辨率。可以使用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
    

    # 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()
    

    可以通过多种方式控制图形中轴的大小。我在上面使用了subplot。您也可以使用axesgridspec 直接添加轴。

    【讨论】:

    • 在第一个示例中,您并排显示 2 个图像;你如何改变图像本身的大小?例如更靠近图像画布的边框
    • 更简单,在没有 subplot() 行的情况下调用 imgshow() 之前只写 plt.figure(figsize = (x_new, y_new)) 似乎就足够了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-29
    • 1970-01-01
    • 1970-01-01
    • 2012-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多