How to use PGPLOT Graphics with Fortran95 on
Clive Page, 2012 June 19
is a library of Fortran-callable routines for producing graphical output of
many types including line drawings, images and contour maps. It has
particularly good facilities for making plots of scientific data, things
like logarithmic axes and error-bars, and annotations can make use of
exponential or sexagesimal number formats. On suitable devices such an
X-windows terminal it supports user-interaction via a pointing device such
as a mouse. PGPLOT was written by Tim Pearson, a radio-astronomer at
CalTech, starting around 1983. The package does not appear to have
been updated in the last couple of years, but it is reliable,
well-structured, well-documented, well-tested, and widely used in the
scientific community. Copyright in the source code is retained by the
California Institute of Technology and the free usage licence covers
non-commercial purposes only, but the source code can be downloaded freely.
Unfortunately PGPLOT is only moderately portable, because it is written in a
mixture of languages and because some of the device drivers are operating
system-specific. Most of PGPLOT is written in Fortran77 (pretty much
conforming to the Standard) but a few low-level routines and some device
drivers are written in C. The installation procedure is also
complicated: it doesn't just involve building with a Make file. It is
necessary to select a set of suitable device drivers, from which the install
script then creates a file called GREXEC.F, which is a device dispatcher
written in Fortran77. It also requires the font information to be
converted from a text file to an unformatted (binary) file suitable for the
Fortran compiler in use. PGPLOT supports a large number of devices,
most of them plotters found nowadays only in museums, but also output
formats such as Postscript, GIF, and PNG. Interactive graphics on
Unix-like systems is possible using an X11 driver, but I could not get this
to work under Windows, nor could I find adequate instructions anywhere
for installing PGPLOT under Windows for use with a modern Fortran compiler.
My solution, outlined below, is to use the GrWin library written by
Tsuguhiro Tamaribuchi of Shizuoka University in Japan. GrWin is
however distributed with pre-built libraries only for the obsolete g77
compiler, and my attempts to recompile the source code for use by g95 were
not successful. However I found a work-around which is described
1. Install the MinGW Package
MinGW is a minimal Gnu package for Microsoft Windows, i.e. it provides a
basic set of GNU (Unix-like) tools services for the Windows
environment. The package can be found at http://www.mingw.org.
Under Navigation select Downloads, and on the line marked "Automated MinGW
installation" select the download link, then select MinGW-5.1.4.exe (the
version number may well have been updated by the time you read this).
You should select both the basic tools, and the g77 compiler (this is needed
for a library that it includes). I recommend installing the package
into the default location, C:\MINGW
2. Install the g95 or gfortran Compiler
This may be found at www.g95.org
from which you should select Downloads and then Binaries. What you
need is the item in the G95 BINARIES section called "Self-extracting Windows
x86" - this may be downloaded by FTP or HTTP - there is not much difference
but HTTP may be more resilient in the face of network problems. After
downloading the file run it to start the installation process. In
general it is safe to select the defaults, but if you try to install this in
the default location it chooses for you, that is C:\FORTRAN,
the installer should notice that you have an existing MinGW structure and
give you the option of installing in C:\MINGW
instead - this is the location that I recommend.
You should now be able to use g95 to compile Fortran programs whether in the
old fixed-format style (.f files) or new free-format style (.f90 or .f95
files) and it is probably a good idea to do a couple of tests to make sure
the compiler is installed correctly. These install scripts normally
create or modify various environment variables such as PATH,
LIBRARY_PATH, and G95_LIBRARY_PATH.
The g95 compiler is run from a command-window, which is well hidden by
Microsoft. If using Windows-XP you can find it it in Start | All
Programs | Accessories. It can be found in similar places in later versions of Windows.
If you are using g95 extensively it is worth
dragging the icon to the desktop - the properties of this icon can then be
altered to allow you to have some specified sub-directory (folder) as the
default working directory when starting up the command window. It is
also possible to change the size of the command window.
Alternatively you may prefer the gfortran compiler which seems now to be
ahead of g95. This can be downloaded from http://gcc.gnu.org/wiki/GFortranBinaries
3. Installing the GrWin Package including PGPLOT
The main page for the package (in English) is http://spdg1.sci.shizuoka.ac.jp/grwinlib/english/
- this has a link to the downloads page, from which you need to select
GrWinC-100e_MinGW_gfortran.msi or the equivalent for g95. Note: I
found that even though I was using a 64-bit version of Windows7, it was this
version not the -x64 variant that worked for me.
After downloading this file run it to install the package. The default
location of C:\GrWin is fine,
and you need to install the GrWin0 library and PGPLOT options, but not the
Calcomp library (unless you really still have software that needs this
library). This installer installs the essential bits of PGPLOT in
including a set of pgdemoN.f
The g77 compiler should have been installed along with the MinGW packages,
so you should be able to use it to compile one of these pgdemoN files.
The one I usually try is pgdemo2.f but all should work. Because some
of the paths and environment variables may not have set up yet, you may need
to change to work in the directory (folder) C:\MINGW\BIN
to get this to work. The commands needed to copy a file and
compile and link it, and then run it are, for example (shown here in
upper-case, but the lower-case equivalent would also work):
The program should prompt you for a device (or enter ? to get a list of
them). Choose /GW for a graphics window to appear with various PGPLOT
test patterns - you have to press the Enter key in the command window to
move to the next page of output. The text annotation in these plots
may well be missing because the GRFONT.DAT
file is not in the right place (or possibly not in the right
format). The solution is to define an environment variable
pointing to the directory where they are located. This can be done
temporarily (for the duration of a command window) with a command line this:
COPY \GRWIN\PGPLOT\PGDEMO2.F .
The way of making a permanent environment variable is also well hidden
within Windows®. You can find it as follows: go
to Start then Control Panel and then scroll down and select System.
When the System window appears select Advanced tab then click the
Environment Variables button which should be found inconspicuously near the
bottom left. There are two sets of environment variables, one for you
as the current user, and another for all users of the PC. It does not
matter which you use if you are the only user of the PC. Click on NEW
and then set PGPLOT_DIR as
the Variable name, and C:\GRWIN\PGPLOT
as the Variable value. Then a set of OKs is needed to update the
system and remove all the system windows. If necessary you can
download from here a precompiled version of grfont.dat and rgb.txt .
4. Using g95 with GrWin
The batch file used in the test above, PGGWF77.BAT, needs to be modified
somewhat to use g95. Since it only contains one vital line, I suggest
you edit this or create a new one or download the one I made called PG95.BAT, using any good text editor (or
even a really bad one such as WordPad which is all that comes with
Windows). The command file you need is:
-fno-backslash -O -o %~n1 %1 %2 %3 %4 %5 %6 %7 %8 %9
-Wl,--subsystem,console -lcpgplot -lpgplot -lGrWin -mwindows -lg2c
Note: this may wrap around on your display, but is just one line.
A few words of explanation about the items in this file:
If you find (as I did on one installation) that the linked failed because it
could not find all the required libraries, you may need to edit the batch
file to include an -L option such as -LC:\GrWin\MinGw_gfortran\lib
- note that there is no space after the -L.
- The -Bstatic switch is
only needed to generate a static complete executable file, and may be
omitted if the executable is only going to be used in situ.
- The -fno-backslash
switch is needed since PGPLOT text annotations may involve using the
backslash character as an escape (e.g. to produce font changes) and it
is necessary to get g95 to treat it as a normal character and not use
the default C convention.
- The %~n1 argument means
that only the name (not the file extension) of the first argument is
used as the name of the resulting executable - this means you need the
file extension on the command line, but it will take file.f
or file.f90 as necessary
(the former denotes fixed line layout, the latter denotes free-format
- The other arguments %1, %2 etc allow you to specify up to 9 Fortran
source files in the command.
- The item at the end, -lg2c,
allows routines in the pgplot library which were compiled using g77 to
find the necessary low-level routines in the library file libg2c.a.
Thus you use it like this, note that the file extension is needed on the
source files, but that .f, .f90, and .f95 are all accepted
5. Adding a GIF Driver
You may notice that the only hard-copy drivers are for Postscript files -
this format, a forerunner of PDF, is widely used in the Unix/GNU/Linux
world, but not common in the Windows environment (although the GSview
program handles it fairly well). File formats which are more common in
the Windows world are PNG and GIF. I have not managed to make the PNG
driver available, but it is now possible to add GIF output format, using a
bit of Fortran source-code which you can download, called addgif.f90 - simply add this to your
This works with gfortran as well (using the pgfortran batch file). The
devices available are /GIF and /VGIF, the latter producing a .gif file in
portrait orientation. The default size of the gif images is 850 x 680
pixels (or 680 x 850 in portrait), but this can be changed by setting values
for environment variables PGPLOT_GIF_WIDTH
that there must be no spaces either side of the equals sign, e.g. you need a
command like this:
To restore the defaults use the command with nothing after the equals sign.
The source-code for addgif.f90 is substantially modified from that in the
PGPLOT distribution, as I could not get this to produce satisfactory results
with g95 or gfortran (the Fortran77-vintage code necessarily included lots
of non-standard features, no longer necessary with modern Fortran).
This file contains an updated device despatcher and GIF driver making use of
the excellent GIF-writing software of Jos Bergervoet, to whom I am very
6. Using PGPLOT with gfortran
I have also produced a batch file called PGFORTRAN.BAT
(only slightly different from pg95.bat) which works with the
gfortran Fortran95 compiler.
7. Note on libg2c.a
I am grateful to Harry Lehto for uncovering the fact that there seems to be
more than one version of the file libg2c.a in the MinGW
distributions over the years, and that not all of them work with this PGPLOT
system. A copy of one that seems to work (size 245922 bytes) is linked here
as libg2c.a (since
it is a binary file, right click and use "save as", then save the file in
your MinGW library directory typically C:\MinGW\lib.