1

I want to plot a function u(x) in gnuplot, the function itself looks like this:

u(x) = (D) * pi * x

plot    D = 100.0, u(x) title "A, \
        D = 225.0, u(x) title "B"

The function with the parameter D = 100.0 should have a xrange reaching from 0 to 1000 and the plot with the parameter D = 225.0 should have a xrange from 0 to 2000.

I have tried the obvious solution

u(x) = (D) * pi * x

plot    [0:1000] D = 100.0, u(x) title "A, \
        [0:2000] D = 225.0, u(x) title "B"

This attempt generates three graphs, including a graph with a constant value of 225.0

Best Regards!

2 Answers 2

2

As always, I strongly recommend not to use axis range specifiers in a plot command. They are often ambiguous and do not achieve what you think they will. For one thing, only the first one in the command is specifically taken to override xrange. Subsequent one are instead applied to other axes or to sampling.

The most general way to specify separate ranges for different plot components is to use the pseudofile '+' and provide sampling ranges rather than axis ranges. In your case this would become:

u(x) = (D) * pi * x
plot    D=100., [0:1000] '+' using 1:(u(x)) title "A", \
        D = 225.0, [0:2000] '+' using 1:(u(x)) title "B"

enter image description here

Things to note:

  • The definitions D=<value> do not have a sampling range
  • You can if you want specify a sampling interval to go with each range. For example [1:1000:5] would sample every 5 on x to yield 200 sampled values.
  • This particular plot ends up with an xrange of [0:2000] because it defaults to autoscaling. You could set this separately beforehand, e.g. `set xrange [-100:2500], and the explicit sampling ranges in the plot command would control which portion of the full x range was drawn into. See the online demo piecewise functions
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the explanations! Ok, I see using [xmin:xmax] '+' ... seems to be the intended way. This is somewhat "hidden" in the documenation: help plot --> range --> sampling. An example directly under help plot would be nice. Furthermore, I thought plot D=100, sin(x) would be the same as D=100; plot D, sin(x), but apparently it is not. But plot (D=100), sin(x) is like the second and plot D=100 sin(x) like the first (mind: without comma, which as I read the documentation shouldn't be correct syntax?!). Good to know.
0

edit: @Ethan's answer is the intended way to go!

Welome to StackOverflow! Plot elements are separated by comma (check help plot). That's why you are plotting two horizontal lines at 100 and 225 and twice the function u(x). (apparently wrong! see my comment under Ethan's answer)

These ranges are also not fully clear to me. According to documentation for each plot-element you should be able to add a range. But apparently, it looks like you have first to plot the function with the larger range. So far, I did not succeed the other way round. Maybe there is a solution which I am not aware of.

Furthermore, you can add your parameter D as function input u(x,D).

Script:

### functions with different parameters and ranges
reset session

u(x,D) = D * pi * x

set xrange[0:2000]
set key invert

plot [0:2000] u(x,225) title "B" lc "blue", \
     [0:1000] u(x,100) title "A" lc "red"
### end of script

Result:

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.