Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting smooth curve with gnuplot

Tags:

gnuplot

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

like image 380
Kartik Chhajed Avatar asked May 05 '26 03:05

Kartik Chhajed


1 Answers

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.

like image 145
Matthew Avatar answered May 09 '26 02:05

Matthew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!