Search code examples
gnuplotaxes

How to format x axis to a smaller scale?


So here is what I'm trying to do. The values on x axis are from 10000, 20000, 30000, ... 100000. I'm trying to write it like this: 10, 20, 30, 40, ... 100 (only x axis)

Is there some way to do this in Gnuplot?

I have this so far: (data.dat - example of data)

# x       y
10000 +1.24241522E-04
11000 +1.28623514E-04
12000 +1.35229020E-04
13000 +1.43767741E-04
14000 +1.53409148E-04
15000 +1.63788695E-04
16000 +1.75429485E-04
17000 +1.88827813E-04
18000 +2.02984785E-04
19000 +2.20830420E-04
...

(my gnuplot script)

set term png

set out 'example.png'

U0 = 0.00732     #parameters for this particular problem
v1 = 68000
b1 = 6550
v2 = 59600
b2 = 6050
I = sqrt(-1)

A(w, w0, b) = ((w0)**2)/(((w0)**2) - ((w)**2) + 2*I*w*b)

f(x) = U0*abs(A(2*pi*x, 2*pi*v1, b1) - A(2*pi*x, 2*pi*v2, b2))

set xlabel "x"
set ylabel "y"

fit f(x) 'data.dat' u 1:2 via U0, v1, b1, v2, b2

plot 'data.dat' u 1:2 t "Title1" w p, U(x) t "Title2"

set out

enter image description here

But how do I do this? I've tried this example
How to scale the axes in Gnuplot
but it doesn't work. See below.

# I modified the things a little bit

f(x) = (.... ... ....)/1000

fit f(x) 'data.dat' u ($1/1000.):2 via U0, v1, b1, v2, b2

plot 'data.dat' u ($1/1000.):2 t "Title1" w p, f(x) t "Title2"

But now the fitted function disappears!

How can I modify x-axis without other function disappearing?

enter image description here

Does there exist a line command in gnuplot for this? I'm sure there has to be a more elegant way of writing this insted of dividing each function by a desired factor.


Solution

  • Two possible ways come to my mind:

    1. if you want to avoid too many zeros in the xtic labels, simply set the xtic label format to engineering

      set format x "%.0s%c"

      This will show, e.g. 10000 and 100000 as 10k and 100k, respectively.

    2. if you scale (in your case: divide) the x values of the data by factor of 1000, gnuplot will take this x range for plotting the function f(x). Since this is will give x values which are a factor of 1000 too small you have to scale your x values by a factor of 1000 accordingly (in your case: multiply).

    Code:

    ### avoid too many zeros in xtic labels
    reset session
    
    # create some random test data
    set print $Data
        A = rand(0)*10+5
        B = rand(0)*50000+25000
        C = rand(0)*5000+5000
        do for [i=10000:100000:500] {
            print sprintf("%g %g",i,A*exp(-((real(i)-B)/C)**2))
        }
    set print
    
    a=1; b=50000; c=5000          # give some reasonable starting values
    f(x) = a*exp(-((x-b)/c)**2)
    set fit quiet nolog
    fit f(x) $Data u 1:2 via a,b,c
    
    set multiplot layout 1,2
    
        set format x "%.0s%c"      # set xtics to engineering
        plot $Data u 1:2 w p, \
             f(x) w l lc "red"
    
        set format x "%g"          # set xtics to default
        plot $Data u ($1/1000):2 w p, \
             f(x*1000) w l lc "red"
    
    unset multiplot
    ### end of code
    

    Result:

    enter image description here