In gnuplot i can draw a rectangle via
set object rect from x0,y0 to x1,y1
How to read the coordinates x0,x1,y0,y1 from a file?
In gnuplot i can draw a rectangle via
set object rect from x0,y0 to x1,y1
How to read the coordinates x0,x1,y0,y1 from a file?
I was searching at length for a clear example for this today, since this question still gets returned in a search - here's a decent workaround solution formed by reading a recent feature request for a simple with rectangles
method.
You can use the boxxyerror
plot style to draw 2D rectangular areas from a datafile. For example - with a file formated with columns like so:
# x0, y0, x1, y1
0.0, 0.0, 1.0, 1.0
1.0, 0.5, 1.1, 1.0
1.1, 0.5, 1.2, 1.0
1.2, 0.5, 1.3, 1.0
1.3, 0.5, 1.4, 1.0
1.4, 0.5, 1.5, 1.0
1.5, 1.0, 1.6, 1.5
1.6, 1.0, 1.7, 1.5
1.7, 1.0, 1.8, 1.5
1.8, 1.0, 1.9, 1.5
1.9, 1.0, 2.0, 1.5
2.0, 1.0, 3.0, 2.0
You can plot it like this using the 6 parameter form:
plot 'datafile.dat' using (($1+$3)/2):(($2+$4)/2):1:3:2:4 with boxxyerr fill fc "purple";
Or like this using the 4 parameter form:
plot 'datafile.dat' u (($1+$3)/2):(($2+$4)/2):(($3-$1)/2):(($4-$2)/2) w boxxy
You can also add more data to set the fill color:
An additional (5th or 7th) input column may be used to provide variable (per-datapoint) color information (see linecolor and rgbcolor variable).
Here's a simple example:
rgb(r,g,b) = 65536 * int(r*200) + 256 + int(g*250) + int(b*156);
plot 'datafile.dat' u (($1+$3)/2):(($2+$4)/2):(($3-$1)/2):(($4-$2)/2):(rgb($1,$3,$4)) w boxxy fs solid 0.50 fc rgb variable
One way would be to put the line of code that sets the rectangle into a separate file and call that file from within the gnuplot script. So you have a file called "coord.txt" that contains the one line
set object rect from 2,2 to 4,40
and you have a gnuplot script called "rect.gp" that says
set title "call rectangle coordinates"
load "coord.txt"
plot x**2
If you now from within gnuplot type load "rect.gp"
you get your graph with the rectangle.
That may not be exactly what you are looking for but maybe a first step.