24

I installed matplotlib successfully inside a virtualenv. Now I'm trying to get it to draw. I know how to change the backend, but I'm having a whole lot of trouble figuring out what to change it to.

Has anyone managed to get it totally working with python3? If yes, how?

I have tried a bunch of things. I have cycled through all the backends to see what all of the complaints are, not I'm trying to get just one of them to work.

Also possibly worth noting is that my goal is to integrate it into a Pyramid app.

Here's what has happened so far for all the different backends:

  • Agg: this was the default backend. it does not draw
  • GTK: requires pygtk which apparently has not been ported to python3
  • GTKAgg: ditto
  • GTKCairo: says 'required package gtk' assume something along the lines of above
  • FltkAgg: Doesn't look like it has a python3 version. Even though it is mentioned in the python3 UI FAQ. I guess that was just a rouse. gosh darnit
  • MacOSX: I'm running ubuntu. assumed inappropriate
  • QtAgg: requires pyqt. see Qt4Agg
  • Qt4Agg: see Installing PyQt4 in a virtualenv
  • TkAgg: ImportError: cannot import name _tkagg. I can run import tkinter in the interpreter though, so I'm not really sure what's broken as yet
  • WX: ImportError: Matplotlib backend_wx and backend_wxagg require wxversion, which was not found
  • WXAgg: ditto
  • CocoaAgg: ImportError: The CococaAgg backend required PyObjC to be installed!
  • GTK3Cairo : ImportError: GTK3 backend requires pygobject to be installed. I tried installing it but when I try to configure it with the correct python it complains about missing headers
  • GTK3Agg: no module named cairo. but I assue I'll hit the same issue as above
  • cairo: Cairo backend requires that pycairo is installed
  • emf:You must first install pyemf from http://pyemf.sf.net. Looks like it is supposed to do windowsey stuff so this may not be a good choice
  • gdk: no module called gobject
  • pdf: runs but doesn't draw
  • pgf: ditto
  • ps: ditto
  • svg: ditto
  • template: ditto .

The script I am using to test my backend is:

import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.show()

So far I have spent waaay too much time trying to get python3.2 and qt4 playing nice, and I just seem to be running into problems every way I turn. So instead of continuing with my trial and error approach I want to ask:

  1. What is the best option for Python3.2 and Pyramid?
  2. How do I make it happen?

In the meantime I will continue with the trial-and-error thing and update my question as I go.

NOTES on stuff I'm trying

For TkAgg:

since tkinter imports correctly I'm assuming it's installed correctly (if I'm wrong I suppose there's a way to test it?). This guy http://ask.sagemath.org/question/626/sage-python-import-matplotlib-no-module-named had a similar problem but Im sure his setup is different from mine. The solution was to find tkagg.py in the bowels of the python3.2 site packages directory and edit the offending import statement. I find it hard to believe that matplotlib ships broken (and I cant run the modified code suggested...)

For WX stuff:

wxPython for Python 3 says there is no support for python3 yet. Also wxPython has no mention of python3 on their site so I guess that's a no-go. running out of options :/

Cocoa:

Ditto: Writing Cocoa applications in Python 3

EMF:

ditto: http://pyemf.sourceforge.net/README.html

Community
  • 1
  • 1
Sheena
  • 15,590
  • 14
  • 75
  • 113
  • When you say e.g. Agg "does not draw" what do you mean? – Jouni K. Seppänen Oct 18 '12 at 07:22
  • @JouniK.Seppänen: absolutely nothing happens. No errors, no graph, no nothing – Sheena Oct 18 '12 at 07:55
  • Since this seems to happen for most of the backends, I'd assume the problem is with your drawing code. It seems unlikely that eight different backends all have bugs that results in them running with no errors, but result in an empty output. – Lennart Regebro Oct 18 '12 at 09:44
  • @JouniK.Seppänen: Do you mean the script I am using to test the backends? The code I used is included (right under the mighty list). I assume that's what you mean because otherwise you may be implying that matplotlib ships broken. The code I am using to test the backends came straight from the manual, and I am not the only person who has had the problem of a backend causing draw to output nothing. The general advice I have seen given is 'change your backend' – Sheena Oct 18 '12 at 12:57
  • Edit: Correcting myself. Surprised to just learn matplotlib has been ported to py3k. I didn't think it had been done yet. (http://pythonsprints.com/2011/04/8/matplotlib-python-3-thanks-cape-town-group/). – Chris Zeh Oct 18 '12 at 22:28
  • Since you mention you're using Ubuntu, this might not be helpful, but the following configuration just worked out of the box for me on Windows 7x64: python 3.2.3, numpy-1.6.2-MKL, scipy-0.11.0, matplotlib-1.2.0rc2 (the last three from http://www.lfd.uci.edu/~gohlke/pythonlibs/). Matplotlib is using TkAgg. – Hans Oct 19 '12 at 08:52
  • 6
    cairo, pdf and those kinds of backends are not for interactive plotting. They can produce figures and save it to a file, though. Just use `plt.savefig('test.pdf')` instead of the `plt.show()` in your script – David Zwicker Oct 19 '12 at 11:36
  • I have a PPA with daily development builds of matplotlib. Add that and install python3-matplotlib. This works for me. https://code.launchpad.net/~takluyver/+archive/matplotlib-daily – Thomas K Oct 19 '12 at 12:07
  • @DavidZwicker: Thanks (+1) that helps. It's not what I want to do in the long run but it is useful now. – Sheena Oct 21 '12 at 14:45
  • @ThomasK: what backend are you using? – Sheena Oct 21 '12 at 14:48
  • @Sheena: I'm letting it pick one automatically - seems to be `Qt4Agg` on this computer. I don't bother trying to install it in a virtualenv, I just use the Ubuntu packages. – Thomas K Oct 22 '12 at 11:36

5 Answers5

15

I ran into exact same issue, but after much efforts I managed to make TkAgg working (which was giving the import error for _tkagg). Little late, but someone might find it useful.

FWIW I resolved the issue as follows (to use TkAgg):

  • Installed tk-dev from package manager
  • Reinstalled python3-tk from package manager
  • Rebuilt and reinstalled matplotlib from source
  • (and of course setting TkAgg in matplotrc)

If you think you have tk-dev and python3-tk installed correctly, you need to rebuild and reinstall matplotlib, so that it generates _tkagg.py in your backends directory.

I'm using Python3.3 (numpy 1.7) on LM14. But I think it should work on Ubuntu/Python3.2

mg007
  • 2,888
  • 24
  • 29
2

I have been doing source builds of python 3.3.2 and matplotlib. As noted above, TkAgg works. If you are building everything from source:

  1. sudo apt-get install tk-dev
  2. Rebuild python3 (make clean; ./configure; make)
  3. Clean matplotlib (delete egg from virtualenv, remove cache objects from ~/.config/matplotlib)
  4. Rebuilt matplotlib from source (rm -r build/; python setup.py build; python setup.py install)

Finally got to see that silly box pop up wit my x=y line.

hours....

justinvf
  • 2,939
  • 2
  • 14
  • 9
1

Matplotlib seems to have taken a large step towards Python 3 with their 1.2.x release. matplotlib 1.2.0rc2 (+ numpy 1.7.0b2) is working for me with

  • Python 3.2.2 (virtualenv on a Linux Mint 12)
  • Python 3.3.0 (local installation from source)

In both cases, matplotlib reports "TkAgg" as the backend used.

Unfortunately, I have no clue if/how this might work with Pyramid.

Hans
  • 2,419
  • 2
  • 30
  • 37
0

I wrote an answer for ensuring matplotlib in a Python 3 virtualenv in Ubuntu 16.04 uses the TkAgg backend at https://askubuntu.com/a/785506/15003 that can be useful here. The upshot is to install tk-dev, activate the virtualenv and reinstall matplotlib in the virtualenv:

sudo apt install tk-dev
source venv/bin/activate
pip --no-cache-dir install -U --force-reinstall matplotlib

python -c 'import matplotlib as mpl; print(mpl.get_backend())' should now say TkAgg.

Community
  • 1
  • 1
edwinksl
  • 7,005
  • 4
  • 30
  • 36
0

For me, the following worked:

  • deactivate,
  • sudo apt-get install python3-matplotlib libfreetype6-dev
  • (activate virtualenv) source /home/...,
  • pip install numpy matplotlib (maybe uninstall and then install if you already have it, or maybe try creating a new venv at this point)
  • pip install --upgrade pip inside venv might also help (for some reason unknown to me), as it did with my installing scipy.

It now plots using virtualenv.

$ lsb_release -a (gives..)

Ubuntu 14.04.5 LTS

1Penko
  • 1
  • 2