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
AI Thread Summary
To plot data points and a function with different x ranges in gnuplot, it's essential to clarify whether the function's x-values should correspond to the data's x-values. If the function should only be plotted for a specific range, define a new function that is valid only within that range, such as g(x) which equals 1 for x ≤ 2 and undefined otherwise. The syntax for plotting must be correct; using a single range for all entities is necessary. The suggested approach is to plot the data with error bars and multiply the function by g(x) to restrict its display. This method ensures that the function only appears where desired, avoiding unwanted extensions across the entire x-range.
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
The same way you normally plot - what's the problem?
(the meaning of your ranges is not clear though)
 
Last edited:
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 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 1 person

Similar threads

Replies
8
Views
2K
Replies
1
Views
2K
Replies
5
Views
2K
Replies
1
Views
4K
Replies
1
Views
3K
Replies
4
Views
1K
Replies
5
Views
5K
Back
Top