27 August, 1998
This is a simple interface between my C++ matrix library, newmat09, and the Gnuplot graph plotting program.
Gnuplot is a freely available program for plotting simple data. It is available for a wide range of platforms and so provides a portable way of adding simple graph plotting facilities to newmat09. See Dartmouth.
My approach is to construct a Plot2 object or a Plot3 object and a variety of objects to be added to this plot.
The interfacing method is to write a command file and data files to disk then call up Gnuplot to read the files and present the plot.
I don't regard this program as very satisfactory but it does provide an interim solution to the problem of getting reasonably acceptable graphs without too much hassle.
Here are the descriptions of the classes, member functions and global variables in the Gnuplot interface. See the file plotdemo.cpp in the library to see examples of their use.
The name of the directory where the data to be plotted and the command file is to be stored is held in the global variable
String TemporaryDirectory;
The main program should start with a statement like
TemporaryDirectory = "c:\\temp";
or
TemporaryDirectory = "temp";
to define this directory. This directory should already exist when the program is run.
LineSeries(const String& title, const Matrix& data, const int colour);
Construct a line plot with (n-1) segments and a given title and colour. The matrix, data, is n x 2 for a two dimensional plot and n x 3 for a 3 dimensional plot. A row beginning with a value 1e50 or greater is used to break a line plot (ie insert a blank line in the file to be read by the Gnuplot program).
ImpulseSeries(const String& title, const Matrix& data, const int colour);
Construct a plot of n vertical lines from the X-axis or X,Y plane to the points stored in the matrix, data. This matrix is n x 2 for a two dimensional plot and n x 3 for a 3 dimensional plot. A row beginning with a value 1e50 or greater is assumed to be missing.
StepSeries(const String& title, const Matrix& data, const int colour);
Construct a plot of n points connected by steps. Available only for two dimensional plots. The matrix, data, is n x 2.
BoxSeries(const String& title, const Matrix& data, const int colour);
Construct a plot of boxes. See Gnuplot documentation.
DotSeries(const String& title, const Matrix& data, const int colour);
Construct a plot of n dots. The matrix, data, is n x 2 for a two dimensional plot and n x 3 for a 3 dimensional plot. The dots tend to be too small to be easily visible.
PointSeries(const String& title, const Matrix& data, const int colour, const int shape);
Construct a plot of n points with the given shape. The matrix, data, is n x 2 for a two dimensional plot and n x 3 for a 3 dimensional plot.
Line(Real x1, Real y1, Real x2, Real y2);
Line(Real x1, Real y1, Real z1, Real x2, Real y2, Real z2);
Construct a straight line on a graph. The colour corresponds to colour=1. The first version is for two dimensional plots and the second is for three dimensional plots.
Arrow(Real x1, Real y1, Real x2, Real y2);
Arrow(Real x1, Real y1, Real z1, Real x2, Real y2, Real z2);
Construct an arrow on a graph. The head of the arrow is at the second set of coordinates. The colour corresponds to colour=1. The first version is for two dimensional plots and the second is for three dimensional plots.
Label(const String& label, Real x, Real y, Justification j);
Label(const String& label, Real x, Real y, Real z, Justification j);
Construct a label for a graph. The justification gives the alignment on the label with respect to the coordinates. It may take on one of the following 4 values: Label::left, Label::right, Label::centre, Label::center.
Axis(const String& title);
Construct an axis with the given title.
void Axis::SetLog(bool il = true);
Use a log scale.
void Axis::SetRange(Real min, Real max) ;
Set the range (if not set, the range is calculated from the data).
void Axis::SetZeroAxis(bool za = true) ;
Show the zero axis.
Plot2(const String& title);
Construct a two dimensional plot.
Plot2& Plot2::operator<< (const BaseSeries& bs);
Plot2& Plot2::operator<< (const GraphObject& go);
Add a series or an object (such as a line) to a plot.
void Plot2::AddCommand(const String& command);
Add a Gnuplot command such set set contour to a plot.
void Plot2::PartCommand(const String& command);
Same as AddCommand except that there is no terminating newline. This is used for building up a command from several components.
void Plot2::Set(const String& command);
Same as AddCommand except that the line is prefixed with Set.
void Plot2::AddFunction(const String& function);
Add a function to a plot (as opposed to plotting data).
void Plot2::Set_X_Axis(const Axis& xa);
void Plot2::Set_Y_Axis(const Axis& ya);
Add axes to a plot
void Plot2::WantKey(bool wk = true) ;
Include a key in the plot.
void Plot2::DoPlot();
Actually plot the plot.
The following functions are the same as or analogous to the corresponding functions in Plot2 and are used for a three dimensional (or surface) plot:
Plot3(const String& title);
Plot3& Plot3::operator<< (const BaseSeries& bs);
Plot3& Plot3::operator<< (const GraphObject& go);
void Plot3::AddCommand(const String& command);
void Plot3::PartCommand(const String& command);
void Plot3::Set(const String& command);
void Plot3::AddFunction(const String& function);
void Plot3::Set_X_Axis(const Axis& xa);
void Plot3::Set_Y_Axis(const Axis& ya);
void Plot3::Set_Z_Axis(const Axis& za);
void Plot3::WantKey(bool wk = true) ;
void Plot3::DoPlot();
Plot3 sets parametric by default. If you are plotting a function with AddFunction you should include a command
plot3.Set("noparametric");
where plot3 is the name of the Plot3 object.
The following functions convert an integer or double to a string:
String ToString(int i);
String ToString(long i);
String ToString(double f, int ndec = 4);
These are useful when building the title of a graph or series.
You need to include newmat09 and my string library in your project.
Several files need editing according to the operating system you are using.
callplot.cpp: There are 4 options under the heading CALLING METHOD. Activate the appropriate statement. If you want to use wgnupl32.exe rather wgnuplot.exe on a PC you will need to make the appropriate changes in callplot.cpp as well.
gnuplot.cpp: There are 2 options under the heading FILE SEPARATOR. Activate the appropriate statement. On Unix systems you probably want to replace the statements fout << "pause -1" << endl; by fout << "pause 3600" << endl;.
plotdemo.cpp: Set the TemporaryDirectory to something appropriate for your system.
I have tested this program with Borland 5 (32 bit console mode and 16 bit easywin) under windows NT and windows 95; Microsoft 5 32 bit console mode) under windows NT; and Gnu C++ 2.7.2 under linux.
On windows systems you want wgnuplot.dll and wgnuplot.exe (downloaded from Dartmouth) in the same directory as your .exe file. Note that the version 3.6 beta currently available on the Mathtools website does not work with this interface. To print the graph or copy it to the clipboard click the top left corner of the graph and select options. As an example this graph was obtained by copying the graph to the clipboard, pasting to Microsoft's Image composer and then saving as a gif file.
On Unix systems Gnuplot needs to be available on your system.
On Windows 95 and Windows 3.1 do not try to have two graphs to be present at the same time.
gnuplot.h | header file for Gnuplot interface program |
gnuplot.cpp | bodies for Gnuplot interface program |
callplot.h | header for calling Gnuplot |
callplot.cpp | bodies for calling Gnuplot |
plotdemo.cpp | demonstration program |
gnu_gp.mak | Gnu C++ make program |
gnuplot.htm | this file |
germany1.gif | example graph |
Go to the download page page to download the package.
August, 1998 - minor fix
January, 1998 - first release as separate package
October, 1995 - developed as part of Points point process simulation package