在gnuplot中的点之间绘制线

时间:2018-11-09 05:26:35

标签: scripting gnuplot

我有下一个脚本来绘制文件“ 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:对不起,我的英语

1 个答案:

答案 0 :(得分:1)

欢迎来到stackoverflow。这是一个有趣的任务。很明显该怎么做,但是,我认为如何做这个gnuplot不是很明显。 以下代码似乎可行,可能还有改进的余地。在gnuplot 5.2.5中测试

使用文件puntos.datroutes.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

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

和代码:

### plot different routes
reset session
set title "recorrido vehiculos"
set term pngcairo
set output "rutasVehiculos.png"

POINTS = "puntos.dat"
ROUTES = "routes.dat"

# load routes file into datablock
set datafile separator "\n"
set table $Routes
    plot ROUTES u (stringcolumn(1)) with table
unset table

# loop routes
set datafile separator whitespace
stats $Routes u 0 nooutput  # get the number of routes
RoutesCount = STATS_records-1
set print $RoutesData
do for [i=1:RoutesCount] {
    # get the points of a single route
    set datafile separator "\n"
    set table $Dummy
       plot ROUTES u (SingleRoute = stringcolumn(1),$1) every ::i::i with table
    unset table
    # create a table of the coordinates of the points of a single route
    set datafile separator whitespace
    do for [j=1:words(SingleRoute)] {
        set table $Dummy2
            plot POINTS u (a=$2,$2):(b=$3,$3) every ::word(SingleRoute,j)-1::word(SingleRoute,j)-1 with table
            print sprintf("%g %s %g %g", j, word(SingleRoute,j), a, b)
        unset table
    }
    print "" # add empty line
}
set print
print sprintf("%g different Routes\n", RoutesCount)
print "RoutesData:"
print $RoutesData
set colorsequence classic 
plot \
    POINTS u 2:3:(sprintf("%d",$1)) with labels font ",7" point pt 7 offset char 0.5,0.5 notitle,\
    for [i=1:RoutesCount] $RoutesData u 3:4 every :::i-1::i-1 w lp lt i title sprintf("Route %g",i)

set output
### end code

结果类似:

enter image description here

相关问题