Handling/CompAlgSystems4GolemUpToShotNo31150/gnuplot/index
Table of Contents
1 Introduction
gnuplot1 is a multiplatform plotting utility with a command-line interface designed for plotting of scientific data. It offers several output methods, e.g. PNG, JPEG, LaTeX.
For obtaining the data from the web server a different method must be used, as gnuplot cannot obtain the data itself.
2 Required software
3 Example script
There are two scripts
- the first one tells gnuplot how to plot the data
- the second one is a *NIX shell script for downloading the data and running gnuplot
Uloop_dp.csv
.
#!/usr/bin/gnuplot
#all lines starting with '#' are comments and are disregarded by the program
#the first line tells a *NIX shell which program should interpret this file
set xlabel "Time [ms]" #set the X-axis label
set ylabel "Loop voltage [V]" #set the Y-axis label
set yrange [0:] #set the Y-axis limit from 0 to data maximum
set datafile separator ";" #set the character separating x values from y values
set title "Loop voltage evolution" #set the figure title
plot "Uloop_dp.csv" using 1:2 title "Loop voltage" with lines linestyle -1
#plot the contents of the file using the first column as x data and second column as y data
# with a label Loop voltage
# with lines, lines are solid and black (gnuplot command 'test' shows other styles
This script can be saved as e.g. plot-data.plt
The second script downloads the data to the current directory and then calls gnuplot to plot them
#!/bin/sh
# comments start with '#'
#the first line says which program should process this script
shot=6231 #discharge number
data_directory=basicdiagn #name of the directory containing the data
data_file="Uloop_dp.csv" #name of the data file
wget "http://golem.fjfi.cvut.cz/shots/${shot}/${data_directory}/${data_file}" #download the data
gnuplot plot-data.plt #call gnuplot and tell him to interpret that file
Save the script as e.g. plot-script.sh
and run it in a command prompt as
user@host$ sh plot-script.sh
Or on *NIX systems, the script can be set to be executable and then the program specified on the first line of the script is automatically called to process it
user@host$ chmod +x plot-script.sh #set the executable bit
user@host$ plot-script.sh #now the script can be run as a stand-alone program
As it now behaves as a program, it should be possible just to simply open in your file manager by (double-)clicking it.
Most shells have also an interactive mode, usually started by simply running sh
in the command prompt. Then the above shell code can be directly copied and pasted into the shell prompt.
3.1 Advanced dynamic script
It is of course possible to merge the two scripts into one. This approach is based on piping the commands to the standard input of the gnuplot program.
#!/bin/sh
# comments start with '#'
#the first line says which program should process this script
shot=6231 #discharge number
data_directory=basicdiagn #name of the directory containing the data
data_file="Uloop_dp.csv" #name of the data file
wget "http://golem.fjfi.cvut.cz/shots/${shot}/${data_directory}/${data_file}" #download the data
#the following lines end with a backslash, so they are chained together and behave like one line
#therefore, there must bu no comment on those lines !
# the ; separates commands because they are on "one line"
echo 'set xlabel "Time [ms]";\
set ylabel "Loop voltage [V]";\
set yrange [0:];\
set datafile separator ";";\
set title "Loop voltage evolution";\
plot "Uloop_dp.csv" using 1:2 title "Loop voltage" with lines linestyle -1' | gnuplot --persist
#pipe into gnuplot and tell him to keep the window open
Again, this code can be either saved to a file and run in the command prompt, or copied and pasted directly into the shell prompt.
3.2 Command-line parameters
To make the script fit for automatic looping it should be able to process command-line parameters. This is done by changing only a few lines in the above script.
#<--SNIP-->
shot=$1 #the shot number will be the first command-line parameters
data_directory=$2 #the name of the directory containing the data will be the second parameter
data_file=$3 #and finally, the third command-line parameter will be the name of the file
#<--SNIP-->
The script can now be run in the command prompt as
user@host$ plot-script.sh 6231 basicdiagn Uloop_dp.csv #provided the executable bit is set
4 Using the interactive shell prompt
The gnuplot program can be also run in an interactive mode by simply running gnuplot
in the command prompt. Then the above code in gnuplot-plot.plt
can be directly copied and pasted into the gnuplot prompt as they are regular commands.
5 Further resources
- documentation on project homepages referenced in section Required software
- Advanced Bash-Scripting Guide for a better understanding of the shell scripts
- documentation accessible in the gnuplot interactive prompt through the
help
command
Footnotes:
1 Its name is gnuplot
and has nothing to do with the GNU project, except for the open-source GPL-2 license