【问题标题】:2D Array for Heatmap热图的二维数组
【发布时间】:2022-11-17 13:43:49
【问题描述】:

我正在尝试创建一个二维数组,我将使用它来绘制热图。

该数组需要 n × n,并且最大值位于其震中,值越远越远,如下图所示。

我怎么能那样做?

【问题讨论】:

    标签: arrays multidimensional-array arraylist


    【解决方案1】:

    您可以使用 numpy 作为数组,使用 matplotlib 分别创建热图。像这样:

    
    import numpy as np
    import matplotlib.pyplot as plt
    # creating array using numpy
    array=np.ones((9,9),dtype=int)
    array[1:8,1:8]=2
    array[2:7,2:7]=3
    array[3:6,3:6]=4
    array[4,4]=5
    print(array)
    
    fig, ax = plt.subplots()
    im = ax.imshow(array,cmap="PuBuGn") # cmap can be Greys, YlGnBu, PuBuGn, BuPu etc
    # Create colorbar
    cbar = ax.figure.colorbar(im, ax=ax,ticks=[1,2,3,4,5])
    cbar.ax.set_ylabel("My bar [1-5]", rotation=-90, va="bottom")
    ax.set_xticklabels([])
    ax.set_yticklabels([])
    ax.set_title("My heatmap")
    fig.tight_layout()
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2016-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-09
      • 1970-01-01
      • 2021-11-12
      相关资源
      最近更新 更多