【问题标题】:Plotting a heatmap or colormap with interpolation in Python在 Python 中使用插值绘制热图或颜色图
【发布时间】:2020-10-12 19:31:26
【问题描述】:

我需要在 python 中创建一个“热图”或“颜色图”。我有三个python列表,分别是:X_COORDINATE、Z_COORDINATE和C_I。 X_COORDINATE 和 Z_COORDINATE 列表包含我有特定数据点的 x 和 z 坐标(存储在 C_I 列表中)。 C_I 列表包含我需要在相应坐标处绘制到 x-z 网格上的值。

热图/颜色图需要在 C_I 列表中已知和包含的点之间进行插值,以便地图是平滑的,而不是方形块。我不能共享代码或源数据,因为这是敏感的。但是,为了理解如何编写这些映射之一,假设:

X = [1.1, 1.5, 2.0, 3.0, 4.0, 5.0, 1.1, 1.5, 2.0, 3.0, 4.0, 5.0, 1.1, 1.5, 2.0, 3.0, 4.0, 5.0, 0.0, 0.5, 1.0, 1.1, 1.5, 2.0, 3.0, 4.0, 5.0, 0.0, 0.5, 1.0, 1.5, 2.0, 3.0, 4.0]

Z = [0, 0, 0, 0, 0, 0, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1, 1, 1, 1, 1, 1, 2.1, 2.1, 2.1, 2.1, 2.1, 2.1, 2.1, 2.1, 2.1, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5]

C_I = [1.02414267447743, 0.210700871073941, 0.156586042435711, 0.109151033138569, 0.2279728779957, 0.204768257586954, 1.09037445301743, 0.287155868433615, 0.211257395413685, 0.132554129593619, 0.0900680495011601, 0.194608837248807, 1.34397119257655, 0.1201882143371, 0.17555070608144, 0.127220190160657, 0.204384526301353, 0.197414938747342, 0.195583977408476, 0.148150828086297, 0.183751866814816, 0.134858902076203, 0.183027629350907, 0.180267135381046, 0.0876356087026242, 0.183285092770786, 0.165502978081942, 0.0487725567447014, 0.172053559692846, 0.142204671797215, 0.166163224221791, 0.249334486033046, 0.150888488422605, 0.259452257883415]

所以 X_COORDINATE 列表中的第零个元素是第一个数据点的 x 坐标,Z_COORDINATE 列表中的第零个元素是第一个数据点的 z 坐标,C_I 列表中的第零个元素是值必须为第一个点绘制。

我想要 X 轴上的 X_COORDINATE 和 z 轴上的 Z_COORDINATE 地图的内部由相应的 C_I 值着色。为了清楚起见,我希望地图类似于以下附加图像;虽然我希望 X 和 Z 轴刻度存在以及相关的颜色条。我该怎么做?

网格在 x 方向上应为 5 个单位,在 z 方向(y 轴)上应为 2.5 个单位

【问题讨论】:

    标签: python matplotlib interpolation heatmap colormap


    【解决方案1】:

    所以,我找到了解决方案。为了将来的参考,这段代码应该给那些有同样问题的人他们需要的东西。我在plot.imshow() 函数中添加了一个“插值”参数,例如:

    import matplotlib.pyplot as plt
    import numpy as np
    from matplotlib.colors import LogNorm
    from scipy.interpolate import interp2d
    
    x_list = np.array(X_COORDINATE)
    z_list = np.array(Z_COORDINATE)
    C_I_list = np.array(C_I)
    
    #   f will be a function with two arguments (x and z coordinates),
    # but those can be array_like structures too, in which case the
    # result will be a matrix representing the values in the grid 
    # specified by those arguments
    f = interp2d(x_list,z_list,C_I_list,kind="linear")
    
    x_coords = np.arange(min(x_list),max(x_list)+1)
    z_coords = np.arange(min(z_list),max(z_list)+1)
    c_i = f(x_coords,z_coords)
    
    fig = plt.imshow(c_i,
               extent=[min(x_list),max(x_list),min(z_list),max(z_list)],
               origin="lower", interpolation='bicubic')
    
    # Show the positions of the sample points, just to have some reference
    fig.axes.set_autoscale_on(False)
    plt.scatter(x_list,z_list,400,facecolors='none')
    plt.colorbar()
    plt.show()
    

    有人知道如何更改地图上颜色的分辨率,以便更好地查看整个地图中 0 和 1 值之间的变化吗?

    地图现在如下所示:

    【讨论】:

    • 此解决方案的问题在于,它假定您的示例中的 x_list 和“z_list”是从矩形网格中单调采样的。虽然效果很好,但用户可能需要将数据分箱到常规网格中,然后应用 interp2
    猜你喜欢
    • 2016-07-20
    • 2021-09-28
    • 1970-01-01
    • 2016-08-09
    • 2013-07-19
    • 1970-01-01
    • 2021-08-30
    • 2010-11-29
    • 2020-03-31
    相关资源
    最近更新 更多