【问题标题】:heatmap using scatter dataset python matplotlib使用散点数据集 python matplotlib 的热图
【发布时间】:2012-02-04 11:08:39
【问题描述】:

我正在编写一个脚本来为二维S上的散点数据制作热图。以下是我正在尝试做的一个玩具示例:

import numpy as np
from matplotlib.pyplot import*
x = [1,2,3,4,5]
y = [1,2,3,4,5]
heatmap, xedges, yedges = np.histogram2d(x, y, bins=50)
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
imshow(heatmap, extent = extent)

我应该期望“最温暖”的区域沿着 y=x 显示,但它们却沿着 y=-x+5 显示,即热图以相反的方向读取一个列表。我不确定为什么会这样。有什么建议吗?

谢谢

【问题讨论】:

    标签: matplotlib heatmap scatter-plot


    【解决方案1】:

    试试imshow 参数origin=lower。默认情况下,它将数组的 (0,0) 元素设置在左上角。

    例如:

    import numpy as np
    import matplotlib.pyplot as plt
    x = [1,2,3,4,5,5]
    y = [1,2,3,4,5,5]
    heatmap, xedges, yedges = np.histogram2d(x, y, bins=10)
    extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
    fig = plt.figure()
    ax1 = fig.add_subplot(211)
    ax1.imshow(heatmap, extent = extent)
    ax1.set_title("imshow Default");
    ax2 = fig.add_subplot(212)
    ax2.imshow(heatmap, extent = extent,origin='lower')
    ax2.set_title("imshow origin='lower'");
    
    fig.savefig('heatmap.png')
    

    生产:

    【讨论】:

      【解决方案2】:

      要保持热图的外观与您在散点图中看到的一致,实际使用:

      ax2.imshow(heatmap.T, extent = extent,origin='lower')
      

      【讨论】:

        猜你喜欢
        • 2011-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-23
        • 1970-01-01
        • 2018-12-05
        • 2011-05-15
        相关资源
        最近更新 更多