3

I'm not getting smooth curve on gnuplot.

This is my code:

set style line 3 lc rgb '#09ad00' lt 1 lw 1.5 #green
set style line 1 lc rgb '#0060ad' lt 1 lw 2  #blue 
set style line 2 lc rgb '#dd181f' lt 1 lw 2  #red
plot [-1:1] f1(x) with line ls 3,f2(x) with line ls 1,f1(x)+f2(x) with line ls 2

I'm getting this plot

zig-zag curve no smooth curve

while I'm expecting this type of curve

smooth curve no zig-zag curve

1
  • Add a line set samples 200 Commented Aug 13, 2017 at 20:00

1 Answer 1

3

You haven't shown what your particular functions are, but this is almost certainly a sampling problem. Gnuplot doesn't really draw curves for functions - it actually computes the functions at multiple points and connects them with straight lines, similarly to what would happen if you were plotting a data file. The number of points that it computes is user settable.

Suppose that I do plot sin(x) and see this: Jagged sine curve

Here the sampling rate is set pretty low. We can look at the individual points in order to see what is going on.

Jagged sine curve with points

In order to improve this, I need to increase the sampling rate by using the set samples ? command. The default is 100 (in 5.0 patch level 6). Depending on how rapidly the function changes, higher values may be needed. I usually set it to around 1000 with set samples 1000. This changes the graph to

Smooth sine curve

which produces a much nicer smooth curve. Again, this is just a bunch of points connected by straight lines, but when there are a lot of these, it looks like a smooth continuous curve.

We can look at the individual points again (using a sampling rate of 100 as 1000 is too many to clearly see the points)

Smooth sine curve with points

We can also see here that there is not much difference between the graph with 1000 points and 100 points. In the case of a sine curve, 100 is enough to see a smooth graph, but with a faster changing curve, we may need more.

The set samples command takes (optionally) two values, but the second value is only used for 3d plots. You can find out more with the help samples command.

Sign up to request clarification or add additional context in comments.

2 Comments

Gnuplot's default (relatively low) number of samples is possibly related to the age of the program (read SLOW computers). I recommend to place a set samples 1001 command in $HOME/.gnuplot, also as a rule of thumb the number of samples could be around 30*number of maxima. Very good answer.
@gboffi I haven't looked at the historical values to see if that default has changed from earlier versions, but yes, you are almost certainly right about why it is so low.

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.