23

Using the standard plot command, I get, what I want except that the yrange is set automatically from (eg) 275 to 300.

Unfortunately, I have several data points with y-coordinate 300, such that they are not visible (due to border lines, etc.).

So, is there any way to set the maximum yrange such that it is always the maximum data plus e.g. 5 units?

Using autoscale, the yrange is set to 275:300. Setting explicitly the range to 275:305 would work for one data file but not for others. So I need some generic method to determine the max-data point and set the yrange larger.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Matthias
  • 387
  • 1
  • 2
  • 6

2 Answers2

27
set offsets <left>, <right>, <top>, <bottom>

will do. Note the scale follows the data scale, so it will eventually depend on the data you want to plot. Alternatively, you can use set offsets graph ... to use fraction of the plot size instead.

Sunhwan Jo
  • 2,293
  • 1
  • 15
  • 13
  • Especially useful, because online the `GPVAL_Y_MAX` method, it also works with `set multiplot ...`. – kdb Aug 04 '20 at 13:15
12

There are Gnuplot defined values GPVAL_Y_MAX and GPVAL_DATA_Y_MAX (also GPVAL_Y_MIN, GPVAL_DATA_Y_MIN ...). After your plot, the maximum value will be stored in these values. So you can plot your data then set yrange GPVAL_Y_MAX+(GPVAL_Y_MAX-GPVAL_Y_MIN)*0.05. At this time you plot you data for the second time. This time you just get what you want. The following is my code.

    reset
    plot "data.dat" u 1:2 #To get the max and min value
    MAX=GPVAL_Y_MAX
    MIN=GPVAL_Y_MIN
    set yrange [MIN-(MAX-MIN)*0.05:MAX+(MAX-MIN)*0.05]
    #Add a fixed value is not a good idea, so I use a relative one
    set term png
    set output "out.png"
    plot "data.dat" u 1:2 w p notitle #This plot will create a file
    #named out.png which is waht you want.

I learned the method from this article--http://gnuplot-surprising.blogspot.com/2011/09/advanced-background-color-1.html

NichtJens
  • 1,709
  • 19
  • 27
hsxz
  • 1,049
  • 8
  • 6
  • 10
    `set offsets graph 0, 0, 0.05, 0.05` will do the same thing. – Sunhwan Jo Sep 09 '11 at 14:02
  • 1
    There doesn't appear to be any way to "get" the default range or to "get" it after setting it. It would be useful for me to be able to recover the present ranges without cluttering up my gnuplot script with extra code to track. It is obvious that the range data exists since "show xrange" for example, displays it. Why cannot I access that data directly in a script? (or perhaps I just don't know how?). – Burton Dec 17 '20 at 16:54