Matplotlib animation iterating over list of pandas dataframes - python

I have a list of pandas DataFrames with 2 columns each. So far I have a function that, when given an index i, it takes the frame corresponding to index i and plots a graph of data from the first column against the data of the second column.
list = [f0,f1,f2,f3,f4,f5,f6,f7,f8,f9]
def getGraph(i):
frame = list[i]
frame.plot(x = "firstColumn",y = "secondColumn")
return 0
My question now is, how do I make this iterate over the list of frames and animate the graphs displaying each one for 0.3 seconds in succession.
Preferably, I would like to use the FuncAnimation class in the animation library which does the heavy lifting and optimizations for you.

Set animate function and init to axes, figure and line:
from matplotlib import pyplot as plt
from matplotlib import animation
import pandas as pd
f0 = pd.DataFrame({'firstColumn': [1,2,3,4,5], 'secondColumn': [1,2,3,4,5]})
f1 = pd.DataFrame({'firstColumn': [5,4,3,2,1], 'secondColumn': [1,2,3,4,5]})
f2 = pd.DataFrame({'firstColumn': [5,4,3.5,2,1], 'secondColumn': [5,4,3,2,1]})
# make a global variable to store dataframes
global mylist
mylist=[f0,f1,f2]
# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = plt.axes(xlim=(0, 5), ylim=(0, 5))
line, = ax.plot([], [], lw=2)
# initialization function: plot the background of each frame
def init():
line.set_data([], [])
return line,
# animation function of dataframes' list
def animate(i):
line.set_data(mylist[i]['firstColumn'], mylist[i]['secondColumn'])
return line,
# call the animator, animate every 300 ms
# set number of frames to the length of your list of dataframes
anim = animation.FuncAnimation(fig, animate, frames=len(mylist), init_func=init, interval=300, blit=True)
plt.show()
For more info look for the tutorial: https://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/

Related

Animating a scatter plot with a stationary gap in python

I'm animating a scatter plot with a code below. It reads data from a .csv file "sample.csv" and animates it by using np.roll in update function.
import numpy as np
import matplotlib.pyplot as plt
from numpy import genfromtxt
from matplotlib.animation import FuncAnimation
my_data = genfromtxt("sample.csv", delimiter=",", skip_header=1) # reading data
fig, ax = plt.subplots()
xdata, ydata = [], []
line, = ax.plot([], [], ".", markersize=14)
plt.grid()
def init():
ax.set_xlim(0, 50)
ax.set_ylim(0, 60)
return line,
def update(frame):
my_data[:,0] = np.roll(my_data[:,0],1) # moving graph
gap_loc = [19,20,21] # location of a gap
my_data[gap_loc, 1] = np.nan # creating a gap in graph
xdata.append(my_data[:,0])
ydata.append(my_data[:,1])
line.set_data(xdata, ydata)
return line,
ani = FuncAnimation(fig, update, frames=np.arange(0,50,1), init_func=init, blit=True)
plt.show()
The result looks like that:
As you see there is a gap which moves together with the remaining points. However, what I want to achieve is that the gap was stationary at the locations on the horizontal axis: 19,20,21.
How can I achieve this effect?
Below please find a dataset, I'm using for this animation.
Day,Var 1,Var 2
1,2,12
2,4,19
3,6,20
4,8,25
5,10,25
6,12,33
7,14,40
8,16,47
9,18,49
10,20,50
11,22,52
12,24,55
13,26,65
14,28,82
15,30,100
16,32,100
17,34,110
18,36,117
19,38,140
20,40,145
21,42,164
22,44,170
23,46,198
24,48,200
25,50,210
26,48,210
27,46,211
28,44,216
29,42,267
30,40,317
31,38,325
32,36,335
33,34,337
34,32,347
35,30,356
36,28,402
37,26,410
38,24,448
39,22,449
40,20,457
41,18,463
42,16,494
43,14,500
44,12,501
45,10,502
46,8,514
47,6,551
48,4,551
49,2,558
50,0,628
Define the gap when you load the data, and do so in the x column rather than the y:
# imports
my_data = genfromtxt("sample.csv", delimiter=",", skip_header=1) # reading data
gap_loc = [19,20,21] # location of a gap
my_data[gap_loc, 0] = np.nan # creating a gap in graph
# plotting code
So now when you roll the x column, there will always be np.nan at the x values [19, 20, 21], regardless of what the y coordinate is. You can use print(my_data) within the update function to make clear what was going on each iteration.
Here is the result:
Also, I think you are over-plotting because you continually expand xdata and ydata using append. I ended up just removing the xdata and ydata and doing:
def update(frame):
my_data[:,0] = np.roll(my_data[:,0],1) # moving graph
line.set_data(my_data[:,0], my_data[:,1])
return line,

Make scroll-able plot from a dictionary updating in a for loop

I have a dictionary which is updated by a for loop , I am trying to plot the key value pairs in a plot which moves or slides as the number of key value pairs keep updating and the plot just shows the 50 current values from the dictionary.
so far I have made :
for k,v in plot.items():
plt.barh(k,v , color='blue')
plt.pause(0.3)
plt.show()
The problem is that the values keep appending and the plots keeps growing. Is there any way to show just last 50 k,v pairs from a dictionary and and keep on updating the plot. I have also tried a function :
def live_plotter(x_vec,y1_data,line1,identifier='',pause_time=0.1):
if line1==[]:
# this is the call to matplotlib that allows dynamic plotting
plt.ion()
#fig = plt.figure(figsize=(13,6))
#ax = fig.add_subplot(111)
# create a variable for the line so we can later update it
line1, = plt.plot(x_vec,y1_data,'-o', alpha=0.8)
#update plot label/title
plt.ylabel('Cross-correlation')
plt.title('Title: {}'.format(identifier))
line1.set_data(x_vec, y1_data)
plt.pause(pause_time)
return line1
But this also doesn't update the plot and just appends to the plot like the code above.
I have also tried the animation function :
fig = plt.figure()
def animate():
for k, v in plot:
print(k, v)
plt.barh(k, v, color='blue')
plt.pause(0.3)
ani = matplotlib.animation.FuncAnimation(fig, animate, interval=1000)
plt.show()
Reproducible example:
import matplotlib.pyplot as plt
import matplotlib.animation
plot_base={'00002200': 986.11152642551519,
'00002201': 989.90915858383369,
'00002202': 992.87095144990781,
'00002203': 994.89971071876084,
'00002204': 992.92424216660561,
'00002205': 993.19845750930426,
'00002206': 988.88001766153957,
'00002207': 988.95062195981848,
'00002208': 993.17138084871465,
'00002209': 993.9863425202193,
'00002210': 993.43551440410283,
'00002211': 993.04540624076844,
'00002212': 991.40048097057559,
'00002213': 993.16124311517319,
'00002214': 994.06785011666204,
'00002215': 985.24294182260996,
'00002216': 990.5369409623512,
'00002217': 991.83512034302737,
'00002218': 993.43756392913269,
'00002219': 989.77919409784511,
'00002220': 988.09683378239572,
'00002221': 992.19961090836773,
'00002222': 992.69477342507912,
'00002223': 992.37890673842412,
'00002224': 991.55520651752556,
'00002225': 992.15414070360941,
'00002226': 991.49292821478309,
'00002227': 994.75013161999084,
'00002228': 991.54858727670728,
'00002229': 993.22846583401292,
'00002230': 993.88719133150084,
'00002231': 992.89934842358855,
'00002232': 991.10582582918869,
'00002233': 993.24750746833467,
'00002234': 992.6478137931806,
'00002235': 991.2614284514957,
'00002236': 994.38800336488725}
plot={}
for k,v in plot_base.items():
plot.update({k: v})
for k,v in plot.items():
bar, = plt.bar(k, v, color='blue')
plt.pause(0.3)
plt.plot()
'''
def animate(i):
bars = []
for k, v in plot.items():
bar, = plt.bar(k, v, color='blue')
bars.append(bar)
return bars
fig=plt.figure(figsize=(12 ,7))
ani = matplotlib.animation.FuncAnimation(fig, animate, interval=1000, blit=True)
plt.show()
'''
When we run this code the values from the dictionary keep appending to the x-axis , that's why I want to make it scroll able(auto-scroll) , The triple quoted code shows the animation part, this make the whole graph appear at once.
I don't think I understand the data structure used here, so this focuses on the animating part. Suppose you have a function store.get_last_20_values from which to obtain the data you want to plot. Then the animation would look like:
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
fig, ax = plt.subplots()
def animate(i):
cat, val = store.get_last_20_values()
ax.clear()
ax.barh(cat, val)
ani = FuncAnimation(fig, animate, interval=1000)
plt.show()
For completeness here is how that function could look like, to make the above runnable.
class Store():
x = []
y = []
def update(self):
n = np.random.randint(0,5)
x = np.random.randint(100000,999999, size=n).astype(str)
y = np.random.randint(50,999, size=n)
self.x.extend(x)
self.y.extend(y)
self.x = self.x[-20:]
self.y = self.y[-20:]
def get_last_20_values(self):
# Some function to return 20 values. Need to adapt to custom needs.
self.update()
return self.x, self.y
store = Store()

Python Scatter Plot Multiple Colorbar issue

I am trying to draw scatter plot with dynamic data. I am able to draw the data points through looping; but everytime it creates new colorbar.
Here is my code:
import time
from threading import Thread
import pandas as pd
import matplotlib.pyplot as plt
import random
class RealTime:
def __init__(self):
self.flight_complete = True
self.data = pd.DataFrame(data=None, columns=list('ABC'))
self.fig=None
self.axis = None
def on_launch(self):
plt.ion()
self.fig = plt.figure()
self.axis = self.fig.add_subplot(111)
def create_data(self):
x = round(random.uniform(-1, 1), 2)
y = round(random.uniform(-1.65, 1.65), 2)
z = 0.5
temp_data = pd.DataFrame([[x, y, z]], columns=list('ABC'))
self.data = self.data.append(temp_data, ignore_index=True)
# Plot the data
self.plot()
def start_fly(self):
self.on_launch()
fly = Thread(target=self.fly_running)
fly.start()
def fly_running(self):
for _ in range(10):
print("Flying")
# Create the data
self.create_data()
time.sleep(0.1)
return
def plot(self):
plt.gca().cla()
self.data.plot(kind="scatter", x="A", y="B", alpha=0.4,
s=50, label="Real Time Position",
c="C", cmap=plt.get_cmap("jet"), colorbar=True, ax=self.axis)
plt.colormaps()
plt.title("Flight Path Map")
self.fig.canvas.draw()
self.fig.canvas.flush_events()
if __name__ == '__main__':
obj = RealTime()
obj.on_launch()
obj.fly_running()
I have read this post : How to retrieve colorbar instance from figure in matplotlib. But I couldn't really work with that.
Do you know why it creates a new colorbar? and how to avoid it?
Best Regards
Panda's plot is creating new colobar because you're asking it to create one (colorbar=True), and it looks like there is now way to tell the function that there is already a colorbar and that it should use that instead.
There are many ways to go around this problem.
the first one would be not not use DataFrame.plot() but instead use matplotlib directly to generate the plot. That will give you more control over the axes that are used and will let you recycle the colorbar from frame to frame. Here are some links that might be relevant:
How do you add a colormap to a matplotlib Animation?
Updating the positions and colors of pyplot.scatter
the second option if you want to keep your code close to what it is now it to erase the whole figure at each frame, and let pandas recreate the axes it need every time. i.e.:
def plot(self):
self.fig.clf()
self.axis = self.fig.add_subplot(111)
self.axis = self.data.plot(kind="scatter", x="A", y="B", alpha=0.4,
s=50, label="Real Time Position",
c="C", cmap=plt.get_cmap("jet"), colorbar=True, ax=self.axis)

Animate Quiver plot from values in Pandas dataframe

Hi im trying to make an animation of a quiver plot from data in my data frame
I have data stored like this in a pandas DataFrame, somewhat like this
QuivXLoc QuivYLoc QuivXVal QuivYVal QuivColorVal QuivPlotNum
0 -70.22 -127.241 1.624 -0.879 1.846623 1
1 -61.74 -127.241 -0.973 -0.027 0.973375 1
2 -65.98 -121.835 0.046 2.416 2.416438 1
3 -74.46 -121.835 -0.151 2.673 2.677262 1
4 -78.70 -116.429 1.073 -0.954 1.435773 2
I am currently plotting it like this, and it generates seperate plots for each sequence number perfectly.
for seq in quidf['QuivPlotNum'].unique():
temp=quidf[quidf['QuivPlotNum']==seq] ## make subset to plot
plt.quiver(temp['QuivXLoc'], temp['QuivYLoc'], temp['QuivXVal'], temp['QuivYVal'], # data
temp['QuivColorVal'], # colour the arrows based on this array
cmap=cm.jet, # colour map
headlength=3) # length of the arrows
Theres some additional code to format the plot that I left out.
What I'd like to do is animate the sequence based on iterating through the Sequence number in my data frame. All the examples I saw for Quiver Animation involved scaling previous function by some scalar that is incremented.
example of similar quiver animation I'd like to generate, I have tried but cannot figure out how to change update_quiver to work for my application:
Plotting animated quivers in Python
Using the matplotlib.animation module and its FuncAnimation class:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation
import pandas as pd
# read in the date and group it by the frame number
data = pd.read_csv('data2.csv', index_col=0)
grouped = data.groupby('QuivPlotNum')
# set up the figure
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.set_xlim(-200, 200)
ax.set_ylim(-200, 200)
# create empty plot for the update function to manipulate
plot = ax.quiver([], [], [], [], [], cmap='jet', headlength=3)
# create an iterator over the group, next() will return a tuple
# of QuivPlotNum, DataFrame
iterator = iter(grouped)
def update(i):
# get next thing in the iterator
key, data = next(iterator)
# set new x, y coordinates for the plot
plot.set_offsets(np.column_stack([data.QuivXLoc, data.QuivYLoc]))
# update vector and color values
plot.set_UVC(data.QuivXVal, data.QuivYVal, data.QuivColorVal)
# create the animation, update every 1000 ms
ani = FuncAnimation(fig, update, interval=1000)
# show it
plt.show()

Animate points with labels with matplotlib

I've got an animation with lines and now I want to label the points.
I tried plt.annotate() and I tried plt.text() but the labes don't move.
This is my example code:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def update_line(num, data, line):
newData = np.array([[1+num,2+num/2,3,4-num/4,5+num],[7,4,9+num/3,2,3]])
line.set_data(newData)
plt.annotate('A0', xy=(newData[0][0],newData[1][0]))
return line,
fig1 = plt.figure()
data = np.array([[1,2,3,4,5],[7,4,9,2,3]])
l, = plt.plot([], [], 'r-')
plt.xlim(0, 20)
plt.ylim(0, 20)
plt.annotate('A0', xy=(data[0][0], data[1][0]))
# plt.text( data[0][0], data[1][0], 'A0')
line_ani = animation.FuncAnimation(fig1, update_line, 25, fargs=(data, l),
interval=200, blit=True)
plt.show()
Can you help me please?
My next step is:
I have vectors with origin in these Points. These vectors change their length and their direction in each animation step.
How can I animate these?
Without animation this works:
soa =np.array( [ [data[0][0],data[1][0],F_A0[i][0][0],F_A0[i][1][0]],
[data[0][1],data[1][1],F_B0[i][0][0],F_B0[i][1][0]],
[data[0][2],data[1][2],F_D[i][0][0],F_D[i][1][0]] ])
X,Y,U,V = zip(*soa)
ax = plt.gca()
ax.quiver(X,Y,U,V,angles='xy',scale_units='xy',scale=1)
First thanks a lot for your fast and very helpful answer!
My Vector animation problem I have solved with this:
annotation = ax.annotate("C0", xy=(data[0][2], data[1][2]), xycoords='data',
xytext=(data[0][2]+1, data[1][2]+1), textcoords='data',
arrowprops=dict(arrowstyle="->"))
and in the 'update-function' I write:
annotation.xytext = (newData[0][2], newData[1][2])
annotation.xy = (data[0][2]+num, data[1][2]+num)
to change the start and end position of the vectors (arrows).
But what is, wehn I have 100 vectors or more? It is not practicable to write:
annotation1 = ...
annotation2 = ...
.
:
annotation100 = ...
I tried with a list:
...
annotation = [annotation1, annotation2, ... , annotation100]
...
def update(num):
...
return line, annotation
and got this error:
AttributeError: 'list' object has no attribute 'axes'
What can I do? Have you any idea?
I'm coming here from this question, where an annotation should be updated that uses both xy and xytext. It appears that, in order to update the annotation correctly, one needs to set the attribute .xy of the annotation to set the position of the annotated point and to use the .set_position() method of the annotation to set the position of the annotation. Setting the .xytext attribute has no effect -- somewhat confusing in my opinion. Below a complete example:
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
fig, ax = plt.subplots()
ax.set_xlim([-1,1])
ax.set_ylim([-1,1])
L = 50
theta = np.linspace(0,2*np.pi,L)
r = np.ones_like(theta)
x = r*np.cos(theta)
y = r*np.sin(theta)
line, = ax.plot(1,0, 'ro')
annotation = ax.annotate(
'annotation', xy=(1,0), xytext=(-1,0),
arrowprops = {'arrowstyle': "->"}
)
def update(i):
new_x = x[i%L]
new_y = y[i%L]
line.set_data(new_x,new_y)
##annotation.xytext = (-new_x,-new_y) <-- does not work
annotation.set_position((-new_x,-new_y))
annotation.xy = (new_x,new_y)
return line, annotation
ani = animation.FuncAnimation(
fig, update, interval = 500, blit = False
)
plt.show()
The result looks something like this:
In case that versions matter, this code has been tested on Python 2.7 and 3.6 with matplotlib version 2.1.1, and in both cases setting .xytext had no effect, while .set_position() and .xy worked as expected.
You have the return all objects that changed from your update function. So since your annotation changed it's position you should return it also:
line.set_data(newData)
annotation = plt.annotate('A0', xy=(newData[0][0],newData[1][0]))
return line, annotation
You can read more about matplotlib animations in this tutorial
You should also specify the init function so that the FuncAnimation knows which elements to remove from the plot when redrawing on the first update. So the full example would be:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# Create initial data
data = np.array([[1,2,3,4,5], [7,4,9,2,3]])
# Create figure and axes
fig = plt.figure()
ax = plt.axes(xlim=(0, 20), ylim=(0, 20))
# Create initial objects
line, = ax.plot([], [], 'r-')
annotation = ax.annotate('A0', xy=(data[0][0], data[1][0]))
annotation.set_animated(True)
# Create the init function that returns the objects
# that will change during the animation process
def init():
return line, annotation
# Create the update function that returns all the
# objects that have changed
def update(num):
newData = np.array([[1 + num, 2 + num / 2, 3, 4 - num / 4, 5 + num],
[7, 4, 9 + num / 3, 2, 3]])
line.set_data(newData)
# This is not working i 1.2.1
# annotation.set_position((newData[0][0], newData[1][0]))
annotation.xytext = (newData[0][0], newData[1][0])
return line, annotation
anim = animation.FuncAnimation(fig, update, frames=25, init_func=init,
interval=200, blit=True)
plt.show()
I think I figured out how to animate multiple annotations through a list. First you just create your annotations list:
for i in range(0,len(someMatrix)):
annotations.append(ax.annotate(str(i), xy=(someMatrix.item(0,i), someMatrix.item(1,i))))
Then in your "animate" function you do as you have already written:
for num, annot in enumerate(annotations):
annot.set_position((someMatrix.item((time,num)), someMatrix.item((time,num))))
(You can write it as a traditional for loop as well if you don't like the enumerate way). Don't forget to return the whole annotations list in your return statement.
Then the important thing is to set "blit=False" in your FuncAnimation:
animation.FuncAnimation(fig, animate, frames="yourframecount",
interval="yourpreferredinterval", blit=False, init_func=init)
It is good to point out that blit=False might slow things down. But its unfortunately the only way I could get animation of annotations in lists to work...

Categories

Resources