построение линий между точками в gnuplot - PullRequest
0 голосов
/ 09 ноября 2018

У меня есть следующий скрипт для построения точек из файла "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

в другом файле, называемом «маршруты», у меня есть маршруты, соединяющие точки, например:

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 голосов
/ 10 ноября 2018

Добро пожаловать в stackoverflow. Это интересное задание. Совершенно ясно, что делать, однако, на мой взгляд, не очень очевидно, как это сделать с gnuplot. Следующий код, кажется, работает, вероятно, с возможностью улучшения. Протестировано в gnuplot 5.2.5

Протестировано с файлами puntos.dat и 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

и

# 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

...