How can I plot data points and a function with different x ranges in gnuplot?

  • Thread starter Thread starter sketos
  • Start date Start date
  • Tags Tags
    Gnuplot Range
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
5 replies · 13K views
sketos
Messages
55
Reaction score
0
Hello,

How can i plot some data points in gnuplot and in the same plot a function, for example
f(x)=0.2*x**(-0.6)
plot [0:6] "data.dat" with errorbars, [0:2] f(x)
 
Physics news on Phys.org
sketos said:
Hello,

How can i plot some data points in gnuplot and in the same plot a function, for example
f(x)=0.2*x**(-0.6)
plot [0:6] "data.dat" with errorbars, [0:2] f(x)
Do you want x=2 on your f(x) plot to correspond to x=2 or x=6 on your data plot?

If it's the former, just plot it normally. The data plot will fill the plot, while the plot of f(x) will only occupy in the leftmost third of the plot.

If it's the latter you are asking for, define an xrange to be used for your data plot and an x2range to be used for your f(x) plot. Then plot with plot "data.dat" axes x1y1 with error bars, f(x) axes x2y1. If you want, you can make a separate y2range for your f(x), too. Be careful, though. Plots are often at their best when they are simple.
 
  • Like
Likes   Reactions: 1 person
i tried your method by setting different x1,x2 axes but then a new problem arrises. Somehow the function f(x) dispaces.
What i am trying to do is fit a power function to the data , but not all of them. When i do

set xrange [0:6]
plot "data.dat" with errorbars,f(x)

the plot looks good but when i use
set xrange [0:6]
set x2range [0:1]

plot "data.dat" axes x1y1 with errorbars, f(x) axes x2y1

the function takes other values at y-axis , i can explain why!
thanks in advance and for the reply!
 
when i say it looks good i mean it fits good with the data but extends in the whole x-range, something that i don't want to happen
 
sketos said:
i tried your method by setting different x1,x2 axes but then a new problem arrises. Somehow the function f(x) dispaces.
Of course it does. That's the whole point of setting an x2range.

You did not try my method because the very first step in my method was for you to answer my question Do you want x=2 on your f(x) plot to correspond to x=2 or x=6 on your data plot? It also appears you did not read the gnu plot documentation on what x2range does. Read the fine documentation! Always!

It appears you want only one x axis, not two, but you want your f(x) only to be plotted for values of x between 0 and 2. You cannot use plot [0:6] "data.dat", [0:2] f(x). It's illegal syntax. The range applies to all entities to be plotted.

What you can do is define another function, call it g(x). g(x) is one for x between 0 and 2, undefined for x>2. Then f(x)*g(x) is just f(x) for x≤2, but is undefined (and hence not plotted) for x>2.
Code:
g(x)=( (x<=2)? 1.0 : (1/0) )
plot [0:6] "data.dat" with errorbars, f(x)*g(x)
 
  • Like
Likes   Reactions: 1 person