gnuplot

Overlaying two plots with overlapping ranges


In gnuplot, I would like to plot the exponential function on the range [-4:+4] and the natural logarithm function on the range [0.01:4] on the same plot. I.e., I would like to combine the results of the following two commands into a single plot.

plot [-4:+4] exp(x);
plot [0.01:4] log(x);

Can anyone give me any advice on how to achieve this.

Apologies if this is a duplicate but I couldn't find a question here with an answer that solves my problem. This problem is similar to this earlier question but the solution given for that question only works if the ranges are contiguous intervals.


Solution

  • The shortest answer is probably:

    plot exp(x), log(x)
    

    You're getting just a warning (not an error) warning: Did you try to plot a complex-valued function?, since negativ arguments in log() are special, i.e. complex, but since it is only a warning your graph is plotted nevertheless, apparently for the real values only.
    In order to avoid this warning you could simply do:

    plot [-4:4] exp(x), [0.01:4] log(x)
    

    If you use autoscale, the y-range will go up to 60 because of exp() and if you want to see more details of the log() function you could limit the y-range, e.g [-6:10]. It's worth mentioning that the default sampling rate for functions is 100 (check help functions and help samples. If you want to get more points, i.e. smoother in case of highly oscillating functions or get your log() closer to zero you can set this value higher, e.g. set samples 1000.

    Script:

    ### plot exp and log together
    reset session
    
    set yrange[-6:10]
    set samples 100
    set grid x,y
    
    plot   [-4:4] exp(x), \
         [0.01:4] log(x) ti "log(x)"
    ### end of script
    

    Result:

    enter image description here