1

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 Commented Nov 12, 2018 at 19:27

1 Answer 1

1

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 with gnuplot. The following code seems to work, probably with room for improvements.

Edit: Shortened and cleaned up.

Assumption is that the xy-data points are in sequential order from 1 to N (here: 9) and no number is left out. Otherwise the script has to be adapted accordingly.

Data:

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

Script: (works for 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

Result:

enter image description here

Addition:

Just for fun, here is a more general solution. The keys/labels/IDs for the points don't have to be numeric and in strict sequence, but can also be unordered strings. The function getIndex() will find the corresponding array index and the coordinates. Since arrays are used, gnuplot>=5.2.0 is required.

Script: (works for gnuplot>=5.2.0, Sept. 2017)

### plotting paths from two files/datablocks
reset session

$Points <<EOD
# ID   x     y
   1   1.0   1.0
   2   2.5   3.4
   3   7.8   2.5
   6   5.4   6.5
   A   8.4   7.5
   B   3.7   5.9
   C   4.5   0.2
   X   0.3   8.9
  10   6.1   3.2
 197   5.0   9.5
 Start 0.9   3.5
 End   9.1   4.2
EOD

$Routes <<EOD
 Start 1 C 3 10 6 X
 C B 197 A End
 X 2 A
EOD

stats $Points u 0 nooutput
N = STATS_records
stats $Routes u 0 nooutput
R = STATS_records

array L[N];  array X[N];  array Y[N]
stats $Points u (n=int($0+1), L[n]=strcol(1), X[n]=$2, Y[n]=$3) nooutput

getIndex(s) = (_n=NaN, sum [_i=1:N] (s eq L[_i] ? _n=_i : 0), _n)

set print $RouteData
    do for [r=1:R] {
        set datafile separator "\n"
        stats $Routes u (route=strcol(1)) every ::r-1::r-1 nooutput
        print sprintf("# Route %d",r)
        do for [i=1:words(route)] {
            s = word(route,i)
            n = getIndex(s)
            print sprintf("%d %s %g %g", i, s, X[n], Y[n])
        }
        print ""; print ""  # two empty lines
    }
set print
set datafile separator whitespace
set colorsequence classic
set key noautotitle

plot $Points u 2:3:1 w labels point pt 7 offset 0.5,0.5, \
     for [r=1:R] $RouteData u 3:4 index r-1 w lp pt 7 lc r ti sprintf("Route %d",r)
### end of script

Result:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Remarkable. For my older gnuplot 5.0 I had to change the first plot to plot ROUTES u 1 with table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.