This is the first time I am using matplotlib and numpy.
Here goes the problem:
If I goto python cli, the intended code works fine. Here is that code
>>> from numpy import *
>>> y = array([1,2])
>>> y = append(y, y[len(y) - 1]+1)
>>> y
array([1, 2, 3])
But if I use it with matplotlib in a script I get this error.
line 26, in onkeypress
y = append(y, y[len(y) - 1]+1)
UnboundLocalError: local variable 'y' referenced before assignment
Here is my script:
from matplotlib.pyplot import figure, show
from numpy import *
figzoom = figure()
axzoom = figzoom.add_subplot(111, xlim=(0,10), ylim=(0, 10),autoscale_on=True)
x = array([1, 2 ])
y = array([1, 10 ])
def onkeypress(event):
if event.key == "up":
y = append(y, y[len(y) - 1]+1)
x = append(x, x[len(x) - 1] )
axzoom.plot(x,y)
I tried "append"ing to a different array,say y1, and then y = y1.copy(). But I still get the same error. I must be missing something trivial here???!!!