GnuDraw examples
With the installation package gnudraw7.zip a number of example files are installed. Some of these are discussed briefly in this page. The examples are
-
ar1.ox drawbiv_ox.ox drawgauss.src example_latex.tex graphs
drawacf.ox drawdensis.ox drawmiss.ox example.ox
drawbiv.ox drawfan.ox drawz.ox example_pdflatex.tex
- tinyexample.ox
- Show the simplicity of using GnuDraw
- drawacf.ox
- Draw multiple autocorrelation functions in one graphing window
- drawbiv.ox
- Bivariate graphs can be drawn without trouble
- drawbiv_ox.ox
- Plot a bivariate density using the original OxDraw routines
- example.ox
- A range of different output formats, and corresponding LaTeX code
- drawdensis.ox
- Create a weighted density plot
- drawgauss.src
- Use GnuDraw from OxGauss
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 GnuDraw */ #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("graphs/tinyexam.png"); ShowDrawWindow(); }
This program would result in a file tinyexam.png.plt, and is translated to tinyexam.png.
The output of GnuPlot, the file tinyexam.png.plt, basically looks similar to
# Writing file graphs/tinyexam.png # 21-03-2012, 13:03:01 # Setting terminal and output set term png set output 'tinyexam.png' set termoption enhanced set datafile missing 'NaN' set size 1,1 set origin 0.00,0.00 set key inside top left Left reverse nobox set border 3 set xtics border nomirror autofreq set ytics border nomirror autofreq set nox2tics set noy2tics plot '-' using 1:($2) title '' with lines lt 1 1 0.224886 ... 10 0.680353 e set term unknown set outputAfter 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("graphs/tinyexam.plb")). These files I translate at a later stage using batch/script file plb2x -eps tinyexam to EPS, or with plb2x -png tinyexam to a PNG file.
Drawing ACFs jointly
Often, I prefer to see multiple ACFs (or correlograms, density plots, or spectra) together in one area. Using GnuDraw one can use an extra argument fJoin. If this is set to TRUE, the graphs will be plotted jointly indeed. drawacf.ox uses// Display the autocorrelation function of both series DrawAcf(0, mY, {"y1", "y2"}, 40, TRUE, FALSE, TRUE, 2, TRUE, TRUE);to this effect.
GnuDraw could be instructed to combine graphs by default by setting
SetDraw(SET_JOIN, TRUE);at the beginning of the plot.
Bivariate graph
An example combining the possibilities of drawing kernel density estimates, contour plots and the univariate density estimates, is provided in drawbiv.ox. This program results in drawbiv.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.
/* ** DrawBiv ** ** 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)", ""); DrawTitle(3, "Density/histogram plot of Chi-2(10)"); DrawDensity(3, mY[0][], "Chi-2(10)", TRUE, TRUE, TRUE); SaveDrawWindow("graphs/drawbiv.png"); ShowDrawWindow(); }
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 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_latex.tex, using
\usepackage{latexsym} % For Diamond in GnuPlot images... \begin{figure}[htb] \centering \input{graphs/examtex.tex} \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{graphs/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 drawdensis.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 drawdensis.png. The second graph skips the weights, leading to a graph of the original uniform density, of course.
/* ** drawDensIS ** ** 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 drawgauss.src shows this.
/* ** DrawGauss.src ** ** Purpose: ** Exemplify using GnuDraw from Ox */ new; library pgraph; println "Start the program using"; println " oxl -DGNUDRAW -g drawgauss.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="graphs/drawgauss.eps"; call xy(x, y); end;No change to the Gauss program is necessary; if Ox is called using
oxl -DGNUDRAW -g drawgauss.srcthe 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="graphs/drawgauss.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.