Monday, June 4, 2012

Plot an interactive svg graph using gnuplot

In this post an example of plotting an interactive svg graph using gnuplot is talked.

First, run the following gnuplot plotting script.

set term svg mouse jsdir "./js"  
set output "sincos.svg"
plot sin(x) w l lc rgb"#ff0000", cos(x) w l lc rgb"#00ff00"
set output

And a file called "sincos.svg" is plotted. As we specified in the script that the javascript file is in directory "./js", "gnuplot-dir/share/js" have to be copied to the current directory (windows operating system, and if you use linux maybe the directory should be /usr/local/share/gnuplot/gnuplot-version/js). This time open the file, and you can find when you click the graph, the coordinate of the current point will be displayed. And when you click the legend, the cooresponding line will be invisible, and click it again, the line will visible.

The jsdir can also be a url location. And this is is usually appropriate if you are embedding the svg into a web page. For example, command --- set term svg mouse jsdir "http://gnuplot.sourceforge.net/demo_svg_4.6/" can be used.

set term svg mouse jsdir "http://gnuplot.sourceforge.net/demo_svg_4.6/"
set output "sincos.svg"
plot sin(x) w l lc rgb"#ff0000", cos(x) w l lc rgb"#00ff00"
set output

Saturday, May 19, 2012

How to pick out the maximum and minimum points when plotting with gnuplot?

Picking out the maximum and minimum points from a data file may be useful when you want to make some explanation to your graph. It is known that GPVAL_Y_MAX (GPVAL_Y_MIN) is the maximum (minimum) value. But what is the corresponding X coordinate?

With Gnuplot 4.6 the both the x and y coordinate of maximum and minimum points can be find out easily. The method is using new command "stats". This command is used for statistic. When it is run, some statistical results will be gotten. If your data file contains two column of data, (STATS_pos_max_y, STATS_max_y) will be the coordinate of the maximum point and (STATS_pos_min_y, STATS_min_y) will be the coordinate of the minimum point.

For example, we have a data file named "data.dat" like this

0.1     0.289010715
0.2     0.050630492
0.3     0.721247895
0.4     0.284271151
0.5     0.505051253
0.6     0.101819025
0.7     0.008466133
0.8     0.36249047
0.9     0.487576233
1.0     0.595090343
1.1     0.865255938
1.2     0.696628854
1.3     0.505899456
1.4     0.338131983
1.5     0.108034045

Run a gnuplot script as follows

reset
set term png
set output "max_min.png"
stats "data.dat" u 1:2 nooutput
set xrange [STATS_min_x:STATS_max_x]
set label 1 "Maximun" at STATS_pos_max_y, STATS_max_y offset 1,-0.5
set label 2 "Minimun" at STATS_pos_min_y, STATS_min_y offset 1,0.5
plot "data.dat" w p pt 3 lc rgb"#ff0000" notitle, \
     STATS_min_y w l lc rgb"#00ffff" notitle, \
     STATS_max_y w l lc rgb"#00ffff" notitle
set output

And you will get a graph like the following one.
Picking out the maximum and minimum points when plotting using gnuplot

Monday, April 2, 2012

New version of gnuplot makes iterations easy --- animation as an example

Gnuplot4.6 have been released on 2012.03.08. And some new features have been added. Among them, I think block-structured if/else/while/do is the most useful one. With this new feature, one can easily deal with loop. In this post I will talk about this new feature taking animation as an example. In a previous post, how to create a gif animation using gnuplot have been talked. And it seems a little complex. An extra file is needed. Here with this new feature in gnuplot4.6, the work can be done simply as follows:

reset
set term gif animate
set output "animate.gif"
n=24 #n frames
dt=2*pi/n
set xrange [0:4*pi]
set yrange [-1:1]
do for [i=0:n]{
  plot sin(x+i*dt)/(1. + i/12.) w l lw 1.5 title sprintf("t=%i",i)
}
set output
Animation using new fearute of gnuplot4.6
 



Friday, March 16, 2012

The diffrence among various forms of quotation marks

There are three different forms of quotation marks in gnuplot. They are single quote, double quote and backquote.

The main difference between sing and double quote marks is whether backslash processing of special characters can be used. In double quote the backslash pattern is effective, so if you type in the following command
print "The first line.\nThe second line."
Then it will comes out to be
The first line.
The second line.
While if you type in the command like this:
print 'The first line.\nThe second line.'
Then the out coming string will be
The first line.\nThe second line.

The back quotes have a absolutely different utility. It is used to enclose system commands for substitution into the command lines. (Note that this utility is not supported on all platforms.) For example in a Linux platform you can add a watermark to your graph like this:

set label "By gnuplot-surprising at `date +%Y-%m-%d`" center\
    at screen 0.5,0.5 tc rgb"#cccccc"
plot sin(x)

The watermarked picture looks like this
Back quotes in gnuplot --- substitution

Friday, February 17, 2012

How to plot a cross shaped axis when using gnuplot

It is always asked by my friends that how to plot a graph with cross shaped axis when using gnuplot. To the best of my knowledge, there is not such a built in axis style in gnuplot. So you have to draw the axis manually. Here a script is provided. You can just use it. If you would like to make some improvements, It is welcomed.

#frame.gnuplot
#Author: 数声风笛离亭晚,我想潇湘君想秦
##########################################################
#Utility: plot a "+" style axis

#Usage:
#call "frame.gnuplot" "ox" "oy" "xmin" "xmax" "ymin"\
#     "ymax" "dx" "dy"

#Parameters: 
#(Frame_ox,Frame_oy):     The position of the origin
#(Frame_xmin,Frame_ymin): The bottum-left point
#(Frame_xmax,Frame_ymax): The top-right point
#Frame_dx:                The interval of x-ticks
#Frame_dy:                The interval of y-ticks

#Before calling this script, Frame_xtic_len and 
#Frame_ytic_len can be set to control the length
#of the corresponding tick length. The length is 
#measured in graph coordinate. The default values 
#are 0.02 and 0.01.

#Example:
#Frame_xtic_len = 0.01
#Frame_ytic_len = 0.01
#call "frame.gnuplot" "0." "1.0" "-10." "10." "0." \
#     "2." "2." "0.2"
#plot sin(x)+1.
##########################################################
Frame_ox=$0
Frame_oy=$1
Frame_xmin=$2
Frame_xmax=$3
Frame_ymin=$4
Frame_ymax=$5
Frame_dx=$6
Frame_dy=$7

#set the default value of Frame_x(y)tic_len
if (!exist("Frame_xtic_len")) Frame_xtic_len=0.02
if (!exist("Frame_ytic_len")) Frame_ytic_len=0.01
#no plot of the default border and tics
set border 0
unset tics
#set the xrange and yrange to the user defined value
set xrange [Frame_xmin:Frame_xmax]
set yrange [Frame_ymin:Frame_ymax]
#draw the axis
set arrow from graph 0, first Frame_oy to graph 1.03,\
    first Frame_oy size graph 0.02,15,60 fill ls -1
set arrow from first Frame_ox, graph 0 to first Frame_ox,\
    graph 1.03 size graph 0.015,15,60 fill ls -1
#draw the ticks
set for [i=0:int((Frame_xmax-Frame_xmin)/Frame_dx)] \
    arrow from Frame_xmin+i*Frame_dx,Frame_oy to \
    Frame_xmin+i*Frame_dx,Frame_oy+ \
    (Frame_ymax - Frame_ymin)*Frame_xtic_len nohead
set for [i=0:int((Frame_ymax-Frame_ymin)/Frame_dy)] \
    arrow from Frame_ox,Frame_ymin+i*Frame_dy to \
    Frame_ox+(Frame_xmax - Frame_xmin)*Frame_ytic_len, \
    Frame_ymin+i*Frame_dy nohead
#draw the labels with the ticks
set for [i=0:int((Frame_xmax-Frame_xmin)/Frame_dx)] \
    label sprintf("%3.1f",Frame_xmin+i*Frame_dx) at \
    Frame_xmin+i*Frame_dx,Frame_oy center offset 0,-0.75
set for [i=0:int((Frame_ymax-Frame_ymin)/Frame_dy)] \
    label sprintf("%3.1f",Frame_ymin+i*Frame_dy) at \
    Frame_ox,Frame_ymin+i*Frame_dy right offset -0.75,0

Now let us show how this script is used by an example.

Frame_xtic_len = 0.01
Frame_ytic_len = 0.01
call "frame.gnuplot" "0." "1.0" "-10." "10." "0." \
     "2." "2." "0.2"
plot sin(x)+1. notitle

Fig.1 Cross shaped axis plotted by gnuplot


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.