Backend for kivy in matplotlib, first steps

A backend in matplotlib is a class that creates a link between matplotlib and any frontend framework in which the data can be rendered. Matplotlib provides a template which works as an excellent starting point for anyone who would like to develop a backend. It is a class which by itself works as a backend but do nothing. The very first thing I have been doing the last three days is reading the comments on this file and analyzing how backends for other frameworks are implemented.

The first objective towards implementing a fully functional backend in kivy is to write one with a canonical renderer such as Agg. The first class I modified from the list of classes that the file provides is FigureCanvas which I renamed FigureCanvasKivyAgg. This class extends from a FigureCanvasAgg and a Kivy Widget. The reason why it extends from FigureCanvasAgg is because this class returns an Agg renderer which will contain the graphical information of the graph. Additionally, it extends from Widget because it is handy to add a widget when someone wants to embed matplotlib graphs or use it with pyplot to provide a fully functional App.

python ../../../examples/mpl/test_plt.py -dmodule://backend_kivy

In FigureCanvasKivyAgg two methods were overridden. The first one is draw which defines the context in which the renderer will be placed. From the FigureCanvasAgg I get the rgba buffer in order to place that in a texture which can be then added to a widget, as you can see in the following snippet.

The second method is blit, in which given a bounding box defines the rectangular area of the graph to be drawn.

self.blitbox = bbox

FigureCanvasKivyAgg can be embed in any Kivy application given it is a Widget

Matplotlib graphs can be embed or run as kivy application. Pyplot is the responsible to allow the total independence from the way the information is visualized. This is an example code in which no single line with kivy is shown. However, internally will create and run an App:

Pyplot looks for a method in the backend call new_figure_manager in this method a figure is created with the information coming from the main app, in the case above figure. Once Figure created a FigureCanvas is created and in this instantiation and application is setup. All this happens until plt.draw(), which calls the draw method in FigureCanvas. Finally to run the app with plt.show() is needed to overwrite the show function in the FigureManager and send the app to run.

For the complete file you can check the code on github. This is the branch where implemented:

how do I make it work with real-time and multiscreen? any good references,
I am working on image processing which requires Kivy to work on the front end numpy and matplotlib support it.
this post was helped me a lot …