gnuplot: max and min values in a range

后端 未结 3 1257
傲寒
傲寒 2020-12-05 08:10

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

3条回答
  •  一整个雨季
    2020-12-05 08:29

    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 
    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].

提交回复
热议问题