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
Click For Summary

Discussion Overview

The discussion focuses on plotting data points and a function with different x ranges in gnuplot. Participants explore various methods to achieve this while addressing issues related to axis ranges and function behavior in the plots.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant asks how to plot data points and a function simultaneously in gnuplot, providing an example function.
  • Another participant questions the clarity of the x ranges and suggests that the user clarify whether the x=2 on the function should correspond to x=2 or x=6 on the data plot.
  • A participant proposes using separate x1 and x2 axes to plot the data and function, but notes that this can lead to the function appearing displaced.
  • Concerns are raised about the function extending over the entire x-range when it should only fit a portion of the data.
  • A later reply emphasizes that using x2range alters the function's placement and suggests defining a new function to restrict the plotting range of f(x) to x≤2.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to plotting with different x ranges. There is no consensus on a single method, and some participants challenge the understanding of gnuplot's syntax and functionality.

Contextual Notes

Participants mention limitations related to the syntax of gnuplot and the implications of using separate x ranges. There are unresolved questions about the desired correspondence between the function and data plots.

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   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

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K