Gnuplotting

Create scientific plots using gnuplot

April 27th, 2010 | 54 Comments

Gnuplot gives us the opportunity to produce great looking plots in a lot of different formats. Therefore it uses different output terminals that can produce output files or as in the last chapter display the output on your computer screen. In this tutorial we will cover the png, svg, postscript and epslatex terminals. Therefore we will use the same code as in the previous plot functions chapter. We already used the wxt terminal there which displays the result on the screen:

set terminal wxt size 350,262 enhanced font 'Verdana,10' persist

png, and svg terminal

The png and svg terminals produce more or less the same looking output as the wxt terminal, so you only have to replace the line for the wxt terminal with

set terminal pngcairo size 350,262 enhanced font 'Verdana,10'
set output 'introduction.png'

for png and with

set terminal svg size 350,262 fname 'Verdana' fsize 10
set output 'introduction.svg'

for svg to produce the same figures I have posted in the last chapter. You may have noticed that we use the pngcairo terminal. It exists also a png terminal, but it produces uglier output and doesn’t use the UTF-8 encoding the cairo library does. You may also have noticed that we set the size to a given x,y value. If we don’t do this, the default value of 640,480 is used. The enhanced option tells the terminals that they should interpret something like n_1 for us. But be aware that not all terminals handle the enhanced notation as mentioned in the gnuplot documentation.
Since we want to create an output file in both cases we have to specify one.
The png and svg terminals work very well to produce figures to use on the web as you can see on this page, but for scientific papers or other stuff written in LaTeX we would like to have figures in postscript or pdf format. I always create my pdf files of the plots from the postscript files, so I will cover only the postscript terminals in this introduction.

postscript terminal

gnuplot has a postscript terminal that can be used to produce figures in the eps format:

set terminal postscript eps enhanced color font 'Helvetica,10'
set output 'introduction.eps'

But this creates very tiny fonts, as stated in the documentation:

In eps mode the whole plot, including the fonts, is reduced to half of the default size.

So if we want to produce the same font and line width dimensions, we have to use this command:

set terminal postscript eps size 3.5,2.62 enhanced color \
    font 'Helvetica,20' linewidth 2

It tells the terminal the size in inches of the plot (note that this is the pixel dimension divided by 100) and uses a font size of 20 instead of 10. In addition to that it doubles the given line widths by a factor of 2.

Besides, the postscript terminal can’t handle all UTF-8 input characters and we have to use the enhanced mode to produce greek letter etc. But in opposite to the before mentioned terminals the enhanced mode will work fully for the postscript terminal.

set xlabel '{/Helvetica-Oblique x}'
set ylabel '{/Helvetica-Oblique y}'
set xtics ('-2{/Symbol p}' -2*pi,'-{/Symbol p}' -pi,0, \
    '{/Symbol p}' pi,'2{/Symbol p}' 2*pi)
plot f(x) title 'sin({/Helvetica-Oblique x})' with lines linestyle 1, \

Finally, we will get the following figure. Note, that I have converted the resulting eps file to png with Gimp using strong text and graphics antialiasing to show it here. But the overall looking should be the same as with the original eps file.

Sinusoid plotted using the postscript terminal

Fig. 1 Sinusoid plotted using the postscript terminal (code to produce this figure)

If we compare Fig. 1 to the original png file made with the pngcairo terminal (Fig. 2) we see that we still have some changes. We need to adjust the tic scales and line widths by hand to get exactly the same result. Note: the font is of course different, because we used another one.

Sinusoid plotted using the pngcairo terminal

Fig. 2 Sinusoid plotted using the pngcairo terminal (code to produce this figure)

This means we should consider if want to create svg and png images or if we want to create eps and png images. In the first case we will use the wxt, svg and pngcairo terminals together.
In the second case we will use only the postscript terminal and create the png files using Gimp or some other image processor.
You may think the handling of symbols e.g. greek letters is very weird in the postscript terminal and you are probably right. But you can also write symbols in LaTeX notation, therefore you have to use the epslatex terminal.

epslatex terminal

If you have to write formulas or a scientific paper you are mostly interested in postscript/pdf or LaTeX output. The best way is to use both of them. That means to producing the lines etc. in the figure as a postscript image, but generate also a tex file that adds the text for us. The postscript image can be easily converted to a pdf using epstopdf if you needed a pdf version of the image. In order to create the tex file containing our figure labels we will use the epslatex terminal. Basically it has two working modes, a standalone mode that can produce a standalone postscript figure and the normal mode that produces a postscript figure and a tex file to include in your LaTeX document. I think the normal mode is the more common application so I will start with this.

For the normal mode we use the following terminal definition:

set terminal epslatex size 3.5,2.62 color colortext
set output 'introduction.tex'

color and colortext are needed if you have some colored labels, text etc. in your plot. size specifies the size of your plot in inches. You can specify the size alternatively in cm:

set terminal epslatex size 8.89cm,6.65cm color colortext

The easiest way is to specify the size to a value that corresponds with the needed one in your latex document. One way to get these values is to set your view to 100% in your pdf-viewer and measure the size of the area which should contain your figure. In Linux this is easily done using ScreenRuler.

Now we can write the labels etc. in LaTeX notation:

set xlabel '$x$'
set ylabel '$y$'
set format '$%g$'
set xtics ('$-2\pi$' -2*pi,'$-\pi$' -pi,0,'$\pi$' pi,'$2\pi$' 2*pi)
plot f(x) title '$\sin(x)$' with lines linestyle 1, \

Note that it is necessary to use the '' quotes and not "" in order to have interpreted your LaTeX code correctly.

If we run gnuplot on this file, it will generate a introduction.tex and a introduction-inc.eps file. In the LaTeX document we can then include the figure by:

\begin{figure}
    \input{introduction}
\end{figure}

Now our figure uses automatically the font and font size of our paper, but we still have to check that the line widths and tics have the size we want to. Also the size of the figure itself has to be correct in order to include it with the \input command without resizing it. Therefore we had to know the size of our figure before hand as mentioned above. Otherwise you should use the \resizebox command in LaTeX, but then it could be that your font size will be incorrect.

\begin{figure}
    \resizebox{\columnwidth}{!}{\input{introduction}}
\end{figure}

Note that you need the color package for colored labels. Therefore put \usepackage{color} in the preamble.

If you haven’t any LaTeX document, but want only to produce a figure with LaTeX labels, you can use the standalone mode.
Therefore exchange the above code with

set terminal epslatex size 3.5,2.62 standalone color colortext 10
set output 'introduction.tex'

The last value is the font size of the plot, which you don’t need if you want to include the figure in your LaTeX document.

To produce the standalone figure we have to run more than just the gnuplot command:

$ gnuplot introduction.gnu
$ latex introduction.tex
$ dvips -o introduction.ps introduction.dvi

The first command results in two files: introduction.tex with a LaTeX header and introduction-inc.eps which is the eps file of the figure without any text. The latex command combine the two to a single dvi file and dvips generates a postscript file. Finally I have converted the postscript file with Gimp and we will get this png file:

Sinusoid plotted using the epslatex terminal

Fig. 3 Sinusoid plotted using the epslatex terminal (code to produce this figure)

If we compare this to Fig. 2 it is obvious that we have to fine tune the line widths and tics to get the same result as for the pngcairo terminal. In order to correct it we take the following values:

set border linewidth 2
set style line 1 linecolor rgb '#0060ad' linetype 1 linewidth 5
set style line 2 linecolor rgb '#dd181f' linetype 1 linewidth 5
set tics scale 1.25

Applying these settings will finally yield to a figure which looks like it should:

Sinusoid plotted using the epslatex terminal

Fig. 4 Sinusoid plotted using the epslatex terminal with corrected line width and tics (code to produce this figure)

54 Comments

  1. francis says:

    new as i am to computer i wish to request to a live tutorial in use of the terminal in downloading and plotting of light curves in astronomy. meanwhile these directions are very good and i appreciate them.
    keep up.
    thx.

  2. Juan David says:

    Hi, I’m a huge fan of your page, after viewing how to use the esplatex terminal I’ve experienced some issues with setting the font size, apparently I need a ‘size*.clo’ file to modify the font size, I would really appreciate if you have a work around for this. Thanks!

  3. Peterspuro says:

    Listcrawler is definitely an dating online site which is used by plenty of singles and other people seeking enjoy Going Here The itemizing on the site provides free and paid membership choices. The cost-free one-evening stay using a casual dating hookup website is one which most single men and women use. The totally free choice allows anyone to search through all types of singles’ user profiles to see if any attention develops. This is actually the first particular date as being a day.

    If the free of charge 1 night stand by using a everyday online dating hookup site is not the designed use, then listcrawler can be used for a longer expression and paid for assistance. The paid for solution allows the user to publish a one-nighttime stand that is then sent out for all individuals the internet site. All alike regulations affect usually the one evening holders because they because of the ones that are signed up utilizing the cost-free model. The escort or even the hookup site is which will create the initial speak to and set up all of those other night time.

    There are a few rules that use when publishing to some singles internet site. Among those rules is you need to not article personal information. Including emails, phone numbers, or any type of street address that the prospective date might find. Furthermore, you ought to not post their real actual area. The reason for this really is that you might post an escort advert by using a place that may be too far away and that is really the only spot that the potential date might go to.

    There are escorts that checklist their actual locations about the escort’s account. Listing the bodily place of the individual can help upcoming single men and women to find out whether they ought to be chasing a relationship with somebody. For instance, if the person features a task in The Big Apple and details their work within 100 miles, a person who life in Texas or Florida could have much less extended distance to travel so that you can meet a person. The escorts around the escort’s listing are energetic grownup dating clientele and each of them has a minimum of one beneficial score.

    Listcrawler is a good hookup website for individual men searching for a subtle dating romantic relationship. The hookup is between two people which is not between a single person and another girl. As the individual guy, the greatest thing to perform is to sign up with as much courting professional services as possible. Listcrawler tends to make this easier for an individual masculine because all he has to do is simply click among the list of offered solutions to obtain the support that fits his needs the best. Some of the solutions involve webcam conversation, tone of voice postal mail, quick text messaging, and image uploads on their profiles.

    When an internet based courting site enables hookups between folks, it is actually referred to as an internet courting internet site. There are many dating websites and listcrawler fits into this niche very nicely. There are actually hundreds of hookup providers listed on the escort’s collection and the best way to determine which one particular meets your needs is to read about the many escorts and assessment their professional services and evaluations. Anything of alert: be sure you browse the relation to assistance of any assistance you plan to make use of.

    Listcrawler is owned by Adult Buddy Finder, which is amongst the largest and most an online success courting services. Mature Friend Locater also operates Biggest Loser and Matchbox, so there is anything on the market for almost any form of internet dating internet site. If you want to hookup with one of these escorts, all you have to do is search for them around the listcrawler web site and judge the one that fits your needs the very best. Most of the escorts are pretty casual and are excited to access know someone as much as you will. They can be just about everywhere and outlined by title, area, condition, country as well as country.

    Internet dating may be a wonderful way to meet those who share related likes and dislikes and would like to get to know one another better. It is additionally ways to save time and cash when it comes to finding someone new directly. There are a lot of advantages available by using on the internet services such as listcrawler, such as saving money and time. Using a dating website is a great selection should you be looking to locate a time, a companion or a long term buddy.

Leave a Reply