formatlabelgnuplotaxisexponential

gnuplot: compact axis label format


Tell me how in gnuplot to properly form the inscriptions in the axis (as shown in the picture).

1) I do not know the values of the y axis

2) I need to set the exponent (power 10) in the axis label automatically

enter image description here


Solution

  • You need to know the order of magnitude of your data before plotting. If you don't know it beforehand, you can get this via stats (check help stats). Then divide your y-values by a factor which (in the example below) is chosen such that the y-axis tics should show values with two digits (digits=2), i.e. range 0-100.

    Script:

    ### automatic prefactor in axis scaling and axis label
    reset session
    
    # generate some random data
    p  = floor(rand(0)*20-10)
    y0 = rand(0)
    set samples 20
    set table $Data
        plot '+' u 0:(y0*10**p/($0)) with table
    unset table
    
    # get the maximum via stats
    stats $Data u 2 nooutput
    yMax = STATS_max
    
    digits    = 2
    factorLog = ceil(log10(yMax)) - digits
    factor    = 10**factorLog
    
    set ylabel sprintf("y-title, x 10^{%d} units", factorLog) font ",12"
    set format y "%g"
    set boxwidth 0.7 relative
    
    plot $Data u 1:($2/factor) with boxes fs solid 1.0 fc rgb "red" ti sprintf("Max value %.2e", yMax)
    ### end of script
    

    Result:

    enter image description here