Posts Tagged ‘kivympl’

Matplotlib backend kivy in Windows hands on

September 1st, 2015 7 comments

The backend implementations for matplotlib in kivy work just with kivy versions greater or equal than 1.9.1. It is required to install kivy in development mode following these steps: After step 10 you can additionally run

python install

The script kivy.bat creates an environment in which some python tools are ready to use, for instance pip. The next step is to download numpy and matplotlib from this binaries repositories: and Once downloaded install both using pip and then install kivy-garden and the matplotlib package for kivy.

pip install "numpy-1.10.0b1+mkl-cp27-none-win32.whl"
pip install "matplotlib-1.4.3-cp27-none-win32.whl"
pip install kivy-garden
garden install matplotlib

You can go now to the folder that by default garden packages are installed. In my case C:\Users\my_user\.kivy\garden\

cd garden.matplotlib/examples

And you should be able to see the matplotlib widget working on windows. Follow the steps from to create a shortcut to execute your kivy apps.

Now you can go directly to the garden path using a file explorer, do right click on and send to kivy-2.7.bat which will start the application.

Categories: kivy, matplotlib Tags: , ,

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 with matplotlib events connected

August 6th, 2015 No comments

NavigationToolbar is the last visual aspect from the backend that has been added with all the possible events connected to matplotlib. The main changes performed on the implementation involved to connect each one of the toolbar elements to the respective matplotlib calls. Additionally, the figure canvas is separated from the toolbar and both are children of a Float Layout inside an app generated via pyplot. The FigureCanvasKivy can be called independently of the NavigationToolbar but not in the opposite direction.

# updated version of the toolbar generation.
def _init_toolbar(self):
        basedir = os.path.join(rcParams['datapath'], 'images')
        actionview = ActionView()
        actionprevious = ActionPrevious(title="Navigation", with_previous=False)
        actionoverflow = ActionOverflow()
        actionview.use_separator = True
        for text, tooltip_text, image_file, callback in self.toolitems:
            if text is None:
            fname = os.path.join(basedir, image_file + '.png')
            if text in ['Pan', 'Zoom']:
                action_button = ActionToggleButton(text=text, icon=fname)
                action_button = ActionButton(text=text, icon=fname)
            action_button.bind(on_press=getattr(self, callback))

Navigation Toolbar Kivy

For matplotlib users the interface can look very familiar. However, I consider a brief explanation is necessary on the function of each button:

Home: Resets the plot axes to the initial state.
Left: Undo an operation performed.
Right: Redo an operation performed.
Pan: Allows to drag the plot.
Zoom: Allows to define a rectangular area to zoom in.
Configure: Not implemented yet but, loads a pop up for repositioning elements.
Save: Loads a Save Dialog to generate an image.
Configure and Save will be added at some point the next week.

Categories: GSOC2015, kivy, matplotlib Tags: , ,

Improvements to the RendererKivy (Part 2)

July 22nd, 2015 No comments

The following modifications have been implemented in these days:

  • Fixed problem when rendering concave polygons. It can be seen from previous images that in the case of an arrow and a wedge there was a defect on the rendering due to the triangulation algorithm to fill it. The mesh renderer uses “triangle_fan” and since all the points form triangles always with the hub in concave polygons this was a problem. It was fixed by splitting concave polygons into convex ones. This was implemented using Tesselator a utility class in the graphics kivy package.
  • Path was being closed when it should not. It has been fixed that a path line was being closed by default, the initial point was removed so the line does not try to close a path when it is not necessary.
  • Elements out of the figure axes are not rendered. The problem here is that everything was being drawn in the same layer. A stencil widget is used as a mask so everything out of bounds it is hidden. In the second figure, 4 stencil instructions are created to show just the graphics instructions visible in the clip rectangle.
  • Focus behavior was added to the FigureCanvasKivy. Keyboard events are automatically bound by the focus behavior.
  • Added support for multiple dash length and offset in a same line. Line can receive a list with the values of length and offset for the respective rendering. For instance [10, 5, 20, 10] would create a line with 10 visible points then 5 hidden 20 visible and 10 hidden. This can be seen on the figure of the sin function below

dynamic dashes in a line4 stencil widgetsartist demointegral demo

Categories: GSOC2015, kivy, matplotlib Tags: , ,

Improvements to the RendererKivy

July 17th, 2015 No comments

The main improvements to the Renderer involved to run a test for the backend_kivy with as many as possible examples. From the test we figured out there were some implementations missed. The following are the main improvements done to the Renderer this week:

  • The rendering of the text is not blurry anymore. This was fixed by working with int values instead of float, it seems like opengl was trying to give some percentage of the pixel value to the previous and next pixel giving this effect.
  • Math text such as integrals, derivatives and so on can be added to the figure canvas. Matplotlib provides a very useful math text parser which receives the string and returns an object that can be embedded on the kivy canvas.
  • Images as textures can be added inside a Figure and outside as well. Matplotlib allows to generate some interpolation graphs which can be inserted into the axes and give some fancy visualization of data. Since color in kivy’s renderering is multiplicative, you would want the color to be white or the texture will be ‘tinted’ as it will be something other than 1.* color channel at pixel. Initially the problem was that the texture was invisible but it was solved by changing the Color to white as it is explained before. When a figure is not present the image is added directly to the canvas as a texture.

Some examples can be seen in the pictures below. Additionally there are two known problems, the first one is that the rendering is out of the axes and the second one is related to the path and mesh creation. This problems can be seen specifically in the integral example and the electric dipole with the arrows and magnetic fields.

gradient bar imshow without figure

Read more…

Categories: GSOC2015, kivy, matplotlib Tags: , ,

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()
        for text, tooltip_text, image_file, callback in self.toolitems:
            if text is None:
                # insert a separator
            fname = os.path.join(basedir, image_file + '.png')
            action_button = ActionButton(text=text, icon=fname)
        self.canvas.add_widget(actionbar, canvas='after')

Navigation Toolbar Kivy MPL