【问题标题】:How to add contour lines to a heat map如何在热图中添加等高线
【发布时间】:2020-12-26 00:39:07
【问题描述】:

我有一个脚本,它获取数据(格式为 3 列 x,y,z)并给出热图:

set logscale x 10
set yrange [1e-9:2e-8]

set xlabel "x"
set ylabel "y"
set multiplot


plot 'filetest.dat' u 1:2:9 with image

这是一个 2D 热图,如下所示:

我想要做的就是在这个图中添加轮廓,在一些 z 值上,例如 -20 到 -8 in,间隔为 2。不幸的是,我找到的答案都无法帮助我解决这个问题.任何帮助将不胜感激。

【问题讨论】:

    标签: plot gnuplot contour


    【解决方案1】:

    虽然www.gnuplot.info 上有很多关于轮廓的例子,但我找不到你的确切情况,因为这些例子是函数,而不是数据块或数据文件(嗯,应该是相似的)。 以下代码可以满足您的要求,但是用于添加标签的构造 '' u 1:2:3:("") w labels 对我来说仍然看起来很奇怪,并且不允许绘制盒装标签。 在 gnuplot 控制台中检查 help contourhelp cntrparam

    代码:

    ### pm3d with contour lines
    reset session
    set view equal xyz
    
    # create some test data
    set samples 40
    set isosamples 40
    set table $Data
        splot '++' u 1:2:($1*$2/2-9)
    unset table
    
    set view map
    set contour base
    set cntrparam levels incremental -20,2,-8
    set cntrlabel font ",10"
    
    set xrange[-5:5]
    set yrange[-5:5]
    
    splot $Data u 1:2:3 w pm3d notitle, '' u 1:2:3:("") w labels notitle
    ### end of code
    

    结果:

    加法:

    这是使用plot w image 而不是splot w pm3d 的另一种方法。 尽管轮廓线顶部的白色标签框仍然不能完全令人满意。向标签添加偏移量不会同时适用于所有标签。我不确定是否有办法只打断标签的轮廓线。

    代码:

    ### heatmap with contour lines
    reset session
    set view equal xyz
    
    # create some test data
    set samples 40
    set isosamples 40
    set table $Data
        splot '++' u 1:2:($1*$2/2-9)
    unset table
    
    set view map
    set contour base
    set cntrparam levels incremental -20,2,-8
    set cntrlabel font ",10"
    
    set xrange[-5:5]
    set yrange[-5:5]
    set style textbox noborder opaque
    
    # put contour lines in a separate datablock
    unset surface
    set table $Contour
       splot $Data u 1:2:3
    unset table
    
    plot $Data u 1:2:3 w image notitle, \
         $Contour u 1:2 w l lw 2 lc "black" not, \
         '' u 1:2:3 every 40::3 w labels boxed notitle
    ### end of code
    

    结果:

    加法2:

    另一种用彩色轮廓线和键代替标签的变体。这个好像有点麻烦,希望有更简单的解决方法。

    代码:

    ### heatmap with colored contour lines
    reset session
    set view equal xyz
    
    # create some test data
    set samples 40
    set isosamples 40
    set table $Data
        splot '++' u 1:2:($1*$2/2-9)
    unset table
    
    set view map
    set contour base
    set cntrparam levels incremental -20,2,-8
    
    set xrange[-5:5]
    set yrange[-5:5]
    set style textbox noborder
    
    # put contour lines in a separate datablock
    unset surface
    set table $Contour
       splot $Data u 1:2:3
    unset table
    
    # get contour levels unique and in sorted order
    set table $Dummy
        plot $Contour u 3 w table
    unset table
    set table $ContourSorted
        plot $Dummy u 1 smooth freq
    unset table
    print $ContourSorted
    
    set key out right Left
    
    plot $Data u 1:2:3 w image notitle, \
         for [i=0:*] $Contour u 1:2:3 index i w l lw 2 lc i+1 not, \
         for [i=|$ContourSorted|-2:5:-1] $ContourSorted u (NaN):1 w l lw 2 lc |$ContourSorted|-i-1 ti word($ContourSorted[i],1)
    ### end of code
    

    结果:

    【讨论】:

    • 当我运行这个确切的代码时,它给了我相同的情节,除了没有黑色轮廓线......你知道这是为什么吗?标签和一切都还在
    • 我不确定为什么绘制第一种方法时会有所不同。我添加了另一种方法。
    • @lilolarorphism 好的,现在我明白了,使用 gnuplot 5.2.6 它绘制黑色轮廓线。使用 5.2.7 和 5.2.8 它不会....然后发生了一些变化,嗯,我需要找出...
    猜你喜欢
    • 2020-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-29
    相关资源
    最近更新 更多