6

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?

user1078195
  • 105
  • 2
  • 5

3 Answers3

1

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:

datafile.dat

# 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

gnuplot_rect_via_boxxyerror

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

gnuplot_rectfill_via_boxxyerror

lys
  • 949
  • 2
  • 9
  • 33
  • 1
    You are absolutely right. The plotting style `with boxxyerror` was available already in gnuplot 4.4.0 (March 2010), probably even earlier. So, I don't understand the currently accepted answer from Dec 2011, the strange answer from 2016 and the feature request from 2020. – theozh Mar 29 '23 at 10:45
  • 1
    By the way, you could also use the 4 column syntax of `boxxyerror`, e.g. `plot 'datafile.dat' u (($1+$3)/2)):(($2+$4)/2):(($3-$1)/2):(($4-$2)/2) w boxxy`. Check `help boxxyerror`. – theozh Mar 29 '23 at 10:53
  • 1
    I think `with boxxyerror` can be a little obscure for newer users - since there are more examples for using `set object rectangle` – lys Mar 29 '23 at 10:56
  • I agree. The `boxxyerror` plotting style could also have been named `with rectangle` and users would find it more easily. – theozh Mar 29 '23 at 11:00
1

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.

vaettchen
  • 7,299
  • 22
  • 41
-1

You probably need to store the data from the file into variables, then use these variables to define the locations of objects. The way to do this is not straight as far as I know. Please refer to here. Good luck!

Community
  • 1
  • 1
pig
  • 33
  • 5