1
votes

I've got next script to plot dots from file "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

file "puntos" has next format:

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

in another file called "routes" i have the routes that joins the points, for example:

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

Route 1 joins points 1, 22, 33, etc. Route 2 joins points 1, 13, 12, etc. Is there a way that perform this with gnuplot?

PS: sorry for my English

1
Although possible, like the answer shows, this is not a task for gnuplot. Use any external script in python, perl, you-name-it, to process your data and leaving only the plotting to gnuplot - Christoph

1 Answers

1
votes

Welcome to stackoverflow. This is an interesting task. It's pretty clear what to do, however, to my opinion not very obvious how to do this gnuplot. The following code seems to work, probably with room for improvements. Tested in gnuplot 5.2.5

Tested with the files puntos.dat and routes.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

and

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

and the code:

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

which results in something like:

enter image description here