Showing posts with label plotting. Show all posts
Showing posts with label plotting. Show all posts

2015-05-15

pylab confusions

There are three pylabs that one may encounter in using Python. Two have been around for a while, and the third just showed up less than a month ago.

The “real” pylab is the procedural interface to matplotlib, i.e. a MATLAB-like command line interface. It imports matplotlib.pyplot and numpy into a single namespace. You can use it from ipython’s prompt by calling the magic function “%pylab”. It is no longer recommended by the matplotlib people. The recommended way is to import with abbreviated namespace names, and use the qualified functions. For example:

import matplotlib.pyplot as pltimport numpy as np
x = np.linspace(0, 2, 100)
plt.plot(x, x, label='linear')plt.plot(x, x**2, label='quadratic')plt.plot(x, x**3, label='cubic')
plt.xlabel('x label')plt.ylabel('y label')
plt.title("Simple Plot")
plt.legend()
plt.show()
Then there is the idea/proposal by Keir Mierle to improve on the pylab idea of a single package one might use to utilize Python for interactive analysis. This is written up in the SciPy wiki, but does not seem to have been updated since 2012.

And finally, if you are like me, and have not been thinking too hard, and typed “pip install pylab” you get this new package from PyPI, first added on 2015-04-23. It does nothing but pull in several other Python packages, i.e. it serves as a metapackage. You can see the source is basically a dummy, with all the action in the requirements defined in setup.py.

2011-12-07

Small matplotlib tip

While making plots using matplotlib, I kept getting this error message when trying to write a string to a certain location in the plot:
UserWarning: findfont: Font family ['cmsy10'] not found.
Turns out, the fix is simple; add the following:

matplotlib.rc('text', usetex=True)
matplotlib is a wonderful Python package for doing plotting and analysis. It uses numpy.  Used interactively with the PyLab module, it feels close to Matlab. If you are a "scientific" user, I highly recommend checking it out.