Sjoerd222888
Sjoerd222888

Reputation: 3476

gnuplot: draw polygon from data

How can I plot polygons with data coming from a file? For example if I have a file containing coordinates of the edges of a four-point polygon for each data point, how would I proceed?

e.g. a data file containing

0 0 0 1 1 1 1 0
2 2 2 3 3 3 3 2

should draw two quadratic rectangular wit center at position (0.5,0.5) and (2.5,2.5).

Upvotes: 3

Views: 5632

Answers (1)

Christoph
Christoph

Reputation: 48390

Gnuplot doesn't have a dedicated plotting style for plotting arbitrary quadrangles. For that, you must use a different data file format like

0 0
0 1
1 1
1 0
0 0

2 2
2 3
3 3
1 0
0 0

which you can then simply plot with plot 'file.txt' using 1:2 with lines. The empty line between the two parts tells gnuplot to not connect the rectangles.

If you cannot or don't want to change the data file format, you can change the data on-the-fly with an external tool like

plot '< awk ''{print $1,$2,"\n",$3,$4,"\n",$5,$6,"\n",$7,$8,"\n",$1,$2,"\n"}'' rect.txt' with lines

Upvotes: 7

Related Questions