Home
set_plot
setplotlib
aiaa-pretty
thesis-umich
units

Home
Dowloads

setplotlib.py

Major versions
Summary
Full history
Source

Bitbucket
Github
Gitweb
Wiki
Known issues

## setplotlib

Interface for matplotlib, especially for graphics sizing

Click here to download.

### Description

Compared to MATLAB, Python's matplotlib library makes creating high-quality graphics much, much easier. Just take a look at the gallery. There is one glaring problem, though: it's extraordinarily difficult to make things (especially the fonts) the right size. In both MATLAB and matplotlib, the idea of making graphics so that the fonts will be readable when the graphic is scaled to 3.1 inches in width is apparently unthinkable.

That's why setplotlib attempts to do the dirty work of sizing your graphics for you. Consider the following example. Let's open a python interpreter in a folder that contains the setplotlib.py file. In a UNIX-like system, that will look something like

$cd path/to/setplotlib/$ python

That will give you a new prompt, which looks like this: >>>. The easiest way to do plotting is to just load all of pylab and turn interactive mode on.

>>> from pylab import *
>>> ion()

This will result in a fairly MATLAB-like experience. Using IPython may give you an even more familiar interface, but let's just stick with a regular python prompt for now. Ok, so now let's do a very basic plot.

>>> x = linspace(0, 1, 101)
>>> y = sin(4*pi*x)
>>> plot(x, y)

And don't forget to add some labels using the amazing LaTeX capabilities that are built in. (Note: This will not work if you do not have a valid LaTeX installation on your computer).

>>> xlabel(r'Fakelitude, $\sin\,\frac{\zeta}{2}$')
>>> ylabel(r'Densifate, $\rho_2$')

The resulting graphic, saved as a PNG, is below.

That's actually scaled to 75%, resulting in 6-inch-wide graphic. You can imagine how easy the labels will be to read when scaled down to 3.25 inches. Note that this is not a resolution problem; matplotlib works extremely well with vector formats, but if the labels are tiny, they will still be very hard to read when printed.

Ok, so let's see what happens when we apply setplotlib to this figure.

>>> import setplotlib
>>> setplotlib.set_plot(gcf())

The result is ...

Here's how it looks as an SVG.

You may have noticed that some other formatting happened when setplotlib ran. For now, this just happens, but in the future, there will be an interface much like that of set_plot.

Note: This only works properly when interactive mode is on. When interactive mode is off, matplotlib's built-in function called tight_layout() does not work properly. This function is used despite its disadvantages because it works with subplots. Future versions of setplotlib may be based on a fork of this built-in function.

### Acknowledgments

As you may have guessed, this project stems from the set_plot project, which was originally forked from a MATLAB concept called pretty_plot by Sean Torrez.