Cgraph: PostScript plotting library in C

Version 2.04, 1998-Aug-29 (see blow for Revision History)
This is a C source code library that contains a set of C functions that generate PostScript for publication quality scientific plots. If you have a PostScript previewer, it is possible to launch the viewer automatically on generated PS/EPS files. The Cgraph Library source code, examples, and documentation are in the public domain. Utilities included in the utils* directories are not ours, and under the distribution terms specified therein.

Why do we want something like this, when Mac and Windows apps can finally produce nice plots? Those GUI apps are OK for small number of plots, but if you ever need to plot tens or hundreds of similarly formatted graphs fast, GUIs become very tedius. Also, if you need to generate plots automatically, e.g., those stock price charts updated every tens of minutes available from some Web sites, a program that incorporates this library may be used to generate EPS files from current data, which can be piped into Ghostscript (or DPS) to update GIF images without manual intervention.

Requires ANSI C compiler. Full source code, documentaiton, and a few usage examples are included. Tested using NEXTSTEP and its Preview.app. It should easily be portable to other platforms and for use with Ghostscript (modify bgshow.c)


Click one of the links to start a download:
Via HTTP download (~190 kbytes, Cgraph.tar.gz)


Examples: (C code included in the package)

The log-log plot below is generated for a B/W publication, using this C program, and an ascii data file (of X, Y pairs). This is a GIF image rendered from the EPS file the program generated (because most Web browsers can't view EPS/PS natively). The plot appeared in my paper: Encoding of binocular disparity by simple cells ... (J Neurophysiol. 1996) as Fig. 15B.

By changing just two words in the EPS file for the above, the otherwise identical file now renders in color as shown below. PS/EPS files generated by Cgraph may (implicitly) contain specifications for both B/W and color, one of which may be activated by changing a couple of def's at the beginning of the file using a standard text editor. This saves time by eliminating the need to prepare two separate figures, one for B/W publications and the other for slide/on-line presentations.

Note that PostScript's automatic conversion from color to B/W is suitable for photographic images, but generally not appropriate for plots and graphs. For example, sending the image below to a B/W device will produce unattractive results, certain to be rejected by most journals.


Available symbols for marking data points


Another minimal (but otherwise difficult to generate) example produced by this C program.


Revision History

2.04: 1998-08-29

Minor bug fix release. No major changes in features, but all compiler warnings in my environment have been eliminated.

2.02: 1997-03-04

Three new functions below, mostly for convenience of batch type processing, have been added. For example, it is now a snap to create and print PS files in a loop without having a previewer open each file.
void cg_launch_preview(int flag);
char *cg_get_output_filename(void);
void cg_set_output_filename(char *file);


See Also

ClibPDF (C library for PDF) is a library of ANSI C functions that works like our Cgraph, but for PDF (Portable Document Format) files. PDF is better known as the format used by Adobe Acrobat suite of products. Using ClibPDF, a simple C program can generate PDF files directly without the aid of any other tools. ClibPDF is a commercial product, but is free to non-profit users.

The GMT System (The Generic Mapping Tools) which generates superb scientific plots of all kinds by combining Unix commands. It is a collection of ~50 free, public-domain commands that perform well-defined drawing tasks. All source code and more are included. It is particularly strong in geographical map plotting (with all kinds of map projections), but it also works very well for other 2D, and 3D plots (XY, contour, surface, etc.). Another plus of the GMT System is that it generates PostScript output only and does not attempt to drive other inferior graphic devices unlike GnuPlot. This package also includes a C library for generating PostScript. The functions included go beyond the scope of our Cgraph library. However, that library is not intended to be used stand-alone outside the GMT System. So, it will need some work.