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
I had the need to create a graph that updates with time. The most convenient solution I came up was to create a new graph each time. The issue was that the script won't be executed after the first graph is created, unless the window is closed manually. That issue was avoided by turning the interactive mode on as shown below
for i in range(0,100):
fig1 = plt.figure(num=1,clear=True) # a figure is created with the id of 1
createFigure(fig=fig1,id=1) # calls a function built by me which would insert data such that figure is 3d scatterplot
plt.ion() # this turns the interactive mode on
plt.show() # create the graph
plt.pause(2) # pause the script for 2 seconds , the number of seconds here determine the time after that graph refreshes
There are two important points to note here
Instead of matplotlib.pyplot.show()
you can just use matplotlib.pyplot.show(block=False)
. This call will not block the program to execute further.
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.
example of dynamic plot , the secret is to do a pause while plotting , here i use networkx:
G.add_node(i,)
G.add_edge(vertic[0],vertic[1],weight=0.2)
print "ok"
#pos=nx.random_layout(G)
#pos = nx.spring_layout(G)
#pos = nx.circular_layout(G)
pos = nx.fruchterman_reingold_layout(G)
nx.draw_networkx_nodes(G,pos,node_size=40)
nx.draw_networkx_edges(G,pos,width=1.0)
plt.axis('off') # supprimer les axes
plt.pause(0.0001)
plt.show() # display
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()