【问题标题】:How to plot heatmap of 3 variables (depth, time, parameter)如何绘制 3 个变量(深度、时间、参数)的热图
【发布时间】:2021-01-22 01:50:26
【问题描述】:

我有随时间和深度变化的温度数据(见附件示例图片)。不过,我的实际数据集要大得多。我想知道如何绘制热图。我对 Python 或 Matlab 持开放态度。

谢谢。

【问题讨论】:

    标签: python matlab plot 2d heatmap


    【解决方案1】:

    在 MATLAB 中

    下面是一种方法,它使用meshgrid() 函数为Temperature 创建一个域,以便在/ 上绘制。要绘制 2D 热图,我们可以使用 surf() 并使用 view() 函数将高度设置为顶视图(90 度)。根据您是否希望进行插值,可以包含或删除 shading interp 的使用。要获取时间标签,我们可以将Time_Vector 转换为字符串数组并使用arrayfunc()(数组函数)将点. 替换为冒号:。最后,我们可以在当前轴上使用set() 函数gca 在绘图上显示新格式化的时间标签。 colormap()可以设置为'hot''winter''spring'等多种选项。

    Time_Vector = (10.00: 0.01: 10.09);
    Depth_Vector = (1:3);
    Temperature = [15 16 17 18 19 20 20 20 20 20;
                   25 30 35 40 45 50 50 50 50 50;
                   30 35 40 45 50 55 60 65 70 75];
    
    [Time_Grid,Depth_Grid] = meshgrid(Time_Vector,Depth_Vector);
    
    surf(Time_Grid,Depth_Grid,Temperature);
    title("Heatmap");
    xlabel("Time"); ylabel("Depth");
    colormap(hot);
    shading interp
    Angle = 0; Elevation = 90;
    view(Angle,Elevation);
    colorbar;
    
    %Time label adjustments%
    Time_Labels = string(Time_Vector);
    Time_Labels = arrayfun(@(x) replace(x,".",":"),Time_Labels);
    set(gca,'xtick',Time_Vector,'xticklabel',Time_Labels);
    

    使用 MATLAB R2019b 运行

    【讨论】:

    • 感谢 MichaelTr7。我还想问,我的温度从 70 到 100 不等。但是,色谱变化并不那么深刻,因为我想它正在考虑 0-100,例如红色到紫色的变化。您如何获得 70-100 范围内的全色谱?
    • 您希望将颜色范围标准化为 70 到 100 之间的数据?
    【解决方案2】:

    在 python 中,如果你有一个矩阵形式的数据,比如你的表格,你可以使用 matplotlib 的 imshow 函数。缺点是刻度标签将是列表中的位置,因此您需要使用 FuncFormatter 选项传递一个函数来转换数据值中的列表位置。一个例子:

    import numpy as np
    import matplotlib.pyplot as plt
    
    from matplotlib.ticker import FixedLocator, AutoLocator, FuncFormatter
    
    
    #Just creating a sample data
    dep = np.array([1,2,3])
    time = np.arange(0,101)
    dp,tm = np.meshgrid(dep,time)
    
    matrix = np.flip(600*np.exp(-tm*dep/50),axis = 0)
    #in this case we will have 3 columns, one for each depth
    
    #ploting:
    fig,ax = plt.subplots()
    
    img = ax.imshow(matrix,cmap = 'OrRd',vmin = 0,vmax = 600,aspect = 'auto')
    
    #set color bar
    fig.colorbar(img,label = 'Temperature')
    
    #Now we will correct the the x axis tick labels
    ax.xaxis.set_major_formatter(FuncFormatter(lambda *x: x[0]+1))
    #And fix the positions if you dont want tick labels like 2.5 wich are not in the data
    ax.xaxis.set_major_locator(FixedLocator([0,1,2]))
    
    ax.set_xlabel('Deph')
    
    #correct the y axis tick labels
    ax.yaxis.set_major_formatter(FuncFormatter(lambda *x: '%i'%(time[-1] - x[0])))
    ax.set_ylabel('Time')
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-02
      • 2016-12-21
      • 2016-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-23
      • 1970-01-01
      相关资源
      最近更新 更多