【问题标题】:Resizing imshow heatmap into a given image size in matplotlib在 matplotlib 中将 imshow 热图调整为给定的图像大小
【发布时间】:2019-03-30 17:23:00
【问题描述】:

我正在使用matplotlib 进行绘图,并且我有以下矩阵Mat,我想将其绘制成热图。

Mat.shape

产生(20,20)

我使用以下代码将其绘制成热图,遵循this

plt.imshow(Mat, cmap='Reds', interpolation='nearest')    
plt.show()

但我必须将此热图调整为1600 x 1200(x,y) 大小,因为我希望它与图像img 重叠。代码如下。

plt.imshow(img, alpha=.5) # for image
plt.xlim(0, 1600)
plt.ylim(1200, 0)
plt.axis('off')

plt.imshow(Mat, cmap='Reds', interpolation='nearest', alpha=.5)   # for heatmap to overlap
plt.show()

出于某种原因,我不想更改img 的大小。


我的尝试

我尝试在plt.imshow(Mat) 级别调整大小,但我发现这很难。我目前看到的唯一选择是将Mat 调整为具有冗余的1600 x 1200 矩阵。谁能给我一些有效的解决方案?

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    使用 extent= parameter of imshow 将矩阵缩放到图像的比例 (more information here as well)。

    plt.figure()
    img = plt.imread('stinkbug.png')
    plt.imshow(img, alpha=1) # for image
    #plt.axis('off')
    
    xmin, xmax = plt.xlim()
    ymin, ymax = plt.ylim()
    
    Mat = np.random.normal(size=(20,20))
    plt.imshow(Mat, cmap='Reds', interpolation='nearest', alpha=.5, extent=(xmin,xmax,ymin,ymax))   # for heatmap to overlap
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2020-02-26
      • 2020-01-29
      • 2011-10-24
      • 2013-08-12
      • 2012-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多