繁体   English   中英

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

[英]labeling 2d contour plot from table in gnuplot

我需要使用带有数据表的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 最后一栏是我想要的轮廓图标签。

在此处输入图片说明

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

避免曲线间隙的技巧(无需外部脚本)是跳过5行并set datafile commentschar " " columnheader(5) set datafile commentschar " "并采用columnheader(5) 然后将# Contour 0, label: 2等行视为数据,第5列是您的键。 显然,尽管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

结果:

在此处输入图片说明

加成:

如果添加行

set key top left opaque box

并将plot命令交换为:

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

在此处输入图片说明

我想你想要这样的东西:

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命令之后的数据文件包含一些无效行“轮廓0,标签:”,没有注释哈希号,也没有标签号。 gnuplot将忽略它们,因为它们不包含有效数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM