gnuplot

undefined variable: GPVAL_DATA_Y_MIN (Gnuplot)


Based on this post (I am using gnuplot gnuplot 4.6 patchlevel 1):

gnuplot: max and min values in a range

I am trying to use set yr [GPVAL_DATA_Y_MIN:GPVAL_DATA_Y_MAX] in my .pg script that plots data from this .dat file:

100 2
200 4
300 9

But I get:

undefined variable: GPVAL_DATA_Y_MIN

This is my script:

set terminal png
set output 'img.png'
set xlabel 'x-label'
set ylabel 'y-label'
set boxwidth 0.5
set style fill solid
set yrange [GPVAL_DATA_Y_MIN:GPVAL_DATA_Y_MAX]
plot 'sample.dat' with boxes title ""

Any ideas?


Solution

  • The gnuplot-defined variables are available only after a plot command (In the question you linked to, a replot is used to plot again).

    Basically you have different options:

    1. First plot to terminal unknown, and then change to the real terminal, set the range (now the variables are available), and replot:

      set xlabel 'x-label'
      set ylabel 'y-label'
      set boxwidth 0.5 relative
      set style fill solid
      
      set terminal unknown
      plot 'sample.dat' with boxes title ""
      
      set terminal pngcairo
      set output 'img.png'
      set yrange [GPVAL_DATA_Y_MIN:GPVAL_DATA_Y_MAX]
      replot
      

      Note, that I used the pngcairo terminal, which gives much better results, than the png terminal. And I used set boxwidth 0.5 relative.

    2. Use set autoscale to fix the ranges instead of setting an explicit yrange. You can use set offset to specify a margin based on the autoscaled values:

      set autoscale yfix
      # set offset 0,0,0.5,0.5
      plot 'sample.dat' with boxes title ''
      
    3. Use the stats command to extract the minimum and maximum values:

      stats 'sample.dat' using 1:2
      set yrange[STATS_min_y:STATS_max_y]
      plot 'sample.dat' with boxes title ''