GnuDraw examples

With the installation package gnudraw.zip a number of example files are installed. These are discussed briefly in this page. The examples are

tinyexam.ox
A tiny example of the simplicity of using GnuDraw
gnubiv1.ox
Bivariate graphs can be drawn without trouble
gnubiv2.ox
A more extensive bivariate example
gnuymd.ox
Plotting irregularly spaced data
drawacf.ox
Draw multiple autocorrelation functions in one graphing window
example.ox
A range of different output formats
gnudensis.ox
Create a weighted density plot
gnugauss.src
Use GnuDraw from OxGauss
drawbiv0/1.ox
Plot a bivariate density using the original OxDraw routines.

Tiny example

To give an idea of the simplicity of the interface, a tiny example program could read
/*
**  TinyExam.ox
**
**  Purpose:
**    Provide a tiny example for using GnuPlot
*/
#include <oxstd.h>      // Include the Ox standard library header
#include <packages/gnudraw/gnudraw.h>

main()
{
  decl mY;

  mY= rann(4, 10);
  Draw(0, mY);

  SaveDrawWindow("tinyexam.png");
}

This program would result in a file tinyexam.plt, and is translated to tinyexam.png.

The output of GnuPlot, the file tinyexam.plt, basically looks similar to

# Writing file tinyexam.png
reset
set term png small 
set output 'tinyexam.png'
set size 1, 1
set origin 0.00,0.00
plot '-' with lines lt 1
         1  -0.652015
         ...
        10  -0.208054
e
set nomultiplot
set term unknown
set output
After seeing these files a couple of times, they lend themselves perfectly for adapting labels, lines, colours etc. at a later date. In practice, I tend to save only files with extension .plb, which are the files without indication of the type of output (generated using a command like SaveDrawWindow("tinyexam.plb")). These files I translate at a later stage using batch/script file plb2x tinyexam eps to EPS, or with plb2x tinyexam png to a PNG file.

In the introduction to my thesis I use several graphs. In the version meant for printing ( PDF, 1.5MB), I included EPS versions. When I thought of putting a HTML-version of parts of the thesis on the web, I could without trouble generate the PNG-versions of the graphs, see the introduction in HTML.

Bivariate graph I

A bivariate graph can be drawn using DrawBivDensity, an extension of the DrawDensity command. The gnubiv1.ox program results in GnuBiv1.png.
/*
**  GnuBiv1
**
**  Purpose:
**    Show a bivariate plot
*/
#include <oxstd.h>      // Include the Ox standard library header
#include <packages/gnudraw/gnudraw.h>

main()
{
  decl mY;

  mY= rann(2, 1000);

  DrawBivDensity(0, mY, {"x", "y"}, TRUE, FALSE, FALSE);

  SaveDrawWindow("gnubiv.png");
  ShowDrawWindow();
}

Bivariate graph II

A more extensive example, combining the possibilities of drawing kernel density estimates, contour plots and the univariate density estimates, is provided in gnubiv2.ox. This program results in GnuBiv2.png.

In this example, the bivariate density of the random values in the matrix mY is approximated by the DrawBivDensity routine. The resulting x, y and z-coordinates of the approximation, [vX, vY, mZ], are used to draw the contours of the bivariate density. These [vX, vY, mZ] can also be filled analytically, as is shown in the third graph. The last, univariate graph, shows how GnuDraw can plot the same density plot as the original OxDraw.

/*
**  GnuBiv2
**
**  Purpose:
**    Show an elaborate bivariate plot
*/
#include <oxstd.h>      // Include the Ox standard library header
#include <packages/gnudraw/gnudraw.h>
#include <oxprob.h>

main()
{
  decl mY, iK, vMMX, vStep, mX, mXX, mZ, vX, vY;

  mY= ranchi(1, 1000, 10)|
      ranchi(1, 1000, 4);

  DrawTitle(0, "Sampled Chi-2(10) vs Chi-2(4)");
  [vX, vY, mZ]= DrawBivDensity(0, mY, "", TRUE, FALSE, FALSE);

  DrawTitle(1, "Contours of sampled Chi-2(10) vs Chi-2(4)");
  DrawXYZ(1, vX, vY, mZ, 2);

  iK= 20;
  vMMX= limits(mY')[:1][]';
  vStep= (vMMX[][1] - vMMX[][0])/(iK-1);
  mX= range(0, iK-1) .* vStep + vMMX[][0];
  mXX= (mX[0][] ** ones(1, iK)) |
       (ones(1, iK) ** mX[1][]);
  mZ= denschi(mX[0][], 10) .* denschi(mX[1][]', 2);

  DrawTitle(2, "Analytical Chi-2(10) vs Chi-2(4)");
  DrawXYZ(2, mX[0][], mX[1][], mZ, 0, "Chi-2(10)", "Chi-2(4)", "p");

  DrawTitle(3, "Density/histogram plot of Chi-2(10)");
  DrawDensity(3, mY[0][], "Chi-2(10)", TRUE, TRUE, TRUE);

  SaveDrawWindow("gnubiv2.png", 8);
  ShowDrawWindow();
  CloseDrawWindow();
}

Irregular dating of observations

One of the reasons for me to start using GnuPlot graphs in the first place, was to be able to plot data which was irregularly spaced in time.

With the routine DrawTMatrix, you can specify yourself when each of the observations is dated. Instead of supplying the mnYear, mnPeriod and iFreq, a matrix mYMDHMS (and two dummy variables, e.g. 0) is given. The matrix should have T columns and up to 6 rows. When two rows are used they should indicate the year and month of the observation; with more rows, also the day, hour, minute and second can be specified. Alternatively, the matrix can contain numbers according to the dayofcalender() function, with the fraction indicating the timepoint within the day.

A simple implementation is given in gnuymd.ox (see below). The output is gnuymd.png.

/*
**  gnuymd
**
**  Purpose:
**    Show the possibility to plot irregular dates
*/
#include <oxstd.h>      // Include the Ox standard library header
#include <oxprob.h>
#include <packages/gnudraw/gnudraw.h>

main()
{
  decl mY, mYMD;

  mY= ranchi(3, 4, 10);
  mYMD= <1987, 1990, 1991, 1991;
            3,    3,    4,    5; 
            1,    5,   15,   21>;

  DrawTitle(0, "Irregular dates");
  DrawTMatrix(0, mY, "Data in 1987, 1990, and twice in 1991", mYMD, 0, 0, 2);

  SaveDrawWindow("gnuymd.plb");
  SaveDrawWindow("gnuymd.png");
  ShowDrawWindow();
}

In this example, the data-set is rather silly. I used the routine myself to create e.g. a graph like figure 1.3, which displays the daily exchange and interest rate returns over the period 1982-2001, skipping over holidays and weekends where necessary. A more detailed example, in tips/intradaily.ox, describes the possibility of using intradaily data in more detail.

Comparing autocorrelation functions

Several times I had a number of time series at hand, of which I wanted to compare the autocorrelation structure. In such a case it is convenient to plot the ACF's and PACF's together in one window. For this purpose, the routine DrawACF allows for an option fJoin, which if TRUE indicates that the (partial) autocorrelation functions should be drawn jointly in one window.

The program drawacf.ox uses this function in two lines reading

  DrawAcf(0, mY, {"y1", "y2"}, 40, TRUE, FALSE, TRUE, 2, TRUE, TRUE);
  DrawAcf(1, mY, {"y1", "y2"}, 40, FALSE, TRUE, TRUE, 2, TRUE, TRUE);
such that the sample ACF of both rows of mY are drawn in the first window, and the sample PACFs in the second.

Furthermore, the program displays the possibility of GnuDraw to put one title above multiple windows with

  DrawTitle(-1, "ACFs and PACFs");
The output is drawacf.png.

A range of output formats

One of the possibilities of GnuPlot is to write a large range of output formats. From Ox, I implemented the terminals for EPS, PNG, PsLaTeX. The program example.ox saves a density plot to the different formats using the SaveDrawWindow command.

For the LaTeX file, the label was of the plot was created in LaTeX-style using

  DrawDensity(0, mY, "$\\theta$", 1, 1, 1);
When EPS output is used, the file ps_guide.ps (included in the CygWin-version of the Windows GnuPlot binary, and in most RPM packages for Linux, can be used to create labels as
  DrawDensity(0, mY, "{/Symbol q}", 1, 1, 1);
Such labels will appear in the final output as true Theta's.

The PNG-file is easily enough included in a HTML file, using e.g. the tag

  <object data="exampng.png" type="image/png">exampng.png</object>
Inclusion in LaTeX can be done as in example.tex, using
\usepackage{latexsym}  % For Diamond in GnuPlot images...
\begin{figure}[htb]
  \centering
    \input{examtex.aux}
  \caption{Inclusion of PicTeX/\LaTeX graphics file}
\end{figure}
for the PSLaTeX-graph, or
\usepackage{graphicx}  % For including EPS images
\begin{figure}[htb] 
  \centering
  \resizebox{0.8\textwidth}{!}
    {\includegraphics{exameps.eps}}
  \caption{Inclusion of EPS file}
\end{figure}
for EPS images. The final file looks like this in PDF.

A weighted density plot

Another possibility I was missing in the original OxDraw implementation, was a simple way to draw a density estimate from a weighted sample. Such a weighted sample may be the result of using an importance sampler, sampling from a candidate density and weighing the sample according to the relative densities of the target and the candidate density.

The program gnudensis.ox shows this effect. First, a sample vU is drawn from the uniform density in [-3, 3]. The weights according to the normal density are computed in vW, and two graphs are drawn. First, using the option to include weights in DrawDensity, a normal density plot results in gnudensis.png. The second graph skips the weights, leading to a graph of the original uniform density, of course.

/*
**  GnuDensIS
**
**  Purpose:
**    Provide an example of using the DrawDensity routine with the
**    importance sampler weights
*/
#include <oxstd.h>      // Include the Ox standard library header
#include <packages/gnudraw/gnudraw.h>

main()
{
  decl vU, vW, vN;

  // Sample random uniform from [-3, 3]
  vU = ranu(1, 1000)*6 - 3;
  // Calculate the weights according to the normal density
  vW= densn(vU);

  // Draw the density of the uniform random values, with weights
  // according to the normal density
  DrawDensity(0, vU, "Uniform-Normal", TRUE, TRUE, TRUE, FALSE, FALSE,
              -1, 2, vW);
  DrawTitle(0, "With weights");
  // Draw the density of the uniform random values, without weights
  DrawDensity(1, vU, "Uniform", TRUE, TRUE, TRUE);
  DrawTitle(1, "Without weights");
  ShowDrawWindow();
}

OxGauss and GnuDraw

As OxGauss implements just a layer over Ox, it is possible to instruct the underlying Gauss to call GnuDraw routines instead of the OxDraw routines.

The program gnugauss.src shows this.

/*
**  GnuGauss.src
**
**  Purpose:
**    Exemplify using GnuDraw from Ox
*/
new;
library pgraph;

println "Start the program using";
println "  oxl -DGNUDRAW -g gnugauss.src";
println "to use GnuDraw instead of OxDraw";
x= seqa(1,1,1000);
y= rndn(1000,1);
call xlabel("X");
call ylabel("Y");

/* GnuDraw saves EPS files instead of TKF's by default */
_ptek="gnugauss.tkf";
call xy(x, y);

end;
Starting with version 3.30 of Ox, no change to the Gauss program is necessary; if Ox is called using
  oxl -DGNUDRAW -g gnugauss.src
the GnuDraw routines are used instead of OxDraw. Note that GnuDraw defaults to saving files in eps format, instead of tkf. Specifying the output file as _ptek="gnugauss.png" results in a png file as expected.

Note that exactly the same functionality is implemented as with OxGauss itself: Not all functions of the original pgraph library might be available.

Drawing bivariate densities in OxDraw

Sometimes it might be easier to use OxDraw for making the final set of plots. One option GnuDraw implements, but OxDraw misses, is the possibility to use the command DrawBivDensity. The library packages/gnudraw/lib/libbiv.ox implements this one function, to use it from OxDraw. See packages/gnudraw/samples/drawbiv0.ox and packages/gnudraw/samples/drawbiv1.ox.
/*
**  DrawBiv0
**
**  Purpose:
**    Show a bivariate plot using OxDraw
**
**  Date:
**    20/2/2004
**
**  Author:
**    Charles Bos
*/
#include <oxstd.h>      // Include the Ox standard library header
#include <oxdraw.h>     // Include the Ox graphics library header
#include <packages/gnudraw/lib/libbiv.ox>

main()
{
  decl mY;

  println ("Example of drawing bivariate histograms using OxDraw");
  mY= rann(2, 1000);

  DrawBivDensity(0, mY, {"x", "y"}, TRUE, FALSE, FALSE, 0);
  SaveDrawWindow("drawbiv0.eps");
  ShowDrawWindow();
}


[Main page] [Top] [Next: Gnudraw installation]
Last change: 1/12/2004