在 gnuplot 中绘制点之间的线

问题描述 投票:0回答:1

我有下一个脚本来从文件“puntos”中绘制点

set title "recorrido vehiculos"
set term png
set output "rutasVehiculos.png"
plot "puntos" u 2:3:(sprintf("%d",$1)) with labels font ",7" point pt 7 offset char 0.5,0.5 notitle

文件“puntos”具有以下格式:

#i x y
1 2.1 3.2
2 0.2 0.3
3 2.9 0.3

在另一个名为“routes”的文件中,我有连接点的路线,例如:

2
1 22 33 20 18 14 8 27 1
1 13 2 17 31 1

路线 1 连接点 1、22、33 等。 路线 2 连接点 1、13、12 等。 有没有办法用 gnuplot 执行此操作?

PS:对不起我的英语

scripting gnuplot
1个回答
1
投票

欢迎来到 StackOverflow!这是一项有趣的任务。很清楚要做什么,但是,在我看来,如何使用 gnuplot 来做到这一点并不是很明显。 以下代码似乎有效,可能还有改进的空间。

编辑:缩短并清理。

假设 xy 数据点按从 1 到 N(此处:9)的顺序排列,并且没有遗漏任何数字。否则,脚本必须进行相应调整。

数据:

SO53220324_puntos.dat

# puntos.dat
#i x y
1 2.1 3.2
2 0.2 0.3
3 2.9 0.3
4 1.3 4.5
5 3.1 2.3
6 1.9 0.7
7 3.6 1.7
8 2.3 1.5
9 1.0 2.0

SO53220324_routes.dat

# routes.dat
2
1 5 7 3 6 2 9
6 8 5 9 4

脚本:(适用于 gnuplot>=5.0.0)

### plot different routes from two files
reset session

set term pngcairo size 640,384 font ",10"
set output "SO53220324_rutasVehiculos.png"

POINTS = "SO53220324_puntos.dat"
ROUTES = "SO53220324_routes.dat"

stats ROUTES u 0 nooutput   # get the number of routes
RoutesCount = STATS_records-1

set print $RoutesData
    # loop routes
    do for [i=1:RoutesCount] {
        # get the points of a single route
        set datafile separator "\n"
        stats ROUTES u (SingleRoute = strcol(1)) every ::i::i nooutput
        # create a table of the coordinates of the points of a single route
        set datafile separator whitespace
        do for [j=1:words(SingleRoute)] {
            n = word(SingleRoute,j)
            stats POINTS u (a=$2, b=$3) every ::n-1::n-1 nooutput
            print sprintf("%g %s %g %g", j, n, a, b)
        }
        print ""; print "" # add two empty lines
    }
set print

set key noautotitle
set colorsequence classic
set title "recorrido vehiculos"

plot for [i=1:RoutesCount] $RoutesData u 3:4 index i-1 w lp pt 7 title sprintf("Route %g",i), \
     POINTS u 2:3:(sprintf("%d",$1)) w labels font ",10" point pt 7 offset char 0.5,0.5
set output
### end of script

结果:

© www.soinside.com 2019 - 2024. All rights reserved.