Posts Tagged ‘matplotlib’

Writing a matplotlib renderer for Kivy

June 24th, 2015 No comments

A Renderer is a class that knows all the graphics instructions to draw in a canvas. The first implementation involved to use the canonical agg renderer to get a figure which we could texture in a rectangle, this is fully explained in a previous post. The objective of my implementation is to have a backend implemented using kivy graphics instructions. The RendererBase class defines all the required methods to do so. RendererKivy extends from it and implements draw_path, other 5 different methods can be implemented but the most important is draw_path.

In this method all the required information to draw on the canvas is provided however there is another class as well required to change the style of elements rendered. This class is GraphicsContext which is a middle class between a renderer and an Artist. Artist is one of the layers in the hierarchical architecture of matplotlib, the other two layers are backend and scripting. Basically, everything that can be drawn in a canvas is an Artist object such as Axis, Tick, Figure, etc. A GraphicsContextKivy is defined which extends from GraphicsContextBase and specifically translates matplotlib context instructions such as line width, line style, caps, joints, colors, etc. into the equivalent in Kivy. As can be seen in the figure below the lines are dashed and the foreground of the canvas was changed.

backend kivy

At the moment there are three issues: the first one is that kivy implements caps for lines {‘square’, ’round’, ‘none’} and matplotlib {‘butt’, ‘projecting’, ’round’}. the second issue is that dashes in kivy are supported depending on the line width and finally, kivy does not support dynamic values for dash offset and dash length. The renderer, receives this object GraphicsContext and applies such styles defined there into kivy context and vertex instructions. As can be seen in the figure a very simple drawing of the information received in the draw_path is performed. Basically lines are created with all the vertex received but a path is more complex than that and represents a series of possibly disconnected, possibly closed, line and curve segments. The next post will be about this class Path and which kivy instructions are being used to implement the renderer.

Tentative Matplotlib Integration

June 1st, 2015 No comments

The purpose of this post is to introduce the main goal of my google summer of code project. The second goal is to support ink processing in a more easier way. Both together can make a difference from other backends implementations. First, I am going to start by summarizing what I know so far about how matplotlib works. The package matplotlib.pyplot is the root of a tree hierarchy, they call it “state-machine environment”. The next step is to create a figure which internally will create the structure to handle the rest of the objects inside the figure. The next level in the hierarchy is Axes, Axis and Artist.

Axes is the whole figure area where the Axis and plot are going to be rendered. As I can see it Axes is kind of a subplot since a Figure can handle more than one Axes but it needs at least one. Axes relevant methods are set_title(), set_xlabel() and set_ylabel(). Axis refers to the coordinates of the graph two main attributes of this class are Locator and Formatter. This class handles the ticks and labels over the coordinates as well as the mapping with the plotting area. Finally, Artist is the class that takes care of the style of the graph –> color, line styles, fonts, etc.

GIven this, a backend is an abstract class that allows to render matplotlib plots using different frontend platforms. I am going to be working on writting one for Kivy starting next week. There are two types of backends: user interface and non-interactive. The user interface one provides ways to visually interact with the data using different interaction techniques usually mouse and keyboard. The non-interactive are the ones that export the plot to a hard format such as png, pdf or svg. There are two rendering engines one given by vector renderers and one given by raster renderers. The main difference between both is that vector use primitive geometric elements such as lines and polygons and raster which are an array of pixels. The very first one can scale and the second one are resolution dependent.

For the implementation of kivy backend I am gonna start implementing a RendererBase which needs a GraphicsContext. To the best of my knowledge while creating the Agg implementation I would pass Agg’s graphics context otherwise I would pass for this purpose. The methods to implement in this class are draw_path, draw_image, draw_gouraud_triangle, there are some other methods as well that can be implemented for optimization reasons.

A GraphicsContext needs to be instantiated and mapped to the matplotlib base classes methods and attributes. For instance map the color of a line with a function that gives what is that color in the kivy context. Another example could be to tell matplotlib use normal line or anti aliased implementation. FigureCanvas should map kivy canvas in addition with the set of events that would need to be overwritten in the FigureCanvas implementation. FigureManager creates the window for the look of it. There is a class called timer which seems to handle the delay time between events.

For the Show() one can overwrite the default mainloop to use the kivy mainloop

A very first outline can be seen here: Read more…

Categories: GSOC2015 Tags: ,