Visualization is the final step, displaying data graphically to the audience, portraying an idea, and capturing information efficiently and elegantly. We now turn to two packages that allow easy plotting and graphing.
Plotting throughout the book will rely heavily on the matplotlib package, maintained at
http://matplotlib.sourceforge.net/. Matplotlib isa2-D plotting package that interfaces well with NumPy and SciPy. The package is cross-platform and works on Linux, Windows, and Mac OS.
Matplotlib can produce both interactive and hard-copy plots using various engines. You can therefore use it both for interactive work, which is very useful in the early stages of an algorithm design; or you can use it in an automatic mode, for example, batch processing, to plot results to, say, a shared directory or a web server.
Matplotlib is both simple to use and highly customizable, yielding an excellent package for our purposes. It allows a range of 2-D plot types and has excellent graph annotation capabilities.
■Tip Matplotlib has some additional toolkits available, out of which the one that is of interest especially in light of Chapter 1 is the basemap toolkit. The basemap toolkit allows working with map projections. I will not be covering the basemap toolkit in this book.
An alternative package suggested here is gnuplot (http://www.gnuplot.info/). Gnuplot is a widely popular plotting package that has been ported to numerous platforms including Linux, Windows, and Mac OS. This renders gnuplot a very good graphing and plotting package. Gnuplot also supports both interactive and hard-copy graphs.
One of the benefits of gnuplot over matplotlib is 3-D graph support. If you require such capabilities, opt for gnuplot.
In order to use gnuplot interactively from the Python CLI, a software package to connect the two is required. I have used the Gnuplot.py package (http://gnuplot-py.sourceforge. net/) to do so with good results.
■ Note To use gnuplot from Python, be sure to install both gnuplot and Gnuplot.py. After installing Gnuplot. py, you'll have to set the variable Gnuplot.GnuplotOpts.gnuplot_command to point to the location of the gnuplot binary executable. Alternatively, you can edit a configuration file to permanently set this variable; consult with Gnuplot.py's documentation. In Windows, you'll also require pgnuplot.exe, which is a part of gnuplot for Windows and allows sending commands to wgnuplot (the Windows version of the gnuplot application).
As mentioned previously, most of the examples in the book rely on matplotlib, so you'll need to modify the code if you wish to use gnuplot solely. Unless you have a strong reason not to use matplotlib, or that gnuplot is already installed on your system and heavily used, I suggest you stick with matplotlib.
Was this article helpful?