Skip to content

Instantly share code, notes, and snippets.

@deeplycloudy
Last active December 25, 2015 16:59
Show Gist options
  • Save deeplycloudy/7009692 to your computer and use it in GitHub Desktop.
Save deeplycloudy/7009692 to your computer and use it in GitHub Desktop.
Profile matplotlib scatter plots in the agg backend using yep and gperftools
"""
This script uses yep (https://pypi.python.org/pypi/yep) and its dependency
gperftools (https://code.google.com/p/gperftools/) to profile rendering in
matplotlib's agg backend. Hopefully this hits the relevant drawing parts
within the profiler start/stop block. It is meant to try to figure out where
the time is spent doing a 100,000 point color-mapped scatterplot.
To build gperftools against my 32 bit Python on 64 bit Mac OS X, I had to:
./configure CFLAGS="-arch i386 -m32" CXXFLAGS="-arch i386 -m32" LDFLAGS="-arch
i386 -m32"
In order to view the profiler output, which requires graphviz to be installed,
I used a command like that below. You will need to point at the path to your
backend_agg.so; the path is from my Mac OS X EPD install. You can change --dot
to --pdf (see pprof --help), though it created a chopped-off PDF in my case.
pprof --dot
/Library/Frameworks/Python.framework/Versions/Current/lib/python2.7/site-packages/matplotlib/backends/_backend_agg.so
mplscatter.prof > profoutput.dot; open -a Graphviz profoutput.dot
Original source for this idea:
http://stackoverflow.com/questions/2615153/profiling-python-c-extensions
"""
import yep
import matplotlib.pyplot as plt
import numpy as np
f=plt.figure()
ax=f.add_subplot(111)
x = np.arange(1e5, dtype='f4') + 1
y = 1.0/x
yep.start('mplscatter.prof')
s = ax.scatter(x,y,c=x, s=25)
plt.savefig('mplscatterprof.png')
yep.stop()
@mikesart
Copy link

To build gperftools against my 32 bit Python on 64 bit Mac OS X, I had to:
./configure CFLAGS="-arch i386 -m32" CXXFLAGS="-arch i386 -m32" LDFLAGS="-arch
i386 -m32"

This bit was so useful. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment