Wednesday, September 7, 2011

Gradient colored curve in Gnuplot(0)

A friend of mine asked me:"How can we plot a figure like this one (the following one) using gnuplot?"

Gradient colored curve

It is a bit hard. We know that command
plot function-or-datafile with lines linecolor rgb"rgb-color"
can plot a curve with specific color, but the color can not vary along the curve. How to deal with this kind of picture? I thought for a while and answered:"I have three different methods. Let me tell you one by one, and you can select the one which you like most."

Today I will introduce my first method.

The idea is simply cutting the curve into n segments, and plot these n sub-curves with different colors. It is realized by the following gnuplot script.
reset
set term png
set output "gradient_colored_curve1.png"
f(x)=exp(-0.33*x)*sin(5*pi*x)   #The function to be plotted
n=100   #divide the whole curve into n segments
set samples 300 #must be large enough for each segment have at least 2 samples
set palette rgbformulae 30,13,10    #rainbow palette
set cbrange [0:n]
set xrange [0:10]
unset colorbox  #The colorbox will not plotted
#Plot the n segments of the curve
plot for [i=0:n] i<=x*n/10 && x*n/10<(i+1.5) ?f(x):1/0 \
     w l lw 2 lc palette cb i notitle
In the script it is "i+1.5" not "i+1", because we want two segments close to each other have a common part. In such a way the boundary between them will not so obvious. And be sure that the samples should be set to a value large enough for each small segment have at least 2 samples. The advantage of this method is its simplicity. The disadvantage is that since this script will plot for n times, it will take a very long time (on my computer about 1 second). This script just produce a picture the same as the one my friend shown me.

No comments:

Post a Comment

Creative Commons License
Except as otherwise noted, the content of this page is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.