Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gnuplot: max and min values in a range

Tags:

gnuplot

I'm plotting some data with a different X range and I would like to change yrange according to the maximum and minimum value of the data in the current X range. When I use GPVAL_Y_MAX and GPVAL_Y_MIN, these values correspond to the maximum and minimum of the whole data, not just the data in the range.

For example, I have the following data:

1 3
2 5
3 8
4 20
5 30

I use the following script:

plot 'data.txt' u 1:2;
set xrange [1:3];
replot
set xrange [1:5];
replot

In the first plot I would like to set yrange in [3:8], but in the second plot the yrange sholud be [3:30]. If I use something like

set yrange [GPVAL_Y_MIN:GPVAL_Y_MAX]

GPVAL_Y_MIN and GPVAL_Y_MAX have the same value independently of the xrange.

Any solution?

like image 323
jmatamoros Avatar asked Nov 27 '12 08:11

jmatamoros


People also ask

Does gnuplot support multiple Y axes on a single plot?

5.9 Does gnuplot support multiple y-axes on a single plot? Yes. 2D plots can have separate x axes at the bottom (x1) and top (x2), and separate y axes at the left (y1) and right (y2).

What is gnuplot Splot?

splot is the command for drawing 3-d plots (well, actually projections on a 2-d surface, but you knew that). It can create a plot from functions or a data file in a manner very similar to the plot command. See plot (p. ) for features common to the plot (p. ) command; only differences are discussed in detail here.

Is gnuplot easy?

Running gnuplot is easy: from a command prompt on any system, type gnuplot. It is even possible to do this over a telnet or ssh connection, and preview the graphs in text mode!


2 Answers

The variables you want are GPVAL_DATA_Y_MIN and GPVAL_DATA_Y_MAX, which are the y-min/max of the data plotted in a certain range. GPVAL_Y_MIN and GPVAL_Y_MAX are a little less useful generally because they tell you where the edges of the plot border are (in general these values extend a little beyond the GPVAL_DATA... variables because gnuplot leaves a little space between the data and the edge of the plot).

To take advantage of these variables you have to use the range specifiers to the plot command:

plot [1:3] 'data.txt'
set yr [GPVAL_DATA_Y_MIN:GPVAL_DATA_Y_MAX]
replot
...

By the way, the u 1:2 specification is redundant unless you want to remind yourself of which columns you are plotting, since plotting the first two columns as x and y is the gnuplot default. If you don't want to replot to the same output terminal (which is not helpful in some terminals like eps where replotting makes a second page with the same plot), use this command sequence:

set terminal unknown
plot [1:3] 'data.txt'
set terminal <actual output terminal here>
set output 'output.trm'
plot [1:3][GPVAL_DATA_Y_MIN:GPVAL_DATA_Y_MAX] 'data.txt'

Note the use of the range specifier again, this time with a y range specified. This is a little more compact than specifying with set yrange, but makes for a longer line of code.

If you have gnuplot 4.6.0 or higher, you can take advantage of the stats command to avoid replotting. The stats command creates a bunch of handy variables

stats [1:3] 'data.txt'
plot [1:3][stats_min_y:stats_max_y] 'data.txt'

A slightly different command,

stats [1:3] 'data.txt'
plot [stats_min_x:stats_max_x][stats_min_y:stats_max_y] 'data.txt'

Would fill the plot in the x direction based on where the actual data lie. For instance if you had data points at {(1.1, 3), (2, 4), (2.9,5)}, the x range would be set to [1.1:2.9].

like image 131
andyras Avatar answered Sep 23 '22 07:09

andyras


Setting the yrange to GPVAL_DATA_Y_MIN:GPVAL_DATA_Y_MAX has the disadvantage of not using gnuplots autoscaling functionality which extends the ranges to the next tic.

In automatic plotting I therefore prefer the following

f(x)=sin(x)>0.5? 1:-1 #example function

set ytics 0.2
plot  1.01*f(x) # dummy plot to set GPVAL_*
set yrange [GPVAL_Y_MIN:GPVAL_Y_MAX]

plot f(x) # actual plot

This also works for data plots of course:

plot 'data.csv' u 1:(1.01*$2)
set yrange [GPVAL_Y_MIN:GPVAL_Y_MAX]
plot 'data.csv' u 1:2
like image 35
Malte Avatar answered Sep 23 '22 07:09

Malte