Making scientific results: the integration of LaTeX and Gnuplot

"If your only tool is a hammer then every problem becomes a nail".

Abraham Maslow


the

Introduction


Scientific creativity in itself the process is not trivial, requires some detachment from the outside world. And nonlinear in terms of the distribution of intensity in time — wasted sometimes tell half a year, then, within a month and a half to solve most of the issues of concern to you.

And here, you used 100% of the opportunities you have visited 'Eureka' finished a major work and it's time to publish their results in the journal, to report to the conference, and just to please his supervisor/consultant a nice report. You are embarking on a painful phase of an article/report. And how painful will this phase, depends on what tools you decided to use for this job.

Remember the days when young and foolish graduate student, I wrote the first version of the candidate's "brick", designed for thorough "subtraction" me and my supervisor. I didn't know about EPS format, and therefore exported graphics, built in Maple *.bmp is a raster and... manually drew them in MS Visio and then paste in Word. There were other, no less clumsy stupidity. Not surprisingly, then, I cursed everything and gave myself the following thesis to write quite differently.

By successive iterations, today I came to that decision:



And it's time to give experience to people. Interested, welcome to under the cat.

the

1. Gnuplot: what is and what to eat


I think I will not discover America, and the reader familiar with this utility. To long to talk about it, I will give you a number of references, primarily on official website lowering the barrier to entry "notes of debianwiki" and a very useful resource with many examples, well, and very well decorated the FAQ on Russian language. This will help to quickly get into the swing of things, for those who haven't tried this tool at work.

In short — Gnuplot is a powerful tool for the plotting of graphs defined by analytic dependencies, table (experimental data and numerical simulations) that support command mode and scripting. Linux users just install the package from the repository of your distro, Windows users and OS X users can also install this tool, following the instructions on the official website. I'm going to present, all planned to the presentation, on the example of Linux.

Type in the command line:
the
$ gnuplot
G N U P L O T
Version 4.6 patchlevel 6 last modified September 2014
Build System: Linux x86_64

Copyright (C) 1986-1993, 1998, 2004, 2007-2014
Thomas Williams, Colin Kelley and many others

gnuplot home: http://www.gnuplot.info
faq, bugs, etc: type "help FAQ"
immediate help: type "help" (plot window: hit 'h')

Terminal type set to 'qt'
gnuplot>

Receiving an invitation to enter commands. Introduced, well, for example:

the
plot x**3 title 'Cubic parabola y = x^3'

And get:



When I saw this for the first time, I also said "Fuuu...!" This is the result obtained by default, and it looks like it is not solid. So no one bothers to tweak it. First of all change the color and thickness of the graph line.

the
gnuplot> set style line 1 lt 1 lw 3 lc rgb '#4682b4' pt -1
plot x**3 title 'Cubic parabola y = x^3' ls 1

Do "school" axis, with the intersection point at the origin, draw solid lines

the
gnuplot > set xzeroaxis lt -1
gnuplot> set yzeroaxis lt -1
gnuplot > replot

Add grid lines — dotted gray line:

the
gnuplot> set grid xtics lc rgb '#555555' lw 1 lt 0
gnuplot> set grid ytics lc rgb '#555555' lw 1 lt 0

Move the axis labels to the axes closer look:
the
gnuplot> set xtics axis 
gnuplot> set ytics axis 

Change the range of the argument:

the
gnuplot > set xrange [-3:3]

And eventually get this:



So much better than the original version. Customization of the graphs is just gorgeous, more on this can be read on the links above. All the text set out above is intended to seed, and it will be about

the

2. Gnuplottex: integrate graphs in a LaTeX document


Gnuplottex package supplied with TeXlive, allowing you to enter the Gnuplot commands directly in the layout document. Without theorizing, proceed directly to practice. Create a new document

the
\documentclass[12pt]{article}

% Connected any differently, set encoding, language and other parameters to taste
\usepackage[OT1,T2A]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english,russian]{babel}
\usepackage{amsmath,amssymb,amsfonts,textcomp,latexsym,pb-diagram,amsopn}
\usepackage{cite,enumerate,float,indentfirst}
\usepackage{graphicx,xcolor}

% Order to set the size of the page margins, then this will come in handy
\usepackage[left=2cm, right=2cm, top=2cm, bottom=2cm]{geometry}

% Include Gnuplottex
\usepackage{gnuplottex}

\begin{document}

\section{plotting graphs in Gnuplot document \LaTeX}

\end{document}

ATTENTION! document Assembly must be performed with the key -shell-escape, including the ability to run shell commands or so:

the
$ pdflatex -shell-escape gnuplottex_rus.tex

Or you can specify this key in the IDE settings (I have a Kile):



Now, in the body of the document will be sculpting our schedule is:

the
\begin{figure}[h]
\centering
\begin{gnuplot}
plot x**3 title 'Cubic parabola $y = x^3$'
\end{gnuplot}
\end{figure}

After Assembly receiving:



Notice that here the most "tasty"? The formula in the caption to the chart it looks like human beings- all the power of LaTeX at your disposal. And now finished it off with napilnikom:

the
\begin{figure}[h]
\centering
\begin{gnuplot}
set terminal epslatex color size 12cm,15cm
set xzeroaxis lt -1
set yzeroaxis lt -1
set style line 1 lt 1 lw 4 lc rgb '#4682b4' pt -1
set grid ytics lc rgb '#555555' lw 1 lt 0
set grid xtics lc rgb '#555555' lw 1 lt 0
set xrange [-3:3]
plot x**3 title '$y = x^3$' ls 1 
\end{gnuplot}
\end{figure}


We note in particular the command:

the
set terminal epslatex color size 12cm,15cm

Specifies the terminal type: EPS LaTeX with support for color output, size 12 x 15 cm — your picture is the terminal. Finally we get a chart



Let's modify the code slightly by adding another line style and graph:

the
 set style line 2 lt 1 lw 4 lc rgb '#aa0000' pt -1 
.
.
.
plot x**3 title '$y = x^3$' ls 1, \ 
x**4 title '$y = x^4$' ls 2




It is seen that there is no fundamental difficulty in the use of the technology in question. You can add this same three-dimensional chart:

the
\begin{figure}[h]
\centering
\begin{gnuplot}
set terminal epslatex color size 12cm,12cm
splot x**2 + y**3 with lines title '$z = x^2 + y^3$'
\end{gnuplot}
\caption{three-Dimensional graph grid}
\end{figure}




Change the mapping from the grid to color the polygons:

the
splot x**2 + y**3 with pm3d title '$z = x^2 + y^3$'



Customization of appearance, choice of palette — all this can be gleaned from the documentation for Gnuplot, and now we move on to the next item on the program

the

3. Plotting from data files


This is the main power of this tool. Suppose you have a text file with the data formed in the following way:

the
# x y1 y2 
0 0 0
1 1 1
2 4 8
3 9 27
4 16 64

The first column, for example the argument; the second and third values of certain functions. This may be the result of the experiment, or the result of numerical simulation. Construction of two-dimensional graphics will look like this:

the
plot '<data file name> using <column argument>:<column functions> title '<legend>'

Columns are numbered starting with one. In order not to be bored, here is an example from their documents. It will create a folder with the training project directory results/ and Pomeau there a file with the results of the numerical experiment 2319.log (there is a specificity of naming logs...). Then add in our project like this code:
the
\begin{figure}
\centering
\begin{gnuplot}
set terminal epslatex color size 17cm,8cm
set xzeroaxis lt -1
set yzeroaxis lt -1
set xrange [0:20]
set style line 1 lt 1 lw 4 lc rgb '#4682b4' pt -1 
set style line 2 lt 1 lw 4 lc rgb '#ee0000' pt -1 
set style line 3 lt 1 lw 4 lc rgb '#008800' pt -1
set style line 4 lt 1 lw 4 lc rgb '#888800' pt -1
set style line 5 lt 1 lw 4 lc rgb '#00aaaa' pt -1
set style line 6 lt 1 lw 4 lc rgb '#cc0000' pt -1
set grid xtics lc rgb '#555555' lw 1 lt 0
set grid ytics lc rgb '#555555' lw 1 lt 0
set xlabel '$t$ c'
set ylabel '$P$ kN'
set key bottom right 
plot 'results/2319.log' using 1:3 with lines ls 2 ti '$P_2$', \
'results/2319.log' using 1:9 with lines ls 4 ti '$P_9$', \
'results/2319.log' using 1:19 with lines ls 5 ti '$P_{19}$',\
'results/2319.log' using 1:29 with lines ls 1 ti '$P_{29}$',\
'results/2319.log' using 1:49 ls 3 with lines ti '$P_{49}$', \
'results/2319.log' using 1:54 with lines ls 6 ti '$P_{53}$' 
\end{gnuplot}
\caption{Longitudinal force in different sections of the train}
\end{figure} 




Team:

the
set key bottom right 

Places the legend in the lower right corner of the chart, so it does not interfere with top and right side. In addition, the Gnuplot commands and their options could be reduced to some extent unambiguous interpretation of the writing, as in this example: ti => title.

Now imagine that you made up the thesis, but at the last moment you need to substitute other results of the same measurements. It is not necessary pereverstyvat graphics — to change the results file and rebuild the project. All! Your layout is not going anywhere. Five minutes case, if the change data does not lead to far-reaching scientific conclusions.

And last — if you put the graph on slide Beamer, the environment of the slide should contain the option fragile otherwise you will get compile error. Here it is:

the
\begin{frame}[fragile]
%
% Contents of the slide
%
\end{frame}


the

Conclusion


The study has taken me all last night. The presentation had to impose late at night, but I managed today successfully reported (report on the first year of doctoral studies). In the shower left a warm feeling from the possibility of open technologies that help in the scientific work. Which way to go, everyone chooses for himself. Article has survey character and covers only the issues necessary for a quick start. The rest can be easily gleaned from the documentation.

The document created in example, you can pick up here

Success in scientific work, and thank you for your attention to my.
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

When the basin is small, or it's time to choose VPS server

Performance comparison of hierarchical models, Django and PostgreSQL

From Tomsk to Silicon Valley and Back