Matplotlib live plot of data update - python

I want to liveplot some data acquired by instruments.
The following works (random data as an example):
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import random
from itertools import count
import time
%matplotlib qt
fields, volts = [], []
index = count()
def animate(i):
global fields, volts
fields.append(next(index))
volts.append(random.randint(0, 5))
plt.cla()
plt.plot(fields, volts)
fig = plt.figure(figsize=(8,4))
ax = fig.add_subplot(111)
ani = FuncAnimation(fig, animate, 1000)
plt.show()
... but I need to have the data update outside of FuncAnimation, in it own loop.
I believed it could work, but the following code just opens the window and doesnt update it until I stop the program:
fields, volts = [], []
index = count()
def animate(i):
global fields, volts
plt.cla()
plt.plot(fields, volts)
fig = plt.figure(figsize=(8,4))
ax = fig.add_subplot(111)
ani = FuncAnimation(fig, animate, 1000)
plt.show()
while (True):
time.sleep(1)
fields.append(next(index))
volts.append(random.randint(0, 5))

Try this, FuncAnimation calls your animate periodically so you could shift your append statements into that loop and also control the interval using the interval keyword argument.
fields, volts = [], []
index =iter(range(20))
from numpy import random
def animate(i):
global fields, volts
fields.append(len(volts))
volts.append(random.randint(0,5))
plt.cla()
plt.plot(fields, volts, c='c')
fig = plt.figure(figsize=(8,4))
ax = fig.add_subplot(111)
ani = FuncAnimation(fig, animate, frames=20, interval=1000)
plt.show()

Related

Show only one frame at a time using matplotlib.animation.FuncAnimation

I've got some code similar to this
from matplotlib import animation
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
def animate(i):
x0,y0 = np.random.random(size=(2,))*4-2
x = np.random.normal(loc=x0, size=(1000,))
y = np.random.normal(loc=y0, size=(1000,))
for layer in prevlayers:
layer.remove()
prevlayers[:] = []
hexlayer = ax.hexbin(x,y, gridsize=10, alpha=0.5)
# the following line is needed in my code
hexlayer.remove()
prevlayers.append(hexlayer)
return hexlayer,
prevlayers = []
ani = animation.FuncAnimation(fig, animate, frames=12)
ani.save('teste.gif', writer='PillowWriter')
I'm trying to show only one frame at a time, but the code that I have written uses two ax.hexbin() calls and I have to remove one of them in order to show the correct graph. Is there a way to show one hexbin layer at a time using FuncAnimation?
You only need ax.clear() for each frame
from matplotlib import animation
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
def animate(i):
ax.clear()
x0,y0 = np.random.random(size=(2,))*4-2
x = np.random.normal(loc=x0, size=(1000,))
y = np.random.normal(loc=y0, size=(1000,))
hexlayer = ax.hexbin(x,y, gridsize=10, alpha=0.5)
return ax
ani = animation.FuncAnimation(fig, animate, frames=12)
ani.save('teste.gif', writer='PillowWriter')
this code produces

Live graph in python with a fixed window

I am trying to construct a live graph in python to plot random numbers in a graph with a fixed plot window
The width of the plot window will be 20 samples. For the 21st sample, the 1st sample will dissappear from the right side. Here is my code. Cant figure out why is it not plotting.
import random
import time
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
#creating a subplot
ax1 = fig.add_subplot(1,1,1)
xs = []
ys = []
iter = 0
def animate(i,xs,ys,iter):
while True:
iter = iter+1
xs.append(iter)
ys.append(round(random.uniform(-120,20),2))
#I want only 20 data points on the plot i.e the plot window will be only showing 20 samples at a time
x = xs[-20:]
y = ys[-20:]
ax1.clear()
ax1.plot(x, y)
ax1.set_ylim([-120,20])
plt.xlabel('Value')
plt.ylabel('Time')
plt.title('Live Graph')
time.sleep(1)
ani = animation.FuncAnimation(fig, animate, fargs = (xs,ys,iter), interval=1000)
plt.show()
Check out the documentation for FuncAnimation.
The second parameter is a function that is repeatedly called for each consecutive frame, updating the figure with each call. Your function animate is an infinite loop, so the execution thread never returns back to FuncAnimation. Try something like this instead as a starting point:
import random
import time
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
xs = []
ys = []
line, = ax1.plot(xs, ys)
plt.xlabel('Value')
plt.ylabel('Time')
plt.title('Live Graph')
def animate(frame, xs, ys):
xs.append(frame)
ys.append(round(random.uniform(-120,20),2))
x = xs[-20:]
y = ys[-20:]
line.set_xdata(x)
line.set_ydata(y)
ax1.set_xlim(min(x)-1, max(x)+1)
ax1.set_ylim(min(y)-1, max(y)+1)
ax1.set_xticks(list(range(min(x), max(x)+1)))
return line
ani = animation.FuncAnimation(fig, animate, fargs = (xs,ys), interval=100)
plt.show()

How do i make y-axis to be in an ascending order in matplotlib.animation? Sentdex tutorial

import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure(1)
ax1 = fig.add_subplot(1,1,1)
def animate(i):
graph_data = open('example.txt','r').read()
lines = graph_data.split('\n')
xs = []
ys = []
for line in lines:
if len(line) > 1:
x, y = line.split(',')
xs.append(x)
ys.append(y)
ax1.clear()
ax1.plot(xs, ys)
ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.show()
Is there another way to write the code such that y-axis is in ascending order?
If so. where should I find it? This is from sentdex matplotlib tutorial
The better solution is to not do all the stuff manually and rely on numpy reading the data. Also, not clearing the axes in each loopstep may be beneficial.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig, ax = plt.subplots()
line, = ax.plot([],[])
def animate(i):
x,y = np.loadtxt("data/example.txt", unpack=True, delimiter=",")
line.set_data(x,y)
ax.relim()
ax.autoscale_view()
ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.show()

Python: Graphing and animating multiple iterations of the same graph with Python

Hi I am trying to create a movie of 15 Gaussian graphs that move to the left (thats essentially what the code is suppose to do)
However, my idea for how to create the for loop to create the 15 graphs has not created more than 1, it only speeds up the animation.
A similar code worked on matlab. It created 15 different Gaussian curves.
Here is a sample of my code.
any help would be appreciated.
Thanks
import numpy as np
import matplotlib.pyplot as plt
plt.switch_backend('agg')
import matplotlib.animation as animation
Gamma=0.0005
q=1.6e-19
m=0.067*9e-31
B=10
Ec=(1.0567e-34)*B/m
#e=2.78
#E0=0+(1.0567e-34)*x*i/m
fig, ax = plt.subplots()
pass
x = np.arange(0, 3.4e-3, 1.7e-5) # x-array, third number is interval here, x is energy
line, = ax.plot(x, np.e**(-(x-((1.0567e-34)*1*1/m))**2/Gamma**2))
def animate(i):
for p in xrange(1,3):
line.set_ydata(np.e**((-(x-((1.0567e-34)*p*i/m))**2)/Gamma**2)) # update the data
return line,
#Init only required for blitting to give a clean slate.
def init():
line.set_ydata(np.ma.array(x, mask=True))
return line,
ani = animation.FuncAnimation(fig, animate, np.arange(0, 2, .01), init_func=init,
interval=10, blit=True)
Writer = animation.writers['ffmpeg']
writer = Writer(fps=20, metadata=dict(artist='Me'), bitrate=1800)
ani.save('QHanimati.mp4', writer=writer)
plt.show()
You currently have exactly one line in your code. This line gets updated. If you want to have more lines, you need to create more lines.
You then also need to update all of those lines.
(Since the role of p isn't clear from the example I took it as some incrementing number here. I also restricted this to 8 curves not to overcrowd the image.)
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
Gamma=0.0005
q=1.6e-19
m=0.067*9e-31
B=10
Ec=(1.0567e-34)*B/m
fig, ax = plt.subplots()
n = 8 # number of lines
x = np.arange(0, 3.4e-3, 1.7e-5)
lines = [ax.plot(x, np.e**(-(x-((1.0567e-34)*1*1/m))**2/Gamma**2))[0] for _ in range(n)]
def animate(i):
for ln, line in enumerate(lines):
p = (ln+1)/10.
line.set_ydata(np.e**((-(x-((1.0567e-34)*p*i/m))**2)/Gamma**2)) # update the data
return lines
#Init only required for blitting to give a clean slate.
def init():
for line in lines:
line.set_ydata(np.ma.array(x, mask=True))
return lines
ani = animation.FuncAnimation(fig, animate, np.arange(0, 2, .01), init_func=init,
interval=10, blit=True)
plt.show()

How to animate a seaborn's heatmap or correlation matrix?

I am relatively new to python (coming from Matlab). As one project, I am trying to create an animated plot of a correlation matrix over time. To make the plots nice, I am trying seaborn. I struggled to get the animation done at all (having issues with Matplotlib backend on a mac), but a very basic animation now works using this code from the web:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
nx = 50
ny = 50
fig = plt.figure()
data = np.random.rand(nx, ny)
im = plt.imshow(data)
def init():
im.set_data(np.zeros((nx, ny)))
def animate(i):
#xi = i // ny
#yi = i % ny
data = np.random.rand(nx, ny)
im.set_data(data)
return im
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=50, repeat = False)
Now, I was trying to adapt this to seaborn, but did not succeed. It seems that seaborn works on subplots and to animate these was far harder. The best thing I got once was a kind of recursive plot, where seaborn.heatmaps were plotted on top of each other. Also, the im.set_data method was not available.
Any suggestions are highly appreciated.
I replaced plt.imshow (casting data via set_data didn't work) with seaborn.heatmap.
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib import animation
fig = plt.figure()
data = np.random.rand(10, 10)
sns.heatmap(data, vmax=.8, square=True)
def init():
sns.heatmap(np.zeros((10, 10)), vmax=.8, square=True, cbar=False)
def animate(i):
data = np.random.rand(10, 10)
sns.heatmap(data, vmax=.8, square=True, cbar=False)
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=20, repeat = False)
This creates the recursive plot I struggled with.
In addition to your answer above, I wanted to do this from a list of dataframes and save as a gif. So, using your code and Serenity's answer to Matplotlib animation iterating over list of pandas dataframes
fig = plt.figure()
def init():
sns.heatmap(np.zeros((10, 10)), vmax=.8, square=True, cbar=False)
def animate(i):
data = data_list[i]
sns.heatmap(data, vmax=.8, square=True, cbar=False)
data_list = []
for j in range(20):
data = np.random.rand(10, 10)
data_list.append(data)
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=20, repeat = False)
savefile = r"test3.gif"
pillowwriter = animation.PillowWriter(fps=20)
anim.save(savefile, writer=pillowwriter)
plt.show()
Thanks!!!
Here's a complete example (tested with Matplotlib 3.0.3).
import matplotlib.animation as animation
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
def animate_heat_map():
fig = plt.figure()
nx = ny = 20
data = np.random.rand(nx, ny)
ax = sns.heatmap(data, vmin=0, vmax=1)
def init():
plt.clf()
ax = sns.heatmap(data, vmin=0, vmax=1)
def animate(i):
plt.clf()
data = np.random.rand(nx, ny)
ax = sns.heatmap(data, vmin=0, vmax=1)
anim = animation.FuncAnimation(fig, animate, init_func=init, interval=1000)
plt.show()
if __name__ == "__main__":
animate_heat_map()
Based on the answer of r schmaelzle I created animated seaborn heatmap with annotaion.
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib import animation
class Heatmap:
def __init__(self):
self.fig, self.ax = plt.subplots()
self.anim = None
def animate(self):
def init():
sns.heatmap(np.zeros((10, 10)), vmax=.8, ax=self.ax)
def animate(i):
self.ax.texts = []
sns.heatmap(np.random.rand(10, 10), annot=True, vmax=.8, cbar=False, ax=self.ax)
self.anim = animation.FuncAnimation(self.fig, animate, init_func=init, frames=20, repeat=False)
if __name__ == '__main__':
hm = Heatmap()
hm.animate()
The trick to update annotations is to make empty ax.texts = [].
I hope it will help others! :)

Categories

Resources