Archive

Archive for the ‘backend’ Category

backend_kivy and backend_kivyagg working with matplotlib 1.4.3, python2 and python3.

August 6th, 2015 No comments

Initially backend_kivy was implemented using matplotlib 1.5.1dev which is an unstable release. Small modifications were made based on the changes matplotlib has between 1.4.3 (stable) and 1.5.1dev. Some of the main points that were updated were the generation of MathText and the lines rendering in draw_path. Additionally in the case of backend_kivyagg a read-only memory view was sent from the agg renderer to the kivy backend, a writable copy had to be created due to an exception in cython for loading read-only buffers. Some other improvements involve a bug when the application was being closed which was fixed by adding an on_close event in the app and setting the global var app to None, another improvement is the generation of images from the plots. In the case of backend_kivy the figure canvas widget is being exported to png using the export_to_png(filename). A bug was reported when the FigureCanvasKivy is added to a BorderLayout and the window is resized. For instance if we have a button on top of the figure canvas, when resized the figure canvas is resized to the size of the window moving the button up but then is scaled down again and the dimensions are right. This arises in the backend_kivy due to a small delay that keeps the calculation of the positions in this case of the button out of the canvas. In backend_kivyagg this does not happen due to a fast transition so the button position is calculated correctly. (still an open issue and working on that for backend_kivy.)

Navigation Toolbar for #kivympl using an Action Bar

July 17th, 2015 No comments

After my last post I started the implementation of a Navigation Toolbar for the backend. For the Navigation Toolbar for kivy we decided to experiment with an ActionBar from the set of kivy widgets. It is a very early version and not implemented at all, but this is a snippet of how it would be the layout and the result can be seen on the image below. The only element being used for now are the icons for the action buttons so we can have an idea of how it would look like.  The behavior of each one of the items will be defined on the callbacks. The NavigationToolbar2Kivy extends from NavigationToolbar2 which is the base class.

def _init_toolbar(self):
        basedir = os.path.join(rcParams['datapath'], 'images')
        actionbar = ActionBar(pos_hint={'bottom': 1.0})
        actionview = ActionView()
        actionbar.add_widget(actionview)
        for text, tooltip_text, image_file, callback in self.toolitems:
            if text is None:
                # insert a separator
                continue
            fname = os.path.join(basedir, image_file + '.png')
            action_button = ActionButton(text=text, icon=fname)
            actionview.add_widget(action_button)
        self.canvas.add_widget(actionbar, canvas='after')

Navigation Toolbar Kivy MPL

Kivy matplotlib backend with right text positioning and rendering.

July 8th, 2015 No comments

In order to fix the problem of the layout of the text inside the canvas, an important function had to be overwritten with the information of the bounding box for each text inside it. The function is get_text_width_height_descent in this, one calculates the width and height and return them. Inside matplotlib these values are important to calculate the positioning of the elements according to the layout set by the user. Additionally, in the draw_text method transformations and styling is applied to the text. At the end we can have a working backend with different fonts and styles that are mapped between matplotlib and kivy capabilities.

Another inconvenient in the rendering of the graphs were some glitches while drawing. I was calculating u and v to create the mesh for the polygons when this was not really necessary. After some debugging and analysis of the vertices the final implementation generate the meshes and lines for all the elements in the canvas as follow:

Screenshot from 2015-07-07 12:18:28 example mpl kivy

As can be seen, now it is getting closer to how the static image implementation looks like from the previous post.

Kivy backend using Line and Mesh graphics instructions

June 26th, 2015 2 comments

This is the first prototype for the backend, points are extracted from the path an transformed into polygons or lines. Line and Mesh are used in the kivy side to render these objects in a widget canvas. Labels are used for displaying text. Below some examples can be found. The lines are not well defined and the next is to optimize this drawing as well as the text. Some attributes should be added to the labels, positioning is another problem.

matplotlib examples kivy matplotlib kivy matplotlib kivy matplotlib kivy

Read more…

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.