Matplotlib redraw plots

Date: March 5th 2016
Last updated: March 5th 2016

This is my cheap and easy approach to redrawing plots using matplotlib in kivy. This example completely removes the widget and redraws everything. I imagine it is a horribly inefficient method, but it works. As this was my first success at redrawing plots, I'm taking it as a win. I'll look for alternative methods in the future. There are some additional comments inside (e.g. what I tried and what failed).

Trialled many methods for clearing existing widget.
None have worked so far.... until... I decided to cheat!

In this example I have simply provided a redraw of everything
which will be the most inefficient method of acheiving the
desired result.

It seems fast enough on my desktop, but it will most likely be
slower on a phone or tablet.

import matplotlib
import matplotlib.pyplot as plt
from import FigureCanvas
import kivy
from import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
import matplotlib.mlab as mlab
from random import gauss
import numpy as np

class DrawPlot(BoxLayout):
    def __init__(self, **kwargs):
        super(DrawPlot, self).__init__(**kwargs)

    def get_data(self):
        x = [gauss(100, 15) for i in range(100)]
        return x

    def draw_plot_frame(self):
        self.fig, = plt.subplots()[0, 200])
        canvas =  FigureCanvas(figure=self.fig)
        self.add_widget(canvas, 1)

    def plot(self):
        x = self.get_data() # Doesnt work: first click rm widget, second click exits program
        #self.fig.remove() # Doesnt work: cannot remove artist
        #self.fig.close() #Doesnt work: figure object has no attr 'close'
        #self.fig.clf() #Doesnt work: clears the entire figure widget
        n, bins, patches =, 50, normed=1, facecolor='white', alpha=0.3)
        pdf = mlab.normpdf(bins, 100, 15), pdf, 'r-', linewidth=2)
        #self.fig.clear() #Doesnt work: clears the figure widget
        #self.draw_plot_frame() #Doesnt work: adds a NEW blank graph window
        canvas = self.fig.canvas
        # Tried to replace the previous line with the following line
        #self.fig.canvas.draw() #Doesnt work: adds hist bins to existing figure

    def cheatmethod(self):
        canvas = self.fig.canvas

class SampleApp(App):
    def build(self):
        return DrawPlot()

if __name__ == '__main__':


#:kivy 1.9.1
    orientation: 'vertical'

        orientation: 'vertical'
        size_hint: 1, None
        height: main_title.height

            id: main_title
            text: "Plotting using Matplotlib in Kivy"
            font_size: '35sp'
            size_hint: 1, None
            height: self.height

            size_hint_x: 0.8
            source: root.draw_plot_frame()

        orientation: 'horizontal'
        size_hint: 1, None
        height: playbutton.height
            id: playbutton
            text: 'play'
            size_hint: 1, None
            height: self.height
            on_press: root.cheatmethod() #<==== redraw everything on every click!! 

            text: 'reset'
            size_hint: 1, None
            text: 'quit'
            size_hint: 1, None

The first screen has an empty plot.
Opening window screenshot

The next screen is after the first click on the 'play' button.

histogram redraw screenshot

On the next click, the graph widget is redrawn.

histogram redraw2

And one more time.

histogram redraw 3

results matching ""

    No results matching ""