Matplotlib animation for an particle swarm optimazation - python

I'm doing a school project on evolutionary algorithms. I applied 2D particle swarm optimazation on an problem. I modified the PSO function such that I get as output:
a vector with the best parameters
the optimized solution
a list of array's that contains the x-values of the particles (lijst1)
a list of array's that contains the y-values of the particles (lijst2)
To visualise it, I wrote a loop that iterates through the list and plots the particles in 2D space. This not an elegant solution so I want to use animate from matplotlib.
I want to achieve that each frame shows an iteration of the PSO algorithm or in other words each frame should plot lijst1[i] and lijst2[i].
To give you guys an idea of the output I ran the algorithm a few iterations with few particles.
Lijst1:
[array([-3. , -1.03182383, -1.29765138, -2.63259751, 1.83881941]), array([-3. , -0.99308702, -1.61309168, -1.99811905, 1.31329269]), array([-2.26835254, -1.00436484, -1.48864562, -1.35639631, -0.49339506]), array([-1.89641446, -1.05791927, -1.22609973, -0.94357677, -1.416652 ])]
Lijst2:
[array([ 6. , 6. , 2.25620012, -0.49762621, 1.20955011]), array([ 5.39456692, 5.66213386, 1.53168852, -0.23988646, 1.97288628]), array([3.70817355, 4.68329424, 1.37540136, 0.88142455, 1.08398406]), array([2.60343502, 2.80393532, 1.67560348, 2.0280248 , 0.29435041])]
I used the sample code from the matplotlib website and modified it. Here is where I need help:
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
%matplotlib notebook
n = 4
fig, ax = plt.subplots()
xdata, ydata = [], []
ln, = plt.plot([], [], 'ro')
def init():
ax.set_xlim(-1,1)
ax.set_ylim(-1, 1)
return ln,
def update(frame):
xdata.append(lijst1[frame])
ydata.append(lijst2[frame])
ln.set_data(xdata, ydata)
return ln,
ani = FuncAnimation(fig, update, frames=np.linspace(0,n-1,n), interval=1000, init_func=init, blit=True)
plt.show()
What I first wanted to achieve is to plot all the frames in the same figure. The lijst1 and lijst2 have 4 indices. So I thought let the frames denote [0,1,2,3] so that each frame lijst1[i] and lijst[i] get appended to the data. In the end probably my whole figure will be filled with particles, but that is okay for now.
For some reason I don't get input at all. I'm not sure why this is. Could I please get some feedback on this?
Question: How do I modify the animation function such that I visualise the convergence process of the PSO algorithm?
Thanks in advance

I wrote the code with the understanding that your question is to loop through the contents of the list data presented to you. The change from your code is to set the range of the axes. I also removed the initialization function as I thought it was unnecessary. I added a line from the scatter plot format as a graph setting. This is just a modification to check the animation.
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
%matplotlib notebook
# from IPython.display import HTML
import numpy as np
from matplotlib.animation import PillowWriter
lijst1 = [np.array([-3. , -1.03182383, -1.29765138, -2.63259751, 1.83881941]), np.array([-3. , -0.99308702, -1.61309168, -1.99811905, 1.31329269]), np.array([-2.26835254, -1.00436484, -1.48864562, -1.35639631, -0.49339506]), np.array([-1.89641446, -1.05791927, -1.22609973, -0.94357677, -1.416652 ])]
lijst2 = [np.array([ 6. , 6. , 2.25620012, -0.49762621, 1.20955011]), np.array([ 5.39456692, 5.66213386, 1.53168852, -0.23988646, 1.97288628]), np.array([3.70817355, 4.68329424, 1.37540136, 0.88142455, 1.08398406]), np.array([2.60343502, 2.80393532, 1.67560348, 2.0280248 , 0.29435041])]
n = 4
fig, ax = plt.subplots()
ax = plt.axes(xlim=(-5,5), ylim=(-2, 10))
xdata, ydata = [], []
ln, = plt.plot([], [], 'ro--', lw=2)
def init():
ax.set_xlim(-1,1)
ax.set_ylim(-1, 1)
return ln,
def update(frame):
# xdata.append(lijst1[frame])
# ydata.append(lijst2[frame])
xdata = lijst1[frame].tolist()
ydata = lijst2[frame].tolist()
ln.set_data(xdata, ydata)
return ln,
ani = FuncAnimation(fig, update, frames=len(lijst1), interval=1000, blit=True)# init_func=init,
plt.show()
ani.save('swarm_ani.gif', writer='pillow')
# plt.close()
# HTML(ani.to_html5_video())

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,

Matplotlib animation function: plots points but no animation [duplicate]

I'm trying to do an animation of a scatter plot where colors and size of the points changes at different stage of the animation. For data I have two numpy ndarray with an x value and y value:
data.shape = (ntime, npoint)
x.shape = (npoint)
y.shape = (npoint)
Now I want to plot a scatter plot of the type
pylab.scatter(x,y,c=data[i,:])
and create an animation over the index i. How do I do this?
Suppose you have a scatter plot, scat = ax.scatter(...), then you can
change the positions
scat.set_offsets(array)
where array is a N x 2 shaped array of x and y coordinates.
change the sizes
scat.set_sizes(array)
where array is a 1D array of sizes in points.
change the color
scat.set_array(array)
where array is a 1D array of values which will be colormapped.
Here's a quick example using the animation module.
It's slightly more complex than it has to be, but this should give you a framework to do fancier things.
(Code edited April 2019 to be compatible with current versions. For the older code see revision history)
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
class AnimatedScatter(object):
"""An animated scatter plot using matplotlib.animations.FuncAnimation."""
def __init__(self, numpoints=50):
self.numpoints = numpoints
self.stream = self.data_stream()
# Setup the figure and axes...
self.fig, self.ax = plt.subplots()
# Then setup FuncAnimation.
self.ani = animation.FuncAnimation(self.fig, self.update, interval=5,
init_func=self.setup_plot, blit=True)
def setup_plot(self):
"""Initial drawing of the scatter plot."""
x, y, s, c = next(self.stream).T
self.scat = self.ax.scatter(x, y, c=c, s=s, vmin=0, vmax=1,
cmap="jet", edgecolor="k")
self.ax.axis([-10, 10, -10, 10])
# For FuncAnimation's sake, we need to return the artist we'll be using
# Note that it expects a sequence of artists, thus the trailing comma.
return self.scat,
def data_stream(self):
"""Generate a random walk (brownian motion). Data is scaled to produce
a soft "flickering" effect."""
xy = (np.random.random((self.numpoints, 2))-0.5)*10
s, c = np.random.random((self.numpoints, 2)).T
while True:
xy += 0.03 * (np.random.random((self.numpoints, 2)) - 0.5)
s += 0.05 * (np.random.random(self.numpoints) - 0.5)
c += 0.02 * (np.random.random(self.numpoints) - 0.5)
yield np.c_[xy[:,0], xy[:,1], s, c]
def update(self, i):
"""Update the scatter plot."""
data = next(self.stream)
# Set x and y data...
self.scat.set_offsets(data[:, :2])
# Set sizes...
self.scat.set_sizes(300 * abs(data[:, 2])**1.5 + 100)
# Set colors..
self.scat.set_array(data[:, 3])
# We need to return the updated artist for FuncAnimation to draw..
# Note that it expects a sequence of artists, thus the trailing comma.
return self.scat,
if __name__ == '__main__':
a = AnimatedScatter()
plt.show()
If you're on OSX and using the OSX backend, you'll need to change blit=True to blit=False in the FuncAnimation initialization below. The OSX backend doesn't fully support blitting. The performance will suffer, but the example should run correctly on OSX with blitting disabled.
For a simpler example, which just updates the colors, have a look at the following:
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
def main():
numframes = 100
numpoints = 10
color_data = np.random.random((numframes, numpoints))
x, y, c = np.random.random((3, numpoints))
fig = plt.figure()
scat = plt.scatter(x, y, c=c, s=100)
ani = animation.FuncAnimation(fig, update_plot, frames=range(numframes),
fargs=(color_data, scat))
plt.show()
def update_plot(i, data, scat):
scat.set_array(data[i])
return scat,
main()
I wrote celluloid to make this easier. It's probably easiest to show by example:
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
from celluloid import Camera
numpoints = 10
points = np.random.random((2, numpoints))
colors = cm.rainbow(np.linspace(0, 1, numpoints))
camera = Camera(plt.figure())
for _ in range(100):
points += 0.1 * (np.random.random((2, numpoints)) - .5)
plt.scatter(*points, c=colors, s=100)
camera.snap()
anim = camera.animate(blit=True)
anim.save('scatter.mp4')
It uses ArtistAnimation under the hood. camera.snap captures the current state of the figure which is used to create the frames in the animation.
Edit: To quantify how much memory this uses I ran it through memory_profiler.
Line # Mem usage Increment Line Contents
================================================
11 65.2 MiB 65.2 MiB #profile
12 def main():
13 65.2 MiB 0.0 MiB numpoints = 10
14 65.2 MiB 0.0 MiB points = np.random.random((2, numpoints))
15 65.2 MiB 0.1 MiB colors = cm.rainbow(np.linspace(0, 1, numpoints))
16 65.9 MiB 0.6 MiB fig = plt.figure()
17 65.9 MiB 0.0 MiB camera = Camera(fig)
18 67.8 MiB 0.0 MiB for _ in range(100):
19 67.8 MiB 0.0 MiB points += 0.1 * (np.random.random((2, numpoints)) - .5)
20 67.8 MiB 1.9 MiB plt.scatter(*points, c=colors, s=100)
21 67.8 MiB 0.0 MiB camera.snap()
22 70.1 MiB 2.3 MiB anim = camera.animate(blit=True)
23 72.1 MiB 1.9 MiB anim.save('scatter.mp4')
To summarize this:
Creating 100 plots used 1.9 MiB.
Making the animation used 2.3 MiB.
This method of making animations used 4.2 MiB of memory in sum.
TL/DR: If you are having trouble with the ax.set_... methods for animating your scatter plot, you can try to just clear the plot each frame (i.e., ax.clear()) and re-plot things as desired. This is slower, but might be useful when you want to change a lot of things in a small animation.
Here is an example demonstrating this "clear" approach:
import itertools
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
# set parameters
frames = 10
points = 20
np.random.seed(42)
# create data
data = np.random.rand(points, 2)
# set how the graph will change each frame
sizes = itertools.cycle([10, 50, 150])
colors = np.random.rand(frames, points)
colormaps = itertools.cycle(['Purples', 'Blues', 'Greens', 'Oranges', 'Reds'])
markers = itertools.cycle(['o', 'v', '^', 's', 'p'])
# init the figure
fig, ax = plt.subplots(figsize=(5,5))
def update(i):
# clear the axis each frame
ax.clear()
# replot things
ax.scatter(data[:, 0], data[:, 1],
s=next(sizes),
c=colors[i, :],
cmap=next(colormaps),
marker=next(markers))
# reformat things
ax.set_xlabel('world')
ax.set_ylabel('hello')
ani = animation.FuncAnimation(fig, update, frames=frames, interval=500)
ani.save('scatter.gif', writer='pillow')
The tutorials I have seen from matplotlib and other sources do not seem to use this approach, but I have seen others (as well as myself) suggest it on this site. I see some pros & cons, but I would appreciate anyone else's thoughts:
Pros
You can avoid using the set_... methods for the scatter plot (i.e. .set_offsets(), .set_sizes(), ...), which have more obscure and less-detailed documentation (though the leading answer will get you very far here!). Plus, there are different methods for different plot types (e.g. you use set_data for lines, but not for scatter points). By clearing the axis, you determine the plotted elements each frame like any other plot in matplotlib.
Even more so, it is unclear if some properties are set-able, such as the marker type (as commented) or the colormap. I wouldn't know how to create the above plot using ax.set_..., for example, because of the marker and colormap changes. But this is pretty basic with ax.scatter().
Cons
It can be much slower; i.e. clearing and redrawing everything appears to be more expensive than the set... methods. So for large animations, this approach can be kind of painful.
Clearing the axis also clears things like the axis labels, axis limits, other text, etc. So, those sorts of formatting things need to be included in update (else they will be gone). This can be annoying if you want some things to change, but others to stay the same.
Of course, the speed is a big con. Here is an example showing the difference. Given this data:
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
np.random.seed(42)
frames = 40
x = np.arange(frames)
y = np.sin(x)
colors = itertools.cycle(['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'])
data = [(np.random.uniform(-1, 1, 10) + x[i],
np.random.uniform(-1, 1, 10) + y[i])
for i in range(frames)]
You can plot using the set... method:
fig, ax = plt.subplots()
s = ax.scatter([], [])
ax.set_xlim(-2, frames+2)
ax.set_ylim(min(y) - 1, max(y) + 1)
def update(i):
s.set_offsets(np.column_stack([data[i][0], data[i][1]]))
s.set_facecolor(next(colors))
ani = animation.FuncAnimation(fig, update, frames=frames, interval=100)
ani.save('set.gif', writer='pillow')
Or the "clear" method:
fig, ax = plt.subplots()
def update(i):
ax.clear()
ax.scatter(data[i][0], data[i][1], c=next(colors))
ax.set_xlim(-2, frames+2)
ax.set_ylim(min(y) - 1, max(y) + 1)
ani = animation.FuncAnimation(fig, update, frames=frames, interval=100)
ani.save('clear.gif', writer='pillow')
To get this figure:
Using %%time, we can see that clearing and replotting takes (more than) twice as long:
for set...: Wall time: 1.33 s
for clear: Wall time: 2.73 s
Play with the frames parameter to test this at different scales. For smaller animations (less frames/data), the time difference between the two methods is inconsequential (and for me, sometimes causes me to prefer the clearing method). But for larger cases, using set_... can save significant time.
Here is the thing. I used to a user of Qt and Matlab and I am not quite familiar with the animation system on the matplotlib.
But I do have find a way that can make any kind of animation you want just like it is in matlab. It is really powerful. No need to check the module references and you are good to plot anything you want. So I hope it can help.
The basic idea is to use the time event inside PyQt( I am sure other Gui system on the Python like wxPython and TraitUi has the same inner mechanism to make an event response. But I just don't know how). Every time a PyQt's Timer event is called I refresh the whole canvas and redraw the whole picture, I know the speed and performance may be slowly influenced but it is not that much.
Here is a little example of it:
import sys
from PyQt4 import QtGui
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
import numpy as np
class Monitor(FigureCanvas):
def __init__(self):
self.fig = Figure()
self.ax = self.fig.add_subplot(111)
FigureCanvas.__init__(self, self.fig)
self.x = np.linspace(0,5*np.pi,400)
self.p = 0.0
self.y = np.sin(self.x+self.p)
self.line = self.ax.scatter(self.x,self.y)
self.fig.canvas.draw()
self.timer = self.startTimer(100)
def timerEvent(self, evt):
# update the height of the bars, one liner is easier
self.p += 0.1
self.y = np.sin(self.x+self.p)
self.ax.cla()
self.line = self.ax.scatter(self.x,self.y)
self.fig.canvas.draw()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
w = Monitor()
w.setWindowTitle("Convergence")
w.show()
sys.exit(app.exec_())
You can adjust the refresh speed in the
self.timer = self.startTimer(100)
I am just like you who want to use the Animated scatter plot to make a sorting animation. But I just cannot find a so called "set" function. So I refreshed the whole canva.
Hope it helps..
Why Not try this
import numpy as np
import matplotlib.pyplot as plt
x=np.random.random()
y=np.random.random()
fig, ax = plt.subplots()
ax.scatter(x,y,color='teal')
ax.scatter(y,x,color='crimson')
ax.set_xlim([0,1])
ax.set_ylim([0,1])
for i in np.arange(50):
x=np.random.random()
y=np.random.random()
bha=ax.scatter(x,y)
plt.draw()
plt.pause(0.5)
bha.remove()
plt.show()

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()

Matplotlib animation iterating over list of pandas dataframes

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/

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