Making matplotlib work with PyPy

If you read this, perhaps you already tried once to switch to from CPython to PyPy and you realized that matplotlib is not working. Or someone else sent you here to don’t waste your time :D.

In this post I will guide you, step by step to compile yourself matplotlib for CPython 2.7 and PyPy with all necessary dependencies.

1. Install all necessary system packages:

sudo apt-get install gstreamer-1.0 libgstreamer-plugins-base1.0 doxygen gtk+2.0 freeglut3-dev

2. Install PyPy 5.8 from and activate pip for it by running pypy -m ensurepip

3. Install numpy

pypy -m pip install numpy

4. Download latest sources of matplotlib from GitHub (it will take a while for all ~300 MB):

git clone

5. Download wxpython-cffi:

hg clone

6. Download wxWidgets sources and extract it:

tar -xjf wxWidgets-3.0.3.tar.bz2

7. Set some paths:

export WXWIN=/your/path/wxWidgets-3.0.3
which doxygen (usually /usr/bin/doxygen)
export DOXYGEN=/your/path/doxygen

8. Build wxWidgets

cd wxpython-cffi
pypy dox (this should finish with success)
pypy build_wx

9. Build wxPython

pypy etg --generator=cffi --nodoc   (you should get some errors)

open file wx/core.pi and comment all lines from 27712 to 27720(inclusive) and save
run the build conmand again:

pypy etg --generator=cffi --nodoc
pypy cffi_gen

10. Prepare to build matplotlib

ln -s /your/path/wxpython-cffi/cffi/ /your/path/pypy/site-packages/wx-2.8
from folder wxpython-cffi/cffi copy folders wx, wrapper_lib to/your/path/pypy/site-packages/
from folder wxpython-cffi/cffi copy files *.so to pypy/site-packages/wx/

test the wx module with this command:

pypy -c "import wx"

it will print out some warnings but is OK

cd ../matplotlib
cp setup.cfg.template setup.cfg
Open to edit setup.cfg and go near the end of file
Uncomment tkagg and set it to False (tkagg = False)
Uncomment wxagg and set it to True (wxagg = True)
Uncomment backend and set it to WXAgg (backend = WXAgg)
Save and exit
Open to edit lib/matplotlib/backends/ and, from line 1803 remove the inheritance wx.Printout to look like this: `class PrintoutWx():`
Also comment line 1805
Add below 1805: self.title = title
Save and exit
Download file and copy in your pypy/site-packages/

The step with wx.Printout is needed because it seems that this version of wxPython doesn’t have this class implemented yet.

11. Build matplotlib

pypy build
pypy install

12. Cleanup

remove the symbolic link wx-2.8

13. Finish

And you are ready to go! OK, there will be some warning and errors but at least is a starting point for improvement.

Screenshot from 2017-06-23 15-54-21

Everything in one script:

By Gabriel Munteanu, gabriel.munteanu [at] rinftech [dot] com


2 thoughts on “Making matplotlib work with PyPy

  1. Hi,
    Cheers for this changing the apt-get line to have sudo is definitely a worthwhile tweak.
    I tried this, but wasn’t able to get matplotlib working, when I tried any of the examples in matplotlib/tutorials I got errors like this –

    $ python
    /home/stu/.virtualenvs/pypy-nightly-trunk/site-packages/wx/ UserWarning: wxPython/wxWidgets release number mismatch
    warnings.warn(“wxPython/wxWidgets release number mismatch”)
    /home/stu/.virtualenvs/pypy-nightly-trunk/site-packages/wx/ UserWarning: implicit cast from ‘unsigned char *’ to ‘char *’ will be forbidden in the future (check that the types are as you expect; use an explicit ffi.cast() if they are correct)
    clib.Cursor_set_flags(wrapper_lib.get_ptr(self), flags)
    Traceback (most recent call last):
    File “”, line 142, in
    import matplotlib.pyplot as plt
    File “/home/stu/.virtualenvs/pypy-nightly-trunk/site-packages/matplotlib-2.1.0rc1+110.g963f91300.dirty-py2.7-linux-x86_64.egg/matplotlib/”, line 113, in
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
    File “/home/stu/.virtualenvs/pypy-nightly-trunk/site-packages/matplotlib-2.1.0rc1+110.g963f91300.dirty-py2.7-linux-x86_64.egg/matplotlib/backends/”, line 60, in pylab_setup
    [backend_name], 0)
    File “/home/stu/.virtualenvs/pypy-nightly-trunk/site-packages/matplotlib-2.1.0rc1+110.g963f91300.dirty-py2.7-linux-x86_64.egg/matplotlib/backends/”, line 12, in
    from . import backend_wx
    File “/home/stu/.virtualenvs/pypy-nightly-trunk/site-packages/matplotlib-2.1.0rc1+110.g963f91300.dirty-py2.7-linux-x86_64.egg/matplotlib/backends/”, line 1818
    SyntaxError: return outside function


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s