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

In summary, to plot data points and a function on the same plot in gnuplot, you can use the syntax "plot [x1:x2] "data.dat" with errorbars, [x3:x4] f(x)" where x1 and x2 define the range for the data plot and x3 and x4 define the range for the function plot. If you want the function to only be plotted for a certain range on the x-axis, you can define a new function that is only defined within that range and multiply it by the original function. It is important to carefully read the gnuplot documentation and to always keep in mind the syntax for plotting multiple entities on the same plot.
  • #1
sketos
56
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
  • #2
The same way you normally plot - what's the problem?
(the meaning of your ranges is not clear though)
 
Last edited:
  • #3
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
  • #4
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!
 
  • #5
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
 
  • #6
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

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

What is the purpose of setting a different x range in gnuplot?

Setting a different x range in gnuplot allows you to adjust the visible range of your data on the x-axis. This can be helpful when you have a large dataset and want to focus on a specific portion of it, or when you want to zoom in on a particular area of interest.

How do I set a different x range in gnuplot?

To set a different x range in gnuplot, you can use the "set xrange" command followed by the desired range. For example, "set xrange [0:10]" would set the x range from 0 to 10. You can also use this command to specify a range using variables or functions.

Can I set a different x range for each plot in a multiplot in gnuplot?

Yes, you can set a different x range for each plot in a multiplot by using the "set xrange" command before each plot. You can also use the "set multiplot layout" command to specify the number of rows and columns in your multiplot, and then use the "set origin" and "set size" commands to adjust the position and size of each plot.

What happens if I set a different x range that is outside of my data range in gnuplot?

If you set a different x range that is outside of your data range in gnuplot, the plot will still be displayed, but the portion of the plot that falls outside of the data range will be empty. This can be useful if you want to include blank space on your plot for annotation or labeling.

Can I reset the x range in gnuplot back to the default setting?

Yes, you can reset the x range in gnuplot back to the default setting by using the command "set xrange restore". This will restore the x range to the full range of your data as it was before any changes were made.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
905
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • Computing and Technology
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
Back
Top