【问题标题】:labeling 2d contour plot from table in gnuplot从 gnuplot 中的表中标记 2d 等高线图
【发布时间】:2019-07-19 04:28:31
【问题描述】:

我需要使用带有数据表的 gnuplot 创建一个 2D 等高线图。我不确定如何标记轮廓。

我无法使用 splot 功能创建等高线标签,因为我想在等高线图上添加 2D 图。

以下是复制代码以创建 2D 等高线图。我的问题是如何使用数据表创建标签。

reset
f(x,y)=(x**2+y-11)**2+(x+y**2-7)**2
set xrange [0:5]
set yrange [0:5]
set isosample 250, 250
set table 'test1.dat'
splot f(x,y)
unset table

set contour base
set cntrparam levels disc 450,250,150,100,60,30,10,2 
unset surface
set table 'cont1.dat'
splot f(x,y)
unset table


reset session

set terminal wxt size 800,600 enhanced font 'Verdana,10' persist


set style arrow 2 head nofilled size screen 0.03,15 ls 2 lc rgb "blue"

set xrange [0:5]
set yrange [0:5]
unset key
#set palette rgbformulae 33,13,10

p 'cont1.dat' w l lt -1 lw 1.5

这是基于运行上述代码的cont1.dat。最后一列是我想让它绘制等高线图的标签。

【问题讨论】:

    标签: plot gnuplot contour


    【解决方案1】:

    我不清楚您是在谈论轮廓的“标签”还是轮廓的“键”(或图例)。 这是两种可能性的最小化示例。我假设您刚刚选择了一个用于演示目的的函数,但您的数据将来自文件。

    避免曲线间隙的一个技巧(没有外部脚本)是跳过 5 行和 set datafile commentschar " " 并采用 columnheader(5)。然后将# Contour 0, label: 2 之类的行视为数据,其中第 5 列是您的键。显然,尽管 cmetschar 设置为空格,但实际数据仍然(或幸运地)未被解释为 cmets。

    代码:

    ### contour lines with labels
    reset session
    
    f(x,y)=(x**2+y-11)**2+(x+y**2-7)**2
    
    set xrange [0:5]
    set yrange [0:5]
    set isosample 250, 250
    
    set contour base
    set cntrparam levels disc 450,250,150,100,60,30,10,2 
    unset surface
    set table $Contour
        splot f(x,y)
    unset table
    
    set style textbox opaque noborder
    
    set multiplot layout 2,1
        plot $Contour u 1:2 w l lw 1.5 notitle, '' u 1:2:3 every 50 w labels boxed notitle
    
        set datafile commentschar " "
        plot for [i=1:8] $Contour u 1:2:(i) skip 5 index i-1 w l lw 1.5 lc var title columnheader(5)
    unset multiplot
    ### end of code
    

    结果:

    加法:

    如果添加行

    set key top left opaque box
    

    并将绘图命令交换为:

    plot for [i=1:8] $Contour u 1:2 skip 5 index i-1 w l lw 1.5 lc 0 dt i title columnheader(5)
    

    您将获得以下信息:

    请注意,只有 5 种预定义的短划线类型会重复,但是,您可以定义自己的短划线模式(请参阅 help dashtype)。

    【讨论】:

    • 你的图看起来很清晰,你能告诉我你用的是什么字体、页面大小、终端、纵横比吗?
    • 这是在Win7,wxt终端上,大小恰好是568x643 px,然后在交互式终端中保存为PNG文件。字体是标准字体。好吧,我(还)不知道如何找出字体的实际名称。
    • 你真的在寻找第一个还是第二个情节?如果您将第二个图与更多图(如您所写)结合起来,那么 commentschar " " 可能会导致一些不需要的结果。您需要注意并需要检查。
    • 我玩过commentschars,但我不知道skip。很高兴知道。
    • 所以我假设您要的是第二个情节。见加法,,,
    【解决方案2】:

    我想你想要这样的东西:

    datafile = 'cont1.dat'
    stats datafile nooutput
    plot for [i=0: STATS_blocks-1] datafile index i title columnhead(3) w l
    

    stats 命令收集有关数据文件的一些统计信息,特别是它计算块的数量。块之间用两条空线隔开,每个块对应不同的轮廓层级。

    columnheads(3) 命令将每个块中第一行第三列的条目作为标题。

    不幸的是,在您的情况下,由于columnhead 命令,第一行被解释为标题行并被跳过以进行绘图。这会导致结果图中出现空白:

    我建议在不使用第一个数据行的情况下进行一些预处理以生成标题行。对我有用的最简单的事情是删除每个块开始的注释行开头的#。我在Linux上,所以我使用sed

    sed "s/# Contour/Contour/" -i cont1.dat
    

    这可以从gnuplot中调用,最终脚本如下:

    reset
    
    # We need the datafile several times
    datafile = 'cont1.dat'
    
    f(x,y)=(x**2+y-11)**2+(x+y**2-7)**2
    
    set xrange [0:5]
    set yrange [0:5]
    set isosample 250, 250
    
    # Generate data
    set contour base
    set cntrparam levels disc 450,250,150,100,60,30,10,2 
    set view map
    unset surface
    set table datafile
    splot f(x,y)
    unset table
    
    # Count blocks
    stats datafile nooutput
    
    system("sed \"s/# Contour/Contour/\" -i ".datafile)
    
    # Plot each data block separatly and set the title to 
    # the first entry of the third column of the respective block
    plot for [i=0: STATS_blocks-1] datafile index i title columnhead(4) w l 
    

    一个缺点:sed 命令之后的数据文件包含一些无效行“Contour 0, label:”,没有注释哈希#,也没有标签编号。它们被 gnuplot 忽略,因为它们不包含有效数据。

    【讨论】:

      猜你喜欢
      • 2019-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多