【问题标题】:How to create a heat map matrix and generate regions based 'heat' in Python?如何在 Python 中创建热图矩阵并生成基于“热”的区域?
【发布时间】:2016-04-27 00:37:34
【问题描述】:

给定一组点(x, y, 'heat'),

In [15]: df.head()
Out[15]: 
          x         y      heat
0  0.660055  0.395942  2.368304
1  0.126268  0.187978  6.760261
2  0.174857  0.637188  1.025078
3  0.460085  0.759171  2.635334
4  0.689242  0.173868  4.845778

如何生成热图矩阵并划定热区(hard)?

这样,给定一个点,就有可能得到同一区域内的所有点。

PS: 从Generate a heatmap in MatPlotLib using a scatter data set,我知道如何生成区域图,但不知道如何生成区域“矩阵”(因此,给定一个属性,它会说明它在哪个区域)。

【问题讨论】:

    标签: python matplotlib cluster-analysis heatmap


    【解决方案1】:

    我想这取决于您如何制作热图,但假设您使用了您链接的帖子中的第一个示例:

    import numpy as np
    import numpy.random
    import matplotlib.pyplot as plt
    
    # Generate some test data
    x = np.random.randn(8873)
    y = np.random.randn(8873)
    
    heatmap, xedges, yedges = np.histogram2d(x, y, bins=50)
    extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
    
    plt.clf()
    plt.imshow(heatmap, extent=extent)
    plt.show()
    

    所以你现在如果你有一个关于坐标(a,b)的点的请求,你需要在xedges(我们称之为a_heatmap)中找到最接近a的值的位置,在yedges (b_heatmap) 中与b 最接近的值,然后通过以下方式查找返回值:

    heatmap[a_heatmap, b_heatmap]
    

    【讨论】:

    • 这几乎是我正在寻找的......但是让我们假设测试点“(a,b)”所在的区域是区域[5,5]......但是该区域可能由 [5,5],[5,6],[5,7],[6,5],[6.7] ......所有这些都具有几乎相同的平均“热量”。如何自动“合并”区域以获得“更广泛”的区域?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-10
    • 2016-12-08
    • 2020-08-28
    • 1970-01-01
    • 2020-06-27
    • 2014-05-27
    相关资源
    最近更新 更多