Display numpy array in a for loop using matplotlib imshow - python

I have a numpy array whose elements are updated in a for loop:
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
for t in range(0,10):
imshow(a)
for i in range(0,a.shape[0]):
for j in range(0,a.shape[1]):
a[i][j] += 1
I want to display the array at each iteration, but imshow() doesn't work, it just displays the image once the loop terminates.
ps. I'm using an Ipython notebook
I found different things on the web but none of them work on my computer (for example I tried to use matplotlib's animation module)
The strange thing is that if I try to execute this example (http://matplotlib.org/examples/animation/dynamic_image2.html) using the standard python prompt everything works fine, while on the Ipython notebook it doesn't work. Can anyone explain me why?
notes:
Maybe I oversimplified my code;
I'm working on a forest-fire model, the array is a grid filled with 0 = empty site, 1 = tree, 2 = fire.
At each time step (iteration):
a tree is dropped on a randomly chosen site and if the site is free the tree is planted
a tree ignites with a probability f
I want to display the array using a colormap to visualize the evolution of my model

imshow(a) will plot the values of the array a as pixel values, but it won't display the plot. To view the image after each iteration of the for loop, you need to add show().
This should do what you want:
from matplotlib.pyplot import imshow, show
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
for t in range(0,10):
imshow(a)
show()
for i in range(0,a.shape[0]):
for j in range(0,a.shape[1]):
a[i][j] += 1

For me just using show() doesn't always work, and when it does, things tend to get very slow over time. To handle these problems, I import display and use its display() and clear_output() methods.
import numpy as np
import matplotlib.pyplot as plt
from IPython import display
import time
pause_time = 0.2 # seconds between frames
a = np.random.rand(3,3)
for t in range(0,10):
plt.imshow(a)
plt.title(t)
display.display(plt.gcf())
display.clear_output(wait=True)
time.sleep(pause_time)
a = np.random.rand(3,3)
The above is adapted from this answer. The time module is used just to let you pause the display and control the frame rate to the level you want: it is optional.

Related

Matplotlib Imshow Doesn't Update With Draw

I'm having trouble getting imshow to update with new data. For reference I'm pulling data off a serial port and trying to plot it, updating every second or so. I had been accumulating the data with a thread, so I initially thought that might be the problem as matplotlib isn't thread safe. However, I can't get the following simpler example to work:
import numpy as np
import matplotlib.pyplot as plt
import time
dat = np.random.rand(100,10)
plt.ion()
fig = plt.figure()
ax = fig.add_subplot(111)
image = ax.imshow(np.zeros((10,10)))
fig.canvas.draw()
count = 0
while count < 100:
image.set_data(dat[count:count+10])
fig.canvas.draw()
count += 10
time.sleep(1)
Using TkAgg, I just get the plot of all zeros, it never updates then quits.
With Qt5Agg, an empty window pops up before quitting.
I've tried various combinations of draw_idle(), flush_events() and plt.show(block=False), with the same results.
python 3.8.10 , matplotlib 3.2.2
Immediately after posting this I figured out the solution.
I initialized the plot with all zeros - changing this to be a random array of values fixes it fixes it.
I'm not sure why starting with all zeros broke the color scaling, though from the matplotlib documentation the default normalization scales the input data on [0,1], so I suspect that was the issue.

plt.pause leading to improper subplot placement

I am following this tutorial, and I stumbled upon something I don't understand.
The idea is to have a function, that plots an image. This function is then called in a loop where subplots are defined:
minimal example
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
def show_image(image):
"""Show image"""
plt.imshow(image)
def show_image_wait(image):
"""show image, and wait a little bit. similar implementation than in the tutorial"""
plt.imshow(image)
plt.pause(0.001)
Now, calling both functions in a loop:
no waiting:
for i in range(4):
image = np.random.randint(0,3, (10,10))
plt.subplot(1, 4, i+1)
show_image(image)
# expected output: 1 row, with 4 images side by side
# actual output: 1 row, with 4 images, side by side
with waiting, however:
for i in range(4):
image = np.random.randint(0,3, (10,10))
plt.subplot(1, 4, i+1)
show_image_wait(image)
# expected output: 1 row, with 4 images side by side
# actual output: 4 rows, with 1 images each
A function similar to show_image_wait is used in the tutorial linked above, where all images appear correctly positioned.
I don't understand why waiting for a tiny bit overrides the subplot positioning in my case, and not in the linked example.
All of this happens in a jupyter notebook
Help is greatly appreciated!
The reason you see several rows is that for each loop run a new figure is produced. Independent figures are placed below each other in a jupyter output cell.
This in turn is caused by the one from the last loop iteration being drawn on screen and hence when plt.subplot is called another time, no active figure is present - therefore a new one is created.
The underlying cause of all of this is that plt.pause(..) does a bit more than only pausing. Instead it handles possible events on the figure and eventually draws and shows the figure in interactive mode.
The source of plt.pause is
manager = _pylab_helpers.Gcf.get_active()
if manager is not None:
canvas = manager.canvas
if canvas.figure.stale:
canvas.draw_idle()
show(block=False) # <---- here the figure is shown.
canvas.start_event_loop(interval)
else:
time.sleep(interval)
where I marked the crucial line with a comment.
So in total, if you want a true pause as in "Do not do anything for x seconds", plt.pause is not well suited. In general it is also a bit questionable how useful it is in jupyter notebooks with inline backend, because that backend does not provide any interactivity.
I think it would work if you use plt.show() after the for loop but in JuPyTer notebook, the plotting is inline. A work around solution could be to use time.sleep(0.001). You may try and see if it serves your purpose.
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import time
def show_image_wait(image):
"""show image, and wait a little bit. similar implementation than in the tutorial"""
plt.imshow(image)
time.sleep(0.001)
for i in range(4):
image = np.random.randint(0,3, (10,10))
plt.subplot(1, 4, i+1)
show_image_wait(image)

Real-time plot in matplotlib - python

I'm trying to get real-time spectrum analyzer type plot in matplotlib. I've got some code working (with help from other posts on StackOverflow) as follows:
import time
import numpy as np
import matplotlib.pyplot as plt
plt.axis([0, 1000, 0, 1])
plt.ion()
plt.show()
i=0
np.zeros([1,500],'float')
lines=plt.plot(y[0])
while 1:
i=i+1
lines.pop(0).remove()
y = np.random.rand(1,100)
lines=plt.plot(y[0])
plt.draw()
The code works and I'm getting what I want, but there is a serious problem. The plot window would freeze after some time. I know the program is still running by inspecting the i variable (I'm running the code in Anaconda/Spyder so I can see the variables). However the plot window would show "Non responding" and if I terminate the python program in Spyder by ctrl+c, the plot window comes back to life and show the latest plot.
I'm out of wits here as how to further debug the issue. Anyone to help?
Thanks
I am not sure that adding plt.pause will entirely solve your issue. It may just take longer before the application crash. The memory used by your application seems to constantly increase over time (even after adding plt.pause). Below are two suggestions that may help you with your current issue:
Instead of removing/recreating the lines artists with each iteration with remove and plot, I would use the same artist throughout the whole animation and simply update its ydata.
I'll use explicit handlers for the axe and figure and call show and draw explicitly on the figure manager and canvas instead of going with implicit calls through pyplot, following the advices given in a post by tcaswell.
Following the above, the code would look something like this:
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.axis([0, 100, 0, 1])
y = np.random.rand(100)
lines = ax.plot(y)
fig.canvas.manager.show()
i=0
while 1:
i=i+1
y = np.random.rand(100)
lines[0].set_ydata(y)
fig.canvas.draw()
fig.canvas.flush_events()
I've run the above code for a good 10 minutes and the memory used by the application remained stable the whole time, while the memory used by your current code (without plt.pause) increased by about 30MiB over the same period.
To answer myself, I solved the issue by adding
plt.pause(0.01)
after the
plt.draw()
This probably allows the GUI to finish the drawing and clear the buffer somewhere (my guess) before the new data comes in.
I know I'm late to answer this question, but for your issue you could look into the "joystick" package. It is based on the line.set_data() and canvas.draw() methods, with optional axes re-scaling, hence most probably faster than removing a line and adding a new one. It also allows for interactive text logging or image plotting (in addition to graph plotting).
No need to do your own loops in a separate thread, the package takes care of it, just give the update frequency you wish. Plus the terminal remains available for more monitoring commands while live plotting, which is not possible with a "while True" loop.
See http://www.github.com/ceyzeriat/joystick/ or https://pypi.python.org/pypi/joystick (use pip install joystick to install)
try:
import joystick as jk
import numpy as np
import time
class test(jk.Joystick):
# initialize the infinite loop decorator
_infinite_loop = jk.deco_infinite_loop()
def _init(self, *args, **kwargs):
"""
Function called at initialization, see the doc
"""
self._t0 = time.time() # initialize time
self.xdata = np.array([self._t0]) # time x-axis
self.ydata = np.array([0.0]) # fake data y-axis
# create a graph frame
self.mygraph = self.add_frame(jk.Graph(name="test", size=(500, 500), pos=(50, 50), fmt="go-", xnpts=100, xnptsmax=1000, xylim=(None, None, 0, 1)))
#_infinite_loop(wait_time=0.2)
def _generate_data(self): # function looped every 0.2 second to read or produce data
"""
Loop starting with the simulation start, getting data and
pushing it to the graph every 0.2 seconds
"""
# concatenate data on the time x-axis
self.xdata = jk.core.add_datapoint(self.xdata, time.time(), xnptsmax=self.mygraph.xnptsmax)
# concatenate data on the fake data y-axis
self.ydata = jk.core.add_datapoint(self.ydata, np.random.random(), xnptsmax=self.mygraph.xnptsmax)
self.mygraph.set_xydata(t, self.ydata)
t = test()
t.start()
t.stop()

Matplotlib Quiver memory leak

I have created quite a nice visualization program using Qt as the interface layer and matplotlib to draw much of the content. I use contour and countourf as well as text and lines and never run into problems with releasing objects.
As soon as I start calling Axes.quiver I get leaks. With the size of the dataset it adds up quickly. The following code demonstrates the problem:
from pylab import *
from numpy import ma
import time
X,Y = meshgrid( arange(0,2*pi,.04),arange(0,2*pi,.04) )
U = cos(X)
V = sin(Y)
fig = figure()
ax = fig.add_axes([0.1,0.1,0.8,0.8])
for i in range(90):
Q = ax.quiver( U, V)
time.sleep(0.2)
Q.remove()
I know this simple snippet isnt drawing to screen (leak becomes worse then).
Q has a sys.getrefcount of 4 so nothing I am able to do seems to get rid of it. Calls to fig and ax clear only reduce the refcount to 2. Quiver draws the image i want, but I am completely out of ideas.
My code is much more complex than this. I have tried completely replacing the axes objects but that doesnt help. I really am not free to replace the Figure instance.

pylab.ion() in python 2, matplotlib 1.1.1 and updating of the plot while the program runs

what I am trying to do is having a script compute something, prepare a plot and show the already obtained results as a pylab.figure - in python 2 (specifically python 2.7) with a stable matplotlib (which is 1.1.1).
In python 3 (python 3.2.3 with a matplotlib git build ... version 1.2.x), this works fine. As a simple example (simulating a lengthy computation by time.sleep()) consider
import pylab
import time
import random
dat=[0,1]
pylab.plot(dat)
pylab.ion()
pylab.draw()
for i in range (18):
dat.append(random.uniform(0,1))
pylab.plot(dat)
pylab.draw()
time.sleep(1)
In python 2 (version 2.7.3 vith matplotlib 1.1.1), the code runs cleanly without errors but does not show the figure. A little trial and error with the python2 interpreter seemed to suggest to replace pylab.draw() with pylab.show(); doing this once is apparently sufficient (not, as with draw calling it after every change/addition to the plot). Hence:
import pylab
import time
import random
dat=[0,1]
pylab.plot(dat)
pylab.ion()
pylab.show()
for i in range (18):
dat.append(random.uniform(0,1))
pylab.plot(dat)
#pylab.draw()
time.sleep(1)
However, this doesn't work either. Again, it runs cleanly but does not show the figure. It seems to do so only when waiting for user input. It is not clear to me why this is, but the plot is finally shown when a raw_input() is added to the loop
import pylab
import time
import random
dat=[0,1]
pylab.plot(dat)
pylab.ion()
pylab.show()
for i in range (18):
dat.append(random.uniform(0,1))
pylab.plot(dat)
#pylab.draw()
time.sleep(1)
raw_input()
With this, the script will of course wait for user input while showing the plot and will not continue computing the data before the user hits enter. This was, of course, not the intention.
This may be caused by different versions of matplotlib (1.1.1 and 1.2.x) or by different python versions (2.7.3 and 3.2.3).
Is there any way to accomplish with python 2 with a stable (1.1.1) matplotlib what the above script (the first one) does in python 3, matplotlib 1.2.x:
- computing data (which takes a while, in the above example simulated by time.sleep()) in a loop or iterated function and
- (while still computing) showing what has already been computed in previous iterations
- and not bothering the user to continually hit enter for the computation to continue
Thanks; I'd appreciate any help...
You want the pause function to give the gui framework a chance to re-draw the screen:
import pylab
import time
import random
import matplotlib.pyplot as plt
dat=[0,1]
fig = plt.figure()
ax = fig.add_subplot(111)
Ln, = ax.plot(dat)
ax.set_xlim([0,20])
plt.ion()
plt.show()
for i in range (18):
dat.append(random.uniform(0,1))
Ln.set_ydata(dat)
Ln.set_xdata(range(len(dat)))
plt.pause(1)
print 'done with loop'
You don't need to create a new Line2D object every pass through, you can just update the data in the existing one.
Documentation:
pause(interval)
Pause for *interval* seconds.
If there is an active figure it will be updated and displayed,
and the gui event loop will run during the pause.
If there is no active figure, or if a non-interactive backend
is in use, this executes time.sleep(interval).
This can be used for crude animation. For more complex
animation, see :mod:`matplotlib.animation`.
This function is experimental; its behavior may be changed
or extended in a future release.
A really over-kill method to is to use the matplotlib.animate module. On the flip side, it gives you a nice way to save the data if you want (ripped from my answer to Python- 1 second plots continous presentation).
example, api, tutorial
Some backends (in my experience "Qt4Agg") require the pause function, as #tcaswell suggested.
Other backends (in my experience "TkAgg") seem to just update on draw() without requiring a pause. So another solution is to switch your backend, for example with matplotlib.use('TkAgg').

Categories

Resources