Showing posts with label gradient color. Show all posts
Showing posts with label gradient color. Show all posts

Friday, September 9, 2011

Gradient colored curve in Gnuplot(2)

In the previous article we plot a gradient colored curve by expanding it to a 3-d surface and view the surface in a particular viewpoint. Today we talk about a third method.

In gnuplot we can specify the plotting colors by "rgbcolor variable". It can assign a separate color for each data point,line segment,or label based on additional information in the input data file. For example "plot 'data.dat' u 1:2:3 with points lc rgb variable" will plot each data point with separate color according to the third column of the data file. Now we come to plot the following data file using "rgbcolor variable".
0.0 	0.00 
0.2 	0.14 
0.4 	0.56 
0.6 	1.25 
0.8 	2.12 
1.0 	3.00 
1.2 	3.65 
1.4 	3.90 
1.6 	3.79 
1.8 	3.64 
2.0 	4.00 
2.2 	5.40 
2.4 	8.10 
2.6 	11.84 
2.8 	15.84 
3.0 	19.00 
3.2 	20.41 
3.4 	19.78 
3.6 	17.78 
3.8 	15.91 
4.0 	16.00 
4.2 	19.42 
4.4 	26.39 
4.6 	35.66 
4.8 	44.78 
5.0 	51.00 
5.2 	52.40 
5.4 	48.90 
5.6 	42.54 
5.8 	36.95 
6.0 	36.00 
6.2 	42.21 
6.4 	55.46 
6.6 	72.72 
6.8 	88.97 
7.0 	99.00 
7.2 	99.63 
7.4 	91.26 
7.6 	78.06 
7.8 	66.75 
8.0 	64.00 
8.2 	73.76 
8.4 	95.28 
8.6 	123.02 
8.8 	148.39 
9.0 	163.00 
9.2 	162.10 
9.4 	146.85 
9.6 	124.35 
9.8 	105.31 
Our plotting script is:
reset
plot "data.dat" #To get the max and min vaule
MAX=GPVAL_Y_MAX
MIN=GPVAL_Y_MIN
LEN=MAX-MIN
set term png
set output "gradient_colored_curve3.png"
set key off
plot "data.dat" w l lw 2 lc rgb"green" smooth csplines, \
     "data.dat" u 1:2:(255-($2-MIN)/LEN*255) w p pt 7 \
     ps 2 lc rgb variable notitle
#(255-($2-MIN)/LEN*255) means when data vary from min
#to max, color vary from blue to black
It is a simple and good method. But it is hard for us to design a beautiful color palette. This is its disadvantage.

At last is what does the picture gradient_colored_curve3.png looks like.

Gradient colored curve plotted by gnuplot

Gradient colored curve in Gnuplot(1)

Last time we talked about how to draw a gradient colored curve. We realized it by cutting the total curve into n segment and plot the n segment using different colors. This time we talk about another method.

We know that at a special viewpoint a surface can appear like a line. We also know that gunplot can plot colored surface with pm3d plotting style. So we may expand our line to a surface. Plot the surface with pm3d. View it in a special viewpoint. Now we come to realize our idea.
reset
set term png
set output "gradient_colored_curve2.png"
set isosample 200,2 
#As along y-axis the surface has a constant value,
#the samples along y-axis can set to be a small value.
set palette rgbformulae 7,5,15
unset colorbox
set xyplane relative 0
set xtics offset 0,-1   
#putting xtics label 1 character lower makes the plotting nicer
unset ytics #ytics is not necessary
unset border #the border will plot manually
#plot axes manually
set arrow 1 from graph 0,0,0 to graph 1.1,0,0
set arrow 2 from graph 0,0,0 to graph 0,0,1.1
#plot xtics manually
set for [i=-10:10:5] arrow from i,0,-1.0 to i,0,-0.95 nohead
set view 89.5,0 #map the 3-d surface to coordinate plane
splot sin(x) w pm3d notitle
Note that the view should be set to a value near "90,0" but not exactly "90,0" (if view exactly equals "90,0", "linwidth" of the curve is 0), here we choose "89.5,0". Compared to the previous one, this method takes less time. The output picture file is shown below.

Gradient colored curve plotted by gnuplot

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.

Thursday, September 1, 2011

Gnuplot advanced background color (1)

An alternative way to realize a linear gradient colored background is using the image plotting style. Let us first look at our final script and then explain it.
reset
set table "back.dat"	#creat a data file
set isosample 2,200	#containing linear
splot y			#gradient color 
unset table		#information

splot y	#Get the max value of z
max=GPVAL_Z_MAX

set term png
set output "backgradient2.png"
set multiplot
#plot the background
set lmargin at screen 0
set rmargin at screen 1
set bmargin at screen 0
set tmargin at screen 1
unset border
unset tics
set palette defined(0"white",max"#ccffcc")  #set gradient color
plot "back.dat" w ima
#plot the curve of sin(x)
set border 1+2+4+8
set tics
set lmargin
set rmargin
set bmargin
set tmargin
plot sin(x) w l lw 2 lc rgb"black" notitle
unset multiplot
!del back.dat
#in unix system the above command may should be change to "!rm back.dat"
In this script, firstly we produce a date file "back.dat" which contain linear gradient color information. Then in a multiplot environment we plot the background using command '''plot "back.dat" w ima''' and figure of sin(x) using '''plot sin(x) w l lw 2 lc rgb"black" notitle'''. Before plotting the background we set l,r,b,tmargin to be 0,1,0,1 respectively, thus the background is filled the whole picture, and border and tics are unset. After that all of border,margins and tics are set again. "! del back.dat" is used to delete temporary data file back.dat. backgradient2.png is show below:

Linear gradient colored background
 

Wednesday, August 31, 2011

Gnuplot advanced background color (0)

It is not so hard to plot a single color background graph using gnuplot, and we have done this in blog ---- simple background color. But how about a linear gradient colored background?

The fill color of a object can not be set to be gradient in gnuplot. So the method in simple background color can not be copied, at least can not be copied directly. But if we draw a set
of small rectangles with different color, it may have the same appearence as a large rectangle with linear gradient color. Now let us realize this idea.
reset
n=200   #number of rectangles
dx=1.0/n    #legth of rectangle
set palette defined(0"white",n"#ccffcc")  #set gradient color
set cbrange [0:n]
unset colorbox
set for [i=1:n] object i rectangle from screen (i-1)*dx,0 to \
screen i*dx+dx/2,1 fs solid 1.0 noborder fillcolor palette cb i \
behind
#draw a set of rectangle with diffrent color."+dx/2" makes the
#adjacent rectangles have a common part, so that the border is not
#so obvious
set term png
set output "backgradient1.png"
plot sin(x) w l lw 0 lc palette cb 0 notitle,sin(x) w l lw 2 lc \
rgb"blue" notitle
#The first plot is just used to enable the palette, and it will be
#covered by the second plot never appearing on the output picture.
set output
And the above commands produce the following graph:

Linear gradient colored background
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.