Making Matplotlib and GTK3 work on python3 windows

1

I'm trying to make GTK3 and Python3 work under windows to my project.

I have an continuum anaconda setup with a 32-bit python 3.4 and Matplotib via conda install matplotlib.

I've installed PyGobject(https://sourceforge.net/projects/pygobjectwin32/) and installed GTK+ / Glade via the installer.

The basic exemple from the GTK3 tutorial works well (empty screen)

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

win = Gtk.Window()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

I want now to embed matplotlib in gtk, I got the example from matplotlib (http://matplotlib.org/examples/user_interfaces/embedding_in_gtk3.html)

I saw then that I needed cairocffi because some incompabilities. (PyCairo has no support for a matplotlib function)

I got the windows binaries for cffi from Gohlke

And finnaly did a

pip install cairocffi

And now I just get a python.exe stopped working.

Tried with GTK3agg and GTK3Cairo backends and I have the same result

Looking around I found that maybe the cairo version is outdated for the functions used by matplotlib, but I dont know how to proceed.

Cairocffi works if I try running something else.

More information (from the comment below):

I still got an unhandled win32 error. I managed to open the error and it says:

Unhandled exception at 0x08CF6D58 (libcairo-2.dll) in python.exe:
0xC0000005: Access violation reading location 0x000000A8.
If there is a handler for this exception, the program may be safely continued. 

It just crashes...

python
matplotlib
networkx
gtk3
cairo
asked on Stack Overflow Jul 20, 2016 by Luc • edited Jul 21, 2016 by jcoppens

2 Answers

0

I've had my share of problems using matplotlib in Python3 + Gtk3. I found this cookbook page with working examples. Try to run the examples in the cookbook - particularly the simplest one:

#!/usr/bin/python3

from gi.repository import Gtk

from matplotlib.figure import Figure
from numpy import arange, pi, random, linspace
import matplotlib.cm as cm
#Possibly this rendering backend is broken currently
#from matplotlib.backends.backend_gtk3agg import FigureCanvasGTK3Agg as FigureCanvas
from matplotlib.backends.backend_gtk3cairo import FigureCanvasGTK3Cairo as FigureCanvas

myfirstwindow = Gtk.Window()
myfirstwindow.connect("delete-event", Gtk.main_quit)
myfirstwindow.set_default_size(400, 400)

fig = Figure(figsize=(5,5), dpi=100)
ax = fig.add_subplot(111, projection='polar')

N = 20
theta = linspace(0.0, 2 * pi, N, endpoint=False)
radii = 10 * random.rand(N)
width = pi / 4 * random.rand(N)

bars = ax.bar(theta, radii, width=width, bottom=0.0)

for r, bar in zip(radii, bars):
    bar.set_facecolor(cm.jet(r / 10.))
    bar.set_alpha(0.5)

ax.plot()

sw = Gtk.ScrolledWindow()
myfirstwindow.add(sw) 

canvas = FigureCanvas(fig)
canvas.set_size_request(400,400)
sw.add_with_viewport(canvas)

myfirstwindow.show_all()
Gtk.main()

Also, not that you need a fairly recent version of matplotlib to make things work on Python3.

If you still have problems, please show us the complete error message.

Note: I tested this on Linux (don't have Windows), but, from you description of problems, the issue is (was) a common one.

answered on Stack Overflow Jul 20, 2016 by jcoppens
0

I have the same issue on Windows since years. The documentation of matplotlib 2.0.0 realease states that Gtk3 backend is not supported on Windows. Recently, I had an issue running the Gtk3Agg backend under Linux (Ubuntu). In both cases, it is always related to Cairo.

Thus, I wrote my own implementation of this backend, you can find it here. import the FigureCanvasGtk3Agg from my module and use it the same way as the official one. It lacks some features, but, if you just want to display a plot, it'll do the job.

You can try the module by running it, it should display a simple colorful graph in a window. I tried it under both Linux and Windows and had no issue.

How does it works:

The trick is to avoid importing Cairo in Python3 as it usually doesn't work (to my experience). It is done by using a GdkPixbuf.

Then Gdk.cairo_set_source_pixbuf does the rest of the job along with calling two methods of the cairo context provided by Gtk in the 'draw-event' callback. Cairo is never imported in the module.

answered on Stack Overflow Sep 7, 2018 by Bigfoot

User contributions licensed under CC BY-SA 3.0