I have python 2.7.3 installed on my 12.04 64 bit system. I have written the following:
Code:
#!/usr/bin/env python
import numpy as np
from matplotlib import pyplot as plt
import time
import sys
if len(sys.argv) < 2:
sys.stderr.write("\n\n\nUsage: " + sys.argv[0] + " needs to know how many points to display\n\n .. Like this: " + sys.argv[0] + " 50\n\n\n")
sys.exit(1)
howmany = int(sys.argv[1])
top = 100
plt.ion () # set plot to animated
xdata = []
c = 0
for a in range(0,top):
xdata.append(a)
c+=1
ydata = [ 0 ] * top * 10
ax1 = plt.axes ()
c = 0
myfile = open("rdata.txt", "r")
for myline in myfile:
q = myline
ydata[c] = q
c+=1
c = 0
# Make plot
line, = plt.plot (ydata[0:howmany])
myfile = open("rdata.txt", "r")
for p in range(0, top * 10):
#for myline in myfile:
# q = int(myline)
# ydata.append (q)
del ydata [ 0 ]
line.set_xdata (np.arange ( len (ydata)))
line.set_ydata (ydata) # update the data
# time.sleep(0.01)
plt.draw () # update the plot
# c +=1
file.close(myfile)
It reads data from a file I generated using random data.
Ultimately I want to use it with a micropython board ( a kickstarter project which has python on a microcontroller ) to make a simple form of oscilloscope.
The code above displays a dynamic graph which looks just like an oscilloscope display but obviously I want to do more.
As you will see if you run it, the display is constantly updated until it has read all the data then ends.
What I want to do is get it embedded so the wrapper includes entry, button and other widgets so I can customise the graph from a GUI rather than pass command line parameters. Everything I have found after hours of searching works fine IF you just want a static display.
For example I tried to modify the following to embed it:
Code:
#!/usr/bin/env python
import matplotlib, sys
matplotlib.use('TkAgg')
from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
from Tkinter import *
from functools import partial as pto
master = Tk()
master.title("Hello World!")
#-------------------------------------------------------------------------------
f = Figure(figsize=(5,4), dpi=100)
a = f.add_subplot(111)
t = arange(0.0,3.0,0.01)
s = sin(2*pi*t)
a.plot(t, s)
dataPlot = FigureCanvasTkAgg(f, master=master)
dataPlot.show()
dataPlot.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
#-------------------------------------------------------------------------------
master.mainloop()
But I cannot find out how I can get it to set s to a value in the ydata[] array.
I have tried, for example:
Code:
f = Figure(figsize=(5,4), dpi=100)
a = f.add_subplot(111)
t = arange(0.0,3.0,0.01)
for x in range(0,50):
s = ydata[x]
a.plot(t, s)
and many other ideas but I get all sorts of different errors and if, by some chance some of it works, the plot window detaches from the main window and just displays some static plot.
Any ideas please?
I won't be able to answer this soon if you have any questions until tonight though.
Bookmarks