Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create dynamic updated graph with Python

I need your help to write a script in Python that will take dynamically changed data, the source of data is not matter here, and display graph on the screen.

I know how to use matplotlib, but the problem with matplotlib, that I can display graph only once, at the end of the script. I need to be able not only to display graph one time, but also update it on the fly, each time when data changes.

I found that it is possible to use wxPython with matplotlib to do this, but it is little bit complicate to do this for me, because i am not familiar with wxPython at all.

So I will be very happy if someone will show me simple example how to use wxPython with matplotlib to show and update simple graph. Or, if it is some other way to do this, it will be good to me too.

PS:
Ok, since no one answered and looked at matplotlib help noticed by @janislaw and wrote some code. This is some dummy example:


import time
import matplotlib.pyplot as plt


def data_gen():
    a=data_gen.a
    if a>10:
        data_gen.a=1
    data_gen.a=data_gen.a+1
    return range (a,a+10)

def run(*args):
    background = fig.canvas.copy_from_bbox(ax.bbox)

    while 1:
        time.sleep(0.1)
        # restore the clean slate background
        fig.canvas.restore_region(background)
        # update the data
        ydata = data_gen()
        xdata=range(len(ydata))

        line.set_data(xdata, ydata)

        # just draw the animated artist
        ax.draw_artist(line)
        # just redraw the axes rectangle
        fig.canvas.blit(ax.bbox)

data_gen.a=1
fig = plt.figure()
ax = fig.add_subplot(111)
line, = ax.plot([], [], animated=True)
ax.set_ylim(0, 20)
ax.set_xlim(0, 10)
ax.grid()

manager = plt.get_current_fig_manager()
manager.window.after(100, run)

plt.show()

This implementation have problems, like script stops if you trying to move the window. But basically it can be used.

like image 807
Alex Avatar asked Apr 11 '11 08:04

Alex


People also ask

How do you make a dynamic plot in Python?

Practical Data Science using Python Make a list of data points and colors. Plot the bars with data and colors, using bar() method. Using FuncAnimation() class, make an animation by repeatedly calling a function, animation, that sets the height of the bar and facecolor of the bars.

How do you update a graph in Python?

Using matplotlib. pyplot. draw(), It is used to update a figure that has been changed. It will redraw the current figure.


2 Answers

Here is a class I wrote that handles this issue. It takes a matplotlib figure that you pass to it and places it in a GUI window. Its in its own thread so that it stays responsive even when your program is busy.

import Tkinter
import threading
import matplotlib
import matplotlib.backends.backend_tkagg

class Plotter():
    def __init__(self,fig):
        self.root = Tkinter.Tk()
        self.root.state("zoomed")

        self.fig = fig
        t = threading.Thread(target=self.PlottingThread,args=(fig,))
        t.start()

    def PlottingThread(self,fig):     
        canvas = matplotlib.backends.backend_tkagg.FigureCanvasTkAgg(fig, master=self.root)
        canvas.show()
        canvas.get_tk_widget().pack(side=Tkinter.TOP, fill=Tkinter.BOTH, expand=1)

        toolbar = matplotlib.backends.backend_tkagg.NavigationToolbar2TkAgg(canvas, self.root)
        toolbar.update()
        canvas._tkcanvas.pack(side=Tkinter.TOP, fill=Tkinter.BOTH, expand=1)

        self.root.mainloop()

In your code, you need to initialize the plotter like this:

import pylab
fig = matplotlib.pyplot.figure()
Plotter(fig)

Then you can plot to it like this:

fig.gca().clear()
fig.gca().plot([1,2,3],[4,5,6])
fig.canvas.draw()
like image 83
Emma Avatar answered Oct 16 '22 16:10

Emma


As an alternative to matplotlib, the Chaco library provides nice graphing capabilities and is in some ways better-suited for live plotting.

See some screenshots here, and in particular, see these examples:

  • data_stream.py

  • spectrum.py

Chaco has backends for qt and wx, so it handles the underlying details for you rather nicely most of the time.

like image 38
Isaiah Norton Avatar answered Oct 16 '22 17:10

Isaiah Norton