从gnuplot中的表标记2d等高线图

时间:2019-07-16 16:28:26

标签: plot gnuplot contour

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

我无法使用绘图功能创建轮廓标签,因为我想在轮廓图的顶部添加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的外观,基于运行上述代码。最后一栏是我要为其绘制轮廓图的标签。

enter image description here

2 个答案:

答案 0 :(得分:2)

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

避免曲线间隙的技巧(无需外部脚本)是跳过5条线和set datafile commentschar " "并取columnheader(5)。像# Contour 0, label: 2这样的行将被视为数据,其中第五列是您的键。显然,尽管commentchar设置为space,但实际数据还是(或幸运地)没有被解释为注释。

代码:

### 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

结果:

enter image description here

添加:

如果添加行

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)。

enter image description here

答案 1 :(得分:1)

我认为您想要这样的东西:

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命令,第一行被解释为标题行,并跳过了打印。这会导致结果图中的空白:

with gaps

我建议您进行一些预处理,以在不使用第一条数据线的情况下生成标题行。对我有用的最简单的方法是在每个块开始的注释行的开头删除#。我在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 

without gaps

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

相关问题