Search code examples
gnuplotscaleaxes

How to use nonlinear axes in Gnuplot?


I saw some examples like this one:

f(x) = log10(x)
g(x) = 10**x
set nonlinear x via f(x) inverse g(x)

So this one is equivalent to just log-scaling the x.
But I don’t get why we need to write the inverse function? And also I have this situation:

For data in x>=0 range I need to scale x in a way that it shows in an almost-half plot;
For data in -100<=x<0 I need to scale x in a way that it shows in a small part of plot;
For data in x<-100 I need to scale x in a way as for data in x>=0.

So let’s imagine we have an a4paper and gnuplot creates his plots on it. I want to have a plot result that will be drawn in an x scales like:

If x>=0 1cm = 5 
If -100<=x<0 1cm = 100
If x<-100 1cm = 5

(I don’t mean it’s important to me to have only this centimeters, it just says that I need a correlation between delta of two x values and real length between them.)

I’m so sorry I can’t understand this mechanics of scaling.


Solution

  • The forward function tells gnuplot where to draw user coordinate [x,y] on the page. Call that location [x',y']. Only the forward function is needed for this. But the interactive terminals echo back mouse position and allow you to click on the plot for various purposes. In order to know what a mouse click on [x',y'] means, the program has to convert it back to the original [x,y]. For that it needs the inverse function.

    For an example of using different scaling functions over different portions of the full plot, see the online demo nonlinear1.dem reproduced below

    # This example shows how a nonlinear axis definition can be used to 
    # create a "broken axis". X coordinates 0-100 are at the left,
    # X coordinates 500-1000 are at the right, there is a small gap between them.
    # So long as no data points with (100 < x < 500) are plotted, this works as expected.
    #
    # f(x) maps x axis (discontinuous) to shadow axis coords (continuous linear range [0:1000])
    # g(x) maps shadow axis coords to x axis readout
    # 
      set title "A 'broken' x axis can be defined using 'set nonlinear x'"
    
    # Define the broken-axis mapping
      axis_gap = 25.
      f(x) = (x <= 100) ? x : (x < 500) ? NaN : (x - 400 + axis_gap)
      g(x) = (x <= 100) ? x : (x < 100 + axis_gap) ? NaN : (x + 400 - axis_gap)
      set xrange [15:600] noextend
      set nonlinear x via f(x) inverse g(x)
    
      set xtics 50.
      set xtics rotate by -90 nomirror
      set ytics nomirror
      set border 3
      unset key
    
    # Creation of the broken axis marks (this should be automated)
      set arrow 500 from 100, graph 0 to 500, graph 0 nohead lt 500 lw 2 lc bgnd front
      set arrow 501 from 100, graph 0 length graph  .01 angle 75 nohead lw 2 front
      set arrow 502 from 100, graph 0 length graph -.01 angle 75 nohead lw 2 front
      set arrow 503 from 500, graph 0 length graph  .01 angle 75 nohead lw 2 front
      set arrow 504 from 500, graph 0 length graph -.01 angle 75 nohead lw 2 front
    
      plot 'silver.dat' with yerrorbars lw 2, '' with lines
    

    enter image description here