Skip to content

Commit

Permalink
fixing gtk3agg error in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias47n9e committed Dec 26, 2014
1 parent 18765ee commit fea42a0
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 21 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ This reposistory is monitored by Landscape (https://landscape.io/).
[![Code Health](https://landscape.io/github/spiessbuerger/GTK3-Matplotlib-Cookbook/master/landscape.png)](https://landscape.io/github/spiessbuerger/GTK3-Matplotlib-Cookbook/master)

##News and Comments
###2014-12-26
I have been busy on some other projects lately. Revisiting this project, I had some trouble running my examples on Ubuntu 14.04 and the current master of Matplotlib (1.5.dev1). The problem seems to be in the *gtk3agg* backend. I get the following error: *NotImplementedError: Surface.create_for_data: Not Implemented yet.*. Not sure how many people have trouble with this, but it is mentioned in different places (e.g. http://matplotlib.1069221.n5.nabble.com/Matplotlib-py3-gtk3-td42953.html). Getting the plots to work again just requires you to switch rendering backends:

from matplotlib.backends.backend_gtk3cairo import FigureCanvasGTK3Cairo as FigureCanvas

###2014-07-26
The fourth chapter about entering data is finished. I also decided to start code reviewing with Landscape. The score was 63 % on standard settings and 23 % on high (meaning very strict code reviewing). The low score is mostly caused by the *conf.py* file from Sphinx, my usage of "old classes" and Landscape complaining about the Gtk specific code. I will try to deal with the issues first, before uploading more chapters.

Expand Down
4 changes: 3 additions & 1 deletion examples/enteringdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

from gi.repository import Gtk
from matplotlib.figure import Figure
from matplotlib.backends.backend_gtk3agg import FigureCanvasGTK3Agg as FigureCanvas
#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

class MainClass(Gtk.Window):
def __init__(self):
Expand Down
4 changes: 3 additions & 1 deletion examples/matplotlibwindow-glade.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
from matplotlib.figure import Figure
from numpy import arange, pi, random, linspace
import matplotlib.cm as cm
from matplotlib.backends.backend_gtk3agg import FigureCanvasGTK3Agg as FigureCanvas
#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

class Signals:
def on_window1_destroy(self, widget):
Expand Down
4 changes: 3 additions & 1 deletion examples/mpl-ntb-glade.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

from matplotlib.figure import Figure
from numpy import sin, cos, pi, linspace
from matplotlib.backends.backend_gtk3agg import FigureCanvasGTK3Agg as FigureCanvas
#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
from matplotlib.backends.backend_gtk3 import NavigationToolbar2GTK3 as NavigationToolbar

class Signals:
Expand Down
4 changes: 3 additions & 1 deletion examples/mpl-ntb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from gi.repository import Gtk
from matplotlib.figure import Figure
from numpy import sin, cos, pi, linspace
from matplotlib.backends.backend_gtk3agg import FigureCanvasGTK3Agg as FigureCanvas
#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
from matplotlib.backends.backend_gtk3 import NavigationToolbar2GTK3 as NavigationToolbar

myfirstwindow = Gtk.Window()
Expand Down
4 changes: 3 additions & 1 deletion examples/mpl-zoom.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from gi.repository import Gtk
from matplotlib.figure import Figure
from numpy import random
from matplotlib.backends.backend_gtk3agg import FigureCanvasGTK3Agg as FigureCanvas
#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
from matplotlib.backends.backend_gtk3 import NavigationToolbar2GTK3 as NavigationToolbar
from matplotlib.patches import Rectangle

Expand Down
4 changes: 3 additions & 1 deletion source/enteringdata.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ The program requires the following imports:
from gi.repository import Gtk
from matplotlib.figure import Figure
from matplotlib.backends.backend_gtk3agg import FigureCanvasGTK3Agg as FigureCanvas
#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

I decided to put the whole program into a class. First we have to set up the window, the layout and a *Gtk.Toolbar*. The two lines starting with *self.context* ensure that the toolbar will be styled similar to your operating system.

Expand Down
17 changes: 12 additions & 5 deletions source/hello-plot.rst
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ We will look at an example that will produce a random radial plot on each applic
from matplotlib.figure import Figure
from numpy import arange, pi, random, linspace
import matplotlib.cm as cm
from matplotlib.backends.backend_gtk3agg import FigureCanvasGTK3Agg as FigureCanvas
#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)
Expand Down Expand Up @@ -195,14 +197,16 @@ We will look at an example that will produce a random radial plot on each applic

The first window with an embedded Matplotlib-graph as it renders in Ubuntu 14.04.

As you probably noticed we imported a few more modules. The module *matplotlib.figure* is required to render the graph. We need some functions from *NumPy* for evenly dividing an interval (*numpy.arange*), the value for the constant pi (*numpy.pi*), a function for random numbers (*numpy.random*) and a function that returns evenly spaced numbers in an interval (*numpy.linspace*). We also need the colormap function (*matplotlib.cm*). The container in which the graph is rendered is the *FigureCanvasGTK3Agg*.
As you probably noticed we imported a few more modules. The module *matplotlib.figure* is required to render the graph. We need some functions from *NumPy* for evenly dividing an interval (*numpy.arange*), the value for the constant pi (*numpy.pi*), a function for random numbers (*numpy.random*) and a function that returns evenly spaced numbers in an interval (*numpy.linspace*). We also need the colormap function (*matplotlib.cm*). The container in which the graph is rendered is the *FigureCanvasGTK3Agg* or *FigureCanvasGTK3Cairo*.

::

from matplotlib.figure import Figure
from numpy import arange, pi, random, linspace
import matplotlib.cm as cm
from matplotlib.backends.backend_gtk3agg import FigureCanvasGTK3Agg as FigureCanvas
#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
In order to give the plot a sufficient default size we add this line:

Expand Down Expand Up @@ -258,7 +262,7 @@ Then we just have to create a *GtkScrolledWindow*, and add it to our *GtkWindow*
sw = Gtk.ScrolledWindow()
myfirstwindow.add(sw)
Finally we can create an instance of a *FigureCanvasGTK3Agg* with our figure included in it. Then we set the size of the canvas and add embed it into the *GtkScrolledWindow*.
Finally we can create an instance of a *FigureCanvasGTK3Agg* or *FigureCanvasGTK3Cairo* with our figure included in it. Then we set the size of the canvas and add embed it into the *GtkScrolledWindow*.

::

Expand Down Expand Up @@ -316,7 +320,9 @@ Starting with the code from the previous examples we only have to make slight ch
from matplotlib.figure import Figure
from numpy import arange, pi, random, linspace
import matplotlib.cm as cm
from matplotlib.backends.backend_gtk3agg import FigureCanvasGTK3Agg as FigureCanvas
#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
class Signals:
def on_window1_destroy(self, widget):
Expand Down Expand Up @@ -372,6 +378,7 @@ Further Reading
- `Numpy: random <http://docs.scipy.org/doc/numpy/reference/routines.random.html>`_
- `Numpy: random.rand <http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.rand.html#numpy.random.rand>`_
- FigureCanvasGTK3Agg documentation [[2014-06-21 Find link for documentation]]
- FigureCanvasGTK3Cairo documentaion [Link needed]
- `Stackoverflow question about subplot grids <http://stackoverflow.com/questions/3584805/in-matplotlib-what-does-111-means-in-fig-add-subplot111>`_


6 changes: 6 additions & 0 deletions source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ Additional reading:

News and Comments
-----------------
2014-12-26
^^^^^^^^^^
I have been busy on some other projects lately. Revisiting this project, I had some trouble running my examples on Ubuntu 14.04 and the current master of Matplotlib (1.5.dev1). The problem seems to be in the *gtk3agg* backend. I get the following error: *NotImplementedError: Surface.create_for_data: Not Implemented yet.*. Not sure how many people have trouble with this, but it is mentioned in different places (e.g. http://matplotlib.1069221.n5.nabble.com/Matplotlib-py3-gtk3-td42953.html). Getting the plots to work again just requires you to switch rendering backends:

from matplotlib.backends.backend_gtk3cairo import FigureCanvasGTK3Cairo as FigureCanvas

2014-07-26
^^^^^^^^^^
The fourth chapter about entering data is finished. I also decided to start code reviewing with Landscape. The score was 63 % on standard settings and 23 % on high (meaning very strict code reviewing). The low score is mostly caused by the *conf.py* file from Sphinx, my usage of "old classes" and Landscape complaining about the Gtk specific code. I will try to deal with the issues first, before uploading more chapters.
Expand Down
15 changes: 9 additions & 6 deletions source/matplotlib-toolbar.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ Assuming that you worked through the first chapter, you should already be slight
from matplotlib.figure import Figure
from numpy import sin, cos, pi, linspace
from matplotlib.backends.backend_gtk3agg import FigureCanvasGTK3Agg as FigureCanvas
from matplotlib.backends.backend_gtk3 import NavigationToolbar2GTK3 as NavigationToolbar
#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 from matplotlib.backends.backend_gtk3 import NavigationToolbar2GTK3 as NavigationToolbar

The next lines of code create a *GtkWindow* and in addition to lines from the first chapter we will also define a title and a icon (from a file in the same directory) for our program window:

Expand Down Expand Up @@ -138,8 +139,9 @@ The last line of the code just show the window and start the main program loop.
from matplotlib.figure import Figure
from numpy import sin, cos, pi, linspace
from matplotlib.backends.backend_gtk3agg import FigureCanvasGTK3Agg as FigureCanvas
from matplotlib.backends.backend_gtk3 import NavigationToolbar2GTK3 as NavigationToolbar
#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 from matplotlib.backends.backend_gtk3 import NavigationToolbar2GTK3 as NavigationToolbar
myfirstwindow = Gtk.Window()
myfirstwindow.connect("delete-event", Gtk.main_quit)
Expand Down Expand Up @@ -271,8 +273,9 @@ The Python code from above only needs minor adjustments. The two containers (*Gt

from matplotlib.figure import Figure
from numpy import sin, cos, pi, linspace
from matplotlib.backends.backend_gtk3agg import FigureCanvasGTK3Agg as FigureCanvas
from matplotlib.backends.backend_gtk3 import NavigationToolbar2GTK3 as NavigationToolbar
#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 from matplotlib.backends.backend_gtk3 import NavigationToolbar2GTK3 as NavigationToolbar

class Signals:
def on_window1_destroy(self, widget):
Expand Down
10 changes: 6 additions & 4 deletions source/zooming.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ At the beginning of the code we will import the usual modules. New is the import
from gi.repository import Gtk
from matplotlib.figure import Figure
from numpy import random
from matplotlib.backends.backend_gtk3agg import FigureCanvasGTK3Agg as FigureCanvas
from matplotlib.backends.backend_gtk3 import NavigationToolbar2GTK3 as NavigationToolbar
#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 from matplotlib.backends.backend_gtk3 import NavigationToolbar2GTK3 as NavigationToolbar
from matplotlib.patches import Rectangle

The *DrawPoints* class will do most of the work in this example. On init, we will define some variables and set up the 2 subplots. Each subplot has its own drawing function, which clears the subplot, before drawing. The *zoom* function recycles the *draw* and *drawzoom* function.
Expand Down Expand Up @@ -130,8 +131,9 @@ This is the code of the whole example:
from gi.repository import Gtk
from matplotlib.figure import Figure
from numpy import random
from matplotlib.backends.backend_gtk3agg import FigureCanvasGTK3Agg as FigureCanvas
from matplotlib.backends.backend_gtk3 import NavigationToolbar2GTK3 as NavigationToolbar
#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 from matplotlib.backends.backend_gtk3 import NavigationToolbar2GTK3 as NavigationToolbar
from matplotlib.patches import Rectangle

class DrawPoints:
Expand Down

0 comments on commit fea42a0

Please sign in to comment.