How can I get matplotlib's imshow to refresh once every second? - python

Have a sensor that gives me an 8x8 grid of temps that I am to display on a live heatmap. I have made an 8x8 rand to simulate that data. This heatmap should be able to run until I tell it not to run anymore. I'm using python3 and matplotlib to attempt this visualization.
I've tried may ways to make this work, including clearing the screen, turning the plt into a figure, telling show() to not block, etc. You can see many of my attempts in the comments. It either displays once only, or never displays at all (e.g. ion() and plt.show(block=False) never display any data). I've hit my head against a wall for 2 whole work days and I can't figure out why it won't display properly.
import time
import socket
import matplotlib.pyplot as plt
import numpy as np
import random
first = True
randArray = []
#plt.ion()
plt.show(block=False)
#fig = plt.figure()
#str1 = ''.join(str(e) for e in amg.pixels)
#print (type(str1))
while True:
#create a bunch of random numbers
randArray = np.random.randint(0,50, size=(8,8))
#print the array, just so I know you're not broken
for x in randArray:
print(x)
#This is my attempt to clear.
if (first == False):
plt.clf()
first = False
#basical visualization
plt.imshow(randArray, cmap='hot', interpolation='nearest')
plt.draw()
plt.show()
#fig.canvas.draw()
#plt.canvas.draw()
#plt.display.update
print("Pausing...")
time.sleep(5)
I expect the code to generate a new set of numbers every 5 seconds, and then refresh the screen with the colors of those new numbers. This should be able to run for hours if I don't interrupt, but the screen never refreshes.
More: I have tried everything listed in the post "How to update a plot in matplotlib?" and everything they do just makes it so that no graph ever populates. The launcher acts like it's going to do something by showing up in the task bar, but then does nothing. I've tried it on a Mac and a Pi, both have the same issue. Maybe it's because that post is 8 years old, and this is python 3 not python 2? Maybe it's because I use imshow() instead of plot()? I haven't figured out how to make their code work on my machine either.
Edit: I've gotten it to work on the raspberry pi thanks to the first commenters recommendations. But now I'm left wondering.... what's wrong with my Mac??

This is a similar question to this one.
You could try to modify your code to something like this:
import time
import socket
import matplotlib.pyplot as plt
import numpy as np
import random
first = True
randArray = []
#plt.ion()
plt.show(block=False)
#fig = plt.figure()
#str1 = ''.join(str(e) for e in amg.pixels)
#print (type(str1))
fig = plt.figure()
for i in range(0,5):
#create a bunch of random numbers
randArray = np.random.randint(0,50, size=(8,8))
#print the array, just so I know you're not broken
for x in randArray:
print(x)
#This is my attempt to clear.
if (first == False):
plt.clf()
first = False
#basical visualization
ax = fig.add_subplot(111)
ax.imshow(randArray, cmap='hot', interpolation='nearest')
fig.canvas.draw()
fig.canvas.flush_events()
#fig.canvas.draw()
#plt.canvas.draw()
#plt.display.update
print("Pausing...")
time.sleep(2)
Have a nice day and get some rest :).

Try this one:
import matplotlib.pyplot as plt
import numpy as np
while True:
#create a bunch of random numbers
random_array = np.random.randint(0,50, size=(8,8))
#print the array, just so I know you're not broken
print(random_array)
#clear the image because we didn't close it
plt.clf()
#show the image
# plt.figure(figsize=(5, 5))
plt.imshow(random_array, cmap='hot', interpolation='nearest')
plt.colorbar()
print("Pausing...")
plt.pause(5)
#uncomment this line and comment the line with plt.clf()
# plt.close()
The magic is with the line plt.pause(5), it shows the image for five seconds. It's up to you if you want to close it (plt.close()) or clear it (plt.clf()). When you want to update constantly your plot, you don't use plt.show() or plt.draw(), you use plt.pause().
Uncomment some lines to try some variations... of course, some of them won't work.

Related

FuncAnimation doesn't display animation

I have code that is running on a different machine.
%matplotlib widget
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
n = 100
x = np.random.randn(n)
def update(curr):
if curr == n:
a.event_source.stop()
plt.cla()
bins = np.arange(-4, 4, 0.5)
plt.hist(x[:curr], bins=bins)
plt.axis([-4,4,0,30])
plt.gca().set_title('Sampling the Normal Distribution')
plt.gca().set_ylabel('Frequency')
plt.gca().set_xlabel('Value')
plt.annotate('n = {}'.format(curr), [3,27])
a = animation.FuncAnimation(plt.figure(), update, interval=100)
plt.show()
However, it gives me this every time
And some times: "UserWarning: Animation was deleted without rendering anything. This is most likely not intended. To prevent deletion, assign the Animation to a variable, e.g. anim, that exists until you output the Animation using plt.show() or anim.save()."
I installed ipympl, restarted kernel, IDE, computer, removed "%matplotlib widget", but all this didn't help.
I hope you'll give me a hand
UPDATE:
I checked several examples of working code and found out 2 things:
All code including this one generates the correct animation, if you save it in any format (mp4, html), you can see it
If the animation is saved, then plt.show() will show the last frame, if not, then as in the picture above
I was having the same problem, and the only workaround I've found is to save the animation and then read it back from the file:
from IPython.display import Image
f = r'test_animation.gif'
writergif = animation.PillowWriter(fps=10)
a.save(f,writer=writergif)
plt.close()
Image(open('test_animation.gif','rb').read())

Updating plot in real time

I am giving data to a matrix (e.g. with shape 100x100) by the following code:
from random import randint
import matplotlib.pyplot as plt
import numpy as np
import random as rand
tab = np.eye(100, 100)
x = np.arange(0, 100, 1)
plt.ion()
fig = plt.figure()
ax = fig.add_subplot(111)
for i in range(100):
for j in range(100):
tab[i, j] = rand.randint(0, 254)
line1, = ax.plot(x, tab[i, :], 'r-')
line1.set_ydata(tab[i, j])
fig.canvas.draw()
fig.canvas.flush_events()
ax.lines.remove(line1)
I need to update matrix using loops and upgrade plot in the same time.
When loop with j ends, i-loop want to clear plot and start plotting again. Is it possible?
My result:
What I need:
After reading your comment i think i understood what you where trying to do
the reason you got those horizontal lines was that you're setting ydata again after plotting(to a constant so its like plotting a horizontal line)
consider the code below:
from random import randint
import matplotlib.pyplot as plt
import numpy as np
import random as rand
tab = np.eye(100, 100)
x = np.arange(0, 100, 1)
plt.ion()
fig = plt.figure()
ax = fig.add_subplot(111)
for i in range(100):
for j in range(100):
tab[i, j] = ((50-i/2)*(50-i/2)-(50-j)*(50-j))/100
for i in range(100):
line1, = ax.plot(x, tab[i, :], 'r-')
fig.canvas.draw()
fig.canvas.flush_events()
ax.lines.remove(line1)
I used another for to instantiate the tab map (since you're using sensor data I guess that is exactly what you're doing in your code because you need to read all of the data (at least the ones for the current cross section) to be able to plot the type of graph you want. this is equivalent to reading all of the data at the beginning and then starting to plot it)
(I also used simulated values instead of random values for the sake of testing)
if you want to draw the data AS THEY COME FROM THE SENSOR then you must define a function to get the data of the current cross section from the sensor and return an array. Idk the library you're using for the sensor but I'm assuming the scan functions are synchronous so the function will return exactly after the input is over making the whole thing pseudo-real time
from random import randint
import matplotlib.pyplot as plt
import numpy as np
import random as rand
x = np.arange(0, 100, 1)
plt.ion()
fig = plt.figure()
ax = fig.add_subplot(111)
for i in range(100):
data = READ_CURRENT_CROSS_SECTION()
line1, = ax.plot(x, data, 'r-')
fig.canvas.draw()
fig.canvas.flush_events()
ax.lines.remove(line1)
again, if plotting the data as the come from the sensor is your goal here it is going to depend a lot on the library you're using but except for all of that the problem with your code was that it was trying to plot while it was getting the data point by point which gives you insufficient data for plotting a cross section(hence the straight lines) (PS: there actually are some ways to pull it off like this but will be extremely slow!)
So either
write a function to scan the whole 2d area and return the whole map before you start plotting(which will be like my first code and the function i just said will replace lines 11-13). this takes away the real time feature but it will give you a beautiful animated plot in a short time
write a function to scan each cross section and return it as a 100 element array. which makes it kind of real time but i guess is harder to implement. This is like my second code but you have to define READ_CURRENT_CROSS_SECTION yourself

Create single animation from programme

This programme fills a figure with square patches. The y axis limit is set so that it will be seen that there is only one patch in one position. It plots this filling process. I want to record the filling as an animation and am trying to do so with 'matplotlib.animation'. I turn the plotting part of the programme into a function (def filler(b):) so that I can pass this function to the animation lines at the bottom. When I run the programme I get an error right at the end of the plotting saying Python has stopped working. Please could somebody explain why. Thanks.
Note that I don't know what the b in the function argument is meant to represent. I include it because without it the programme doesn't run, asking for a positional argument.
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.animation as animation
import numpy as np
startx = 0
endx = 10
blocks = 100
points = np.random.randint(startx,endx,size=blocks)
y = [-1]*int(endx-startx)
fig = plt.figure(figsize=(5,6))
ax = fig.add_subplot(111,aspect='equal')
ax.set_xlim(startx,endx)
ax.set_ylim(0,5)
def filler(b):
for i in range(blocks):
z = 5
a = patches.Rectangle((points[i],z),1,1,ec='k',fc=(1-i/blocks,i/(2*blocks),i/blocks))
ax.add_patch(a)
while z>y[int(points[i])]+1:
z=z-1
plt.pause(0.001)
a.set_y(z)
y[int(points[i])]=z
filler_ani = animation.FuncAnimation(fig, filler,interval=50, repeat=False, blit=True)
filler_ani.save('filler.mp4')
The code in the question mixes two different types of animations. Using a loop and plt.draw(), and a FuncAnimation. This will lead to chaos, as essentially the complete animation on screen is done during the first frame of the FuncAnimation, at the end of that first frame the animation fails.
So, one has to decide. Since it seems you want to do a FuncAnimation here, in order to be able to save it, one needs to get rid of the plt.draw. Then the problem is that there is a for loop and a while loop. This makes it hard to use a framenumber based animation.
Instead one may use a generator based animation.
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.animation as animation
import numpy as np
startx = 0
endx = 10
blocks = 101
points = np.random.randint(startx,endx,size=blocks)
y = [-1]*int(endx-startx)
fig = plt.figure(figsize=(5,6))
ax = fig.add_subplot(111,aspect='equal')
ax.set_xlim(startx,endx)
ax.set_ylim(0,5)
def filler():
yield None
for i in range(blocks):
z = 5
a = patches.Rectangle((points[i],z),1,1,ec='k',fc="r")
ax.add_patch(a)
while z>y[int(points[i])]+1:
z=z-1
a.set_y(z)
yield a
y[int(points[i])]=z
filler_ani = animation.FuncAnimation(fig, lambda x: None, frames=filler,
interval=50, blit=False, repeat=False)
plt.show()
This is kind of hacky, but stays most closely to your initial code.

Plotting to browser continuously using serve_figure

I want to see plots in progress continuously driven by the plot program using browser whenever it is connected. I searched and found serve_figure.py examples that are similar to what I need. But I cant get the following test code to work. Serve_figure.py holds up the for-loop after the first plot. At the browser only the first plot is shown. I don't need the mouse event in serve_figure.py. If there is another way to do this will be most welcome.
#!/usr/bin/env pythonnter
import serve_figure
import time
import numpy as np
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
def animate():
x = np.arange(0, 2*np.pi, 0.01)
line, = ax.plot(x, np.sin(x))
for i in np.arange(1,200):
line.set_ydata(np.sin(x+i/10.0))
fig.canvas.draw()
time.sleep(1)
serve_figure.serve_figure(fig, port=8888)
win = fig.canvas.manager.window
fig.canvas.manager.window.after(200, animate)
plt.show()
BTW, the link to serve_figure.py is
https://github.com/mdboom/mpl_browser_experiments/blob/master/serve_figure.py

Dynamically updating a graphed line in python [duplicate]

This question already has answers here:
pylab.ion() in python 2, matplotlib 1.1.1 and updating of the plot while the program runs
(2 answers)
Closed 9 years ago.
I'm plotting a line using matplotlib and would like to update my line data as soon as new values are generated. However, once in the loop, no window appears. Even though the printed line indicates the loop is running.
Here's my code:
def inteprolate(u,X):
...
return XX
# generate initial data
XX = inteprolate(u,X)
#initial plot
xdata = XX[:,0]
ydata = XX[:,1]
ax=plt.axes()
line, = plt.plot(xdata,ydata)
# If this is in, The plot works the first time, and then pauses
# until the window is closed.
# plt.show()
# get new values and re-plot
while True:
print "!"
XX = inteprolate(u,XX)
line.set_xdata(XX[:,0])
line.set_ydata(XX[:,1])
plt.draw() # no window
How do I update my plot in real-time when the plt.show() is blocking and plt.draw doesn't update/display the window?
You need to call plt.pause in your loop to give the gui a chance to process all of the events you have given it to process. If you do not it can get backed up and never show you your graph.
# get new values and re-plot
plt.ion() # make show non-blocking
plt.show() # show the figure
while True:
print "!"
XX = inteprolate(u,XX)
line.set_xdata(XX[:,0])
line.set_ydata(XX[:,1])
plt.draw() # re-draw the figure
plt.pause(.1) # give the gui time to process the draw events
If you want to do animations, you really should learn how to use the animation module. See this awesome tutorial to get started.
You'll need plt.ion(). Take a look a this: pylab.ion() in python 2, matplotlib 1.1.1 and updating of the plot while the program runs. Also you can explore the Matplotlib animation classes : http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/
An efficient way to do the same as #Alejandro is:
import matplotlib.pyplot as plt
import numpy as np
plt.ion()
x = np.linspace(0,2*np.pi,num=100)
y = np.sin(x)
plt.xlim(0,2*np.pi)
plt.ylim(-1,1)
plot = plt.plot(x[0], y[0])[0]
for i in xrange(x.size):
plot.set_data(x[0:i],y[0:i])
plt.draw()
I think this toy code clarify the answer of #ardoi:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,2*np.pi,num=100)
plt.ion()
for i in xrange(x.size):
plt.plot(x[:i], np.sin(x[:i]))
plt.xlim(0,2*np.pi)
plt.ylim(-1,1)
plt.draw()
plt.clf()
Edit:
The previous code display a sinusoidal function by animating it in the screen.

Categories