User Tools

Site Tools


exercises:2015_uzh_molsim:gnuplot

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

exercises:2015_uzh_molsim:gnuplot [2015/04/23 12:20]
exercises:2015_uzh_molsim:gnuplot [2020/08/21 10:15] (current)
Line 1: Line 1:
 +====== 2d plotting with Gnuplot ======
 +
 +[[http://www.gnuplot.info/|Gnuplot]] is a command-line driven graphing utility that is available for many different operating systems.
 +
 +Gnuplot is a powerful program that can be used to generate publication-quality figures. Here, we are going to introduce only a small subset of its features. If you are interested to learn more, have a look to the [[http://www.gnuplot.info/documentation.html|documentation]].
 +
 +<note tip>You are free to choose a different plotting program for your analysis ([[http://matplotlib.org/|matplotlib]] might e.g. be an interesting alternative). However, Gnuplot is very versatile, can get the job done quickly and is still used by many computational scientists today.</note>
 +
 +''gnuplot'' starts the interactive Gnuplot console. This are some of the commands we are going to need:
 +<code bash>
 +plot sin(x)                                # use plot for 2d plots
 +splot sin(x)*sin(y)                        # use splot for 3d plots
 +plot 'spectrum.ener' using 1:4            # plot column 1 as x and column 4 as y
 +replot 'spectrum.ener' using 1:($3+$5) title 'Total energy'
 +set xlabel "time steps"
 +set xrange [0:10]
 +replot                                     # redo the plot with current settings
 +set terminal x11 enhanced font "arial,20"  # increase font size
 +help <command>                             # gnuplot has a very useful integrated help
 +exit                                       # leave gnuplot (loosing unsaved graphs)
 +</code>
 +
 +Now you are ready to plot some actual data.
 +''spectrum.ener'' in the ''intro'' folder contains results from a molecular dynamics simulation.
 +
 +<note>**TASK 1**
 +
 +  - Open the file in a text editor. What quantities does it contain?
 +  - Plot the kinetic energy and potential energy versus time.
 +  - Add the sum of kinetic and potential energy to the plot. In which type of ensemble was this MD simulation performed?
 +  - Label the axes of the plot with appropriate quantities and units.
 +
 +</note>
 +
 +Later you will want to save your graphs in order to use them in your reports. This is done as follows:
 +<code bash>
 +set terminal png              # we want to create a .png image
 +set output "graph.png"        # with filename "graph.png"
 +replot                        # plot with current settings, directly into file "graph.png"
 +set terminal x11              # switch back to plotting on screen
 +</code>
 +
 +
 +Gnuplot is not just a plotting utility, it can also perform fits.
 +Say, we have a data set ''data.dat'', which contains $x$ in the first column and some computed $f(x)$ in the second column.
 +We want to fit a function $f(x)=ax^2$ to this data set. In Gnuplot, this would be achieved by:
 +<code bash>
 +f(x) = a*x*x                   # Define function to be fitted
 +a = 1                          # initial guess for a
 +fit f(x) "data.dat" using 0:1 via a
 +</code>
 +
 +
 +<note>**TASK 2**
 +
 +  - Create a second plot, this time of temperature versus simulation time.
 +  - Label axes of the plot with appropriate units.
 +  - Use Gnuplot's fitting functionality to extract the //average// temperature.
 +
 +</note>
 +
 +Finally, once you have figured out which commands you need to create the plot you want, it is a good idea to write these commands to a file, say ''script.gp''. This has the advantage that gnuplot can re-create your graph in an instant. On the bash terminal type:
 +<code bash>
 +gnuplot script.gp   # let gnuplot perform the commands in 'script.gp'
 +gnuplot             # alternative: start gnuplot
 +load 'script.gp'    #              and load script from within gnuplot
 +</code>
 +This makes it very quick and easy to change details in the plot at a later point in time.
 +Proficient gnuplot users will often start by writing the file, run it through gnuplot and then adjust the remaining details.