# Lab 06 - Advanced plotting

Lab 06 (Advanced plotting) and Lab 07 (Test) will be switched.

So this is actually Lab 07.

## Objectives

• Conditional plotting
• Time-based data when plotting in gnuplot
• Advanced plotting concepts: Histograms, animations, heatmaps, three-dimensional plots
• Insertion of graphics in the .tex file

## Introduction

A quick plot is enough when you are exploring a data set or a function. But when you present your results to others you need to prepare the plots much more carefully so that they give the information to someone who does not know all the background you do.

Using PostScript plots with LaTeX

1. Make sure all the individual image files are properly trimmed EPS files.
2. Create a LaTeX document.
3. Process this document using LaTeX.
4. Use the dvips utility with the -E flag to turn the resulting DVI file into Encapsulated PostScript.

## Cheatsheet

scatter plot:
plot ’dataset.txt’ using 1:2
plot ’dataset.txt’ using 1:2 with points

example for the short format:
p ’dataset.txt’ u 1:2 w p pt 1 lt 2 lw 2
notitle

line plot:
plot ’dataset.txt’ using 1:2 with lines

multiple data series:
use replot or separate by commas
plot ’dataset.txt’ using 1:2, ’data.csv’ using 1:3

set key:
plot ’dataset.txt’ using 1:2 title "key"

## Tutorial

Datafile: dataset.txt

### 01. [1.5p] Conditional plotting

Q: How to plot col(A):col(B) if col(C) == 0?
A: plot ‘dataset.txt’ using 1:($3==0?$2:1/0)

Q: What if we want to plot conditionally on another column containing text?
A: plot ‘dataset.txt’ using 1:(stringcolumn(3) eq “a”? $2:1/0) title “a” lc rgb “blue” ,\ “” using 1:(stringcolumn(3) eq “b”?$2:1/0) title “b” lc rgb “red”

### 02. [1.5p] Stats (statistical summary)

Q: What is ‘stats’?
Syntax:
stats 'filename' [using N[:M]] [name 'prefix'] [[no]output]]
Example:
stats "dataset.txt" using 2 name "A"

Q: How to draw a rectangle with a colored border in gnuplot?
Syntax:
set object <index> rectangle
{from <position> {to|rto} <position> |
center <position> size <w>,<h> |
at <position> size <w>,<h>}

# Force the entire area enclosed by the axes to have background color cyan
set object 1 rect from graph 0, graph 0 to graph 1, graph 1 back
set object 1 rect fc rgb "cyan" fillstyle solid 1.0

# Position a red square with lower left at 0,0 and upper right at 2,3
set object 2 rect from 0,0 to 2,3 fc lt 1

# Position an empty rectangle (no fill) with a blue border
set object 3 rect from 0,0 to 2,3 fs empty border rgb "blue"

# Return fill and color to the default style but leave vertices unchanged
set object 2 rect default

### 03. [1.5p] Time-based data when plotting in gnuplot

timecolumn() function parses a time string from a column according to the set timefmt settings. The result is a time stamp in seconds.

set xdata time
set timefmt "%H:%S"
set format x "%H"
set style fill solid 0.6 border -1
set boxwidth 0.3 relative
set xrange["00:00":"23:30"]
set style data boxes plot 'dataset.txt' using 1:2, \ '' using (timecolumn(1)+60*20):($2*0.5), \ '' using (timecolumn(1)+60*40):($2*0.7)

### 04. [1.5p] Animations

• Animations in Gnuplot can be built using “do for []{…}”;
• Set a circle in the centre: set object circle at …;
#example_1
plot 'dataset.txt' every I:J:K:L:M:N
plot 'dataset.txt' every 2		# Plot every 2 lines
plot 'dataset.txt' every ::3	        # Plot starting from the 3rd line
plot 'dataset.txt' every ::3::15	# Plot lines 3-15

#example_2
do for [t=0:50] {
outfile = sprintf('animation/bessel%03.0f.png',t)
set output outfile
splot u*sin(v),u*cos(v),bessel(u,t/50.0) w pm3d ls 1
pause 0.02
}

### 05. [1.5p] Heatmaps

There are two different ways to draw heatmaps with gnuplot:

• Plotting 'with image': This is the best option if no interpolation is required (like it can be done with pm3d) and the X-value and Y-values are evenly distributed (otherwise it does not work).
• Plotting with 'pm3d' and 'splot': Pm3d is a splot style for drawing palette-mapped 3d and 4d data as color/gray maps and surfaces. It uses an algorithm that allows plotting gridded as well as non-gridded data without preprocessing. This is by far the most flexible and advanced plotting style. But it draws every 'pixel' as a coloured polygon, which can also result in huge output files.

Palette is a color storage for use by pm3d, filled color contours or polygons, color histograms, color gradient background.

set palette (i.e. without options): sets up the default values
set palette gray switches to a gray only palette
set palette rgbformulae, set palette defined, set palette file and set palette functionsswitch to a color mapping.
set palette color is an easy way to switch back from the gray palette to the last color mapping.

Interpolate: The option interpolate m,n will interpolate between grid points to generate a finer mesh. For data files, this smooths the color surface and enhances the contrast of spikes in the surface.

### 06. [2p] Latex

How can we include the graphics produced by Gnuplot into a TEX document?

# install texlive
apt-get install texlive

set terminal latex
set out 'plot.tex'
plot [-5:5] [-1.5:1.5] sin(x+pi) title "$\\sin(x+\\pi)$"
set out

# run it
gnuplot test.gp

# text editor: create example.tex and put some content in it
\documentclass{article}
\begin{document}
\input{plot.tex}
\end{document}

# run latex on it to produce some output
# ..don't worry about most of this output -- the important part is the Output written on example.dvi line, which says that it was successful.
latex example.tex

# now you need to view the output file with xdvi
# this will pop up a window with the beautifully formatted output in it. Hit q' to quit this, or you can leave it open and it will automatically update when the example.dvi file is modified (so whenever you run latex to update the output).
xdvi ex1.dvi &

# to produce a PDF of this you simply run pdflatex instead of latex
# ..and you'll have an example.pdf file created instead of the example.dvi file.
pdflatex example.tex

### 01. [10p] Tutorial

• Go through tutorials.

### 02. [10p] Conditional plotting

Datafile: conditional_plotting.txt.

Using Gnuplot, generate two separate bar graphs for the following:

• calories_consumed/km-ran.
• sugar_consumed/km-ran.
• ratio = too high? colour the ticks in red : colour the ticks in green.

The ratio is considered to be high enough when $6/$4 > 1. This will help you spot the people who live less healthy. The graphs should be as complete as possible (title, axes names, etc.).

### 03. [10p] Stats

Datafile: health.txt

Use Gnuplot to generate the following graphs:

• Using the 'stats' command, find out the mean and standard deviation value for the “Temperature” and “Heart Rate” columns.
• Create a rectangle that contains all the data points considered to be in the average normal values (assume that the “normal” values should be in the interval [mean-stddev, mean+stddev]).
• Create a multiplot containing 3 plots using the “Temperature” and “Heart Rate” columns: one for all genders, one for males and one for females.
• The graphs should be as complete as possible (title, axes names, etc.)

### 04. [10p] Time-based data when plotting in gnuplot

Datafile: time_data.txt

Using the code provided in “Tutorial 03. Time-based data when plotting in gnuplot”, use the histogram style, and format the xtic labels using strftime and timecolumn.

set timefmt "%H:%S"
set style fill solid 0.6 border -1
set style data histogram
set style histogram clustered gap 1 plot 'data.dat' using 2:xtic(strftime('%H', timecolumn(1))), \ '' using ($2*0.5), \ '' using ($2*0.7)

### 05. [10p] Plot histograms

Datafile: histograms.txt

#### [5p] Task A - Multiple histograms

Using Gnuplot, create multiple histograms with 'set style histogram' and 'boxes'.

#### [5p] Task B - Bar graphs

Create a simple bar graph. Remember to make the lines solid.

• Style your bars differently (set a different color for every bar).
• Do multiple bars for each entry.
• Use a function to pick the colors you want. Remember to set width and fill.

### 06. [10p] Animations

Datafile: animations.txt

• Create a script that animates a trajectory. Set a circle in the centre as a green filled circle.
• Hint: Check the code from “Tutorial” and adjust.

### 07. [20p] Heatmaps

Datafile: heatmaps.txt

#### [10p] Task A - With image/pm3d/dgrid3d

Using Gnuplot, create heatmaps using:

• “with image”
• “pm3d/dgrid3d” and “splot”

#### [10p] Task B - Interpolation

Create heatmap WITHOUT interpolation;

• As default, pm3d uses a color map which varies from black to yellow via blue and red. Change the pallete!
• Double the number of visible points.
• Question: Have Gnuplot choose the correct number of interpolation points by itself.

### 08. [20p] Latex

Datafile: heat_map_data.txt

#### [10p] Task A - 2D maps

Use Gnuplot to create three 2D maps in a single 3D graph. Export the result as a .pdf file (using gnuplottex package) and include also a \caption{Describe how you did the exercise}. Hint: You have to give the splot command 4 pieces of information: the x, y, and the z coordinate,and the value for the color.

set view 55,110
splot "heat_map_data.txt" matrix  u 1:2:(-0.5):3 w image, \
"" matrix u 1:(-0.5):2:3 w image, \
"" matrix u (-0.5):1:2:3 w image

#### [5p] Task B - Generate pdf

Create myscript.tex and add the lines below. You should put in your 'begin{gnuplot}…end{gnuplot}' your solution for plotting. The main advantage for using gnuplottex is that you are allowed to use gnuplot directly inside the .tex file.

\documentclass[a4paper]{article}
\usepackage{gnuplottex}

\begin{document}

\begin{gnuplot}[terminal=pdf,terminaloptions={font ",10" linewidth 3}]
plot sin(x), cos(x)
\end{gnuplot}

\begin{gnuplot}[scale=0.8]
set grid
set title 'gnuplottex test $e^x$'
set ylabel '$y$'
set xlabel '$x$'
plot exp(x) with linespoints
\end{gnuplot}

\end{document}

#### [5p] Task C - Compile

Compile it! Your final result should look like this: myscript.pdf.

#compile with
pdflatex --shell-escape myscript.tex`

Observations: If gnuplottex is missing, here is gnuplottex.sty

### 09. [10p] Feedback 