Tuesday, November 22, 2011

How to plot nothing using gnuplot

Some times we just want to create a figure with only the axes ticks and so on, no any content (The content may be added later use some other tools). We know that if there is not a plot command, gnuplot will not create a output picture, So play some tricks should be played. In this post I will talk on two methods to plot nothing using gnuplot.

The first one is plot a line with background color.
reset
set term png font ",20" xffffff  #set png terminal and the background color is white
set output "blank.png"
set key off
set ytics 0.5
plot sin(x) with line linecolor rgb"#ffffff"
set output

The second one is plot something out of yrange.
set term png font ",20"
set output "blank.png"
set yrange [-1:1]
set key off
plot 2 with lines
set output
Maybe some other methods also exist, But this three shold be the most used and simplest ones. At last a sample picture posted. (The two methods will just creat pictures the simillar as this one.)
Plot nothing using gnuplot

Tuesday, November 15, 2011

Aviodding the blank margin of a eps picture being auto croped by some softwares

Some one complained to me that when he inserted a gnuplot produced eps picture into some software (not all) the blank margins are auto cropped. And he want to avoid this to happen.
Yes, I also found this problem myself. Some software "intelligently" crop the blank margin of a postscript picture. Always this is just OK. But in some special cases, we set a blank margin of special purpose, for example, to leave some spaces for readers to add some notes. In these cases, the "intelligence" becomes "stupid".
I solve this problem by adding a invisible rectangle (with linewidth equals 0 or with color same as the background color and fillstyle onborder) as large as the screen, i.e.,
set object rectangle from screen 0,0 to screen 1,1 \
    lw 0 fillstyle noborder behind   
Then although there are blank margins but they are not blank in fact. So when the picture is inserted to other software, the margin will not be auto cropped.

Sunday, October 30, 2011

Add value labels to the top of bars in a bar chart

It is easy to plot a bar chart with gnuplot using plot style boxes or histogram. This time we talk about how to add values labels to the top of bars in a bar chart.

The first coming idea is adding labels manually using command "set label ...". This a method but a poor efficient one. A better one is plot the labels with plot style "labels". And this is the method we will talk about here. A sample script is shown below.
reset
set term png font "Times,18"    #set terminal and output file
set output "bar_labels.png"
set xlabel "x value"    #set x and y label
set ylabel "Frequency"
set xrange [-0.5:4.5]    #set x and y range
set yrange [0:4]
set xtics 0,1,4    #set xtics
set style fill solid    #set plot style
set boxwidth 0.5
unset key    #legend not plotted
plot "bar_data.dat" using 1:2 with boxes,\
     "bar_data.dat" using 1:2:2 with labels
#plot bar chart and the value labels on the bars
In this script, we have used a data file (bar_data.dat) like this one
0    2
1    3
2    3
3    2
4    2

Run the script, we get picture file bar_labels.png like the following one.

Gnuplot bar chart with value labels

If you do not like the position of the labels in the upper picture, you can just change it. For example,
lot "bar_data.dat" using 1:2 with boxes,\
     "bar_data.dat" using 1:($2+0.25):2 with labels
will put the labels 0.25 units higher.

Value labels with a 0.25 units higher position

Wednesday, October 19, 2011

Broken axes graph in gnuplot (3)

To plot a x-axes broken graph with datafile, the two methods described in the last two posts are also suitable. The first method can be applied straightforwardly. And now we come to an example to apply the second method to a datafile plotting.

Consider such a case, an experiment team research on a material's thermodynamical properties. They heat it and record the temperature for 13 hours the first day, then its time for they to go home. And they go on their recording the second day. At last they get a data file like the following one.
0.00    25.76 
1.00   29.91 
2.00   35.33 
3.00   38.85 
4.00   43.15 
5.00   47.35 
6.00   50.45 
7.00   53.55 
8.00   56.12 
9.00   57.31 
10.00  59.11 
11.00  62.21 
12.00  62.17 
13.00  63.05 

23.00  63.56 
24.00  62.98 
25.00  66.05 
26.00  65.88 
27.00  64.70 
28.00  65.37 
29.00  63.12 
30.00  65.66 
31.00  66.14 
32.00  64.92 
33.00  65.68 
34.00  64.01 
35.00  63.50
36.00   63.75
We can find that their is a break when they go home. We will use a broken axes graph to deal with this break.
set term post eps enhanced
set output "broken_axes3.png"
set xrange [0:36-10+0.5]
set yrange [25:70]
set border 2+8
set arrow 1 from 0,25 to 13,25 nohead
set arrow 2 from 13.5,25 to 26.5,25 nohead
set arrow 3 from 0,70 to 13,70 nohead
set arrow 4 from 13.5,70 to 26.5,70 nohead

set arrow 5 from 12.75,24 to 13.25,26 nohead
set arrow 6 from 13.25,24 to 13.75,26 nohead
set arrow 7 from 12.75,69 to 13.25,71 nohead
set arrow 8 from 13.25,69 to 13.75,71 nohead

set xtics ("0" 0, "4" 4, "8" 8, "12" 12,\
           "24" 14.5, "28" 18.5, "32" 22.5, "36" 26.5)
set xlabel "Time:t(h)"
set ylabel "Temperature:T({/Symbol \260}C)"
plot "data.dat" u ($1<13. ?$1: $1>13. ?($1-9.5):1/0):2 w lp lw 2 ps 2 notitle
In this script the key point is "u ($1<13 ?$1: $1>13 ?($1-9.5):1/0):2" which transform range [23:36] to range [13.5:26.5] and let range [0:13] uneffected. The followng piture is broken_axes3.png (This png file is converted from broken_axes.eps).

Broken x-axes graph plotted using gnuplot

Sunday, October 16, 2011

Broken axes graph in gnuplot (2)

Last time I said there is a "better but a little tricky" method to plot a broken axes graph. And now let us come to see this method.

If we want to plot function f(x) in range of [a:b] and [c:d] (i.e., a broken axes graph), we can define a new function:
g(x) = f(x)                         a<= x <= b
g(x) = f(x+c-b-dx)              b+dx <= x <= b+dx-c+d
g(x) = 1/0                         other x
where dx is the gap length. We plot this new function and modify xtics to a right one. Then we get a broken x-axes graph which will similar to the one we plotted last time. Let us start work our idea out.
reset
set term png
set output "broken_axes2.png"
#Plot f(x)=sin(x**2/4.) at range [20:22] and [40:42]
f(x)=sin(x**2/4.)
g(x)=20<x && x<22 ?f(x) :22<x && x<22.2 ?1/0 :22.2<x && x<24.2 ?f(x+17.8) :1/0
set xrange [20:24.2]
set yrange [-1:1]
set sample 2000
set border 2+8  #the bottom and top border will be plotted mannually
set arrow 1 from 20,-1 to 22,-1 nohead              #bottom and top border
set arrow 2 from 22.2,-1 to 24.2,-1 nohead
set arrow 3 from 20,1 to 22,1 nohead
set arrow 4 from 22.2,1 to 24.2,1 nohead
set arrow 5 from 21.95,-1.05 to 22.05,-0.95 nohead  #axes broken indication line
set arrow 6 from 22.15,-1.05 to 22.25,-0.95 nohead
set arrow 7 from 21.95,0.95 to 22.05,1.05 nohead
set arrow 8 from 22.15,0.95 to 22.25,1.05 nohead
#Modify xtics to a right value
set xtics ("20" 20,"20.5" 20.5,"21" 21,"21.5" 21.5,"22" 22,\
           "40" 22.2,"40.5" 22.7,"41" 23.2,"41.5" 23.7,"42"24.2)
set xlabel "Time:t(s)"
set ylabel "Signal:U(V)"
plot g(x) w l lw 2 notitle
The picture file broken_axes2.png is shown below.

Broken axes graph plotted by gnuplot

Next time I will put this method to the datafile case.
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.