Removing points stored in a dictionary in a matplotlib basemap animation - python

I am trying to do the following: Plot points and store a reference in a dictionary. While animating remove points. A minimal example looks as follows:
%matplotlib qt
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
import numpy as np
import matplotlib.animation as animation
fig = plt.figure()
m = Basemap(projection='aeqd',lat_0=72,lon_0=29, resolution='l',
llcrnrlon=15, llcrnrlat=69,
urcrnrlon=41, urcrnrlat=75.6,area_thresh = 100)
pointDict=dict()
pointDict[1]=m.plot (0, 0,marker='.',label='first')[0]
pointDict[2]=m.plot (0, 0,marker='.',label='second')[0]
def init():
print ("Init")
x,y = m(30, 73)
pointDict[1].set_data(x,y)
x,y = m(31, 73)
pointDict[2].set_data(x,y)
return pointDict.values()
def animate(i):
print ("Frame {0}".format(i))
if i==2:
l=pointDict.pop(1)
print ("Removing {0}".format(l.get_label()))
l.remove()
del l
return pointDict.values()
anim = animation.FuncAnimation(plt.gcf(), animate, init_func=init,
frames=10, interval=1000, blit=True)
plt.show()
Output:
Init
Init
Frame 0
Frame 1
Frame 2
Removing first
Frame 3
Interestingly, if I am plotting just the first point (that is, remove pointDict[2]=m.plot and pointDict[2].set_data in the init function), this works. But if both are plotted, neither removing the first, nor the second point works.
Related questions brought me just as far as I am now:
Matplotlib Basemap animation
How to remove lines in a Matplotlib plot
Matplotlib animating multiple lines and text
Python, Matplotlib, plot multi-lines (array) and animation
I am using Anaconda with Python-2.7 kernel.

I found out what the problem is and want therefore to answer my question by myself:
The problem with this is somewhat unexpected the blit=True.
Obviously, blitting can be only used if the point is set within the animate function. Thus, setting the data in the init routine causes problems.
So there are two options: set blit to False, but this is not very elegant. The other option is to set the points in the first frame.
Then the init and animate functions that work are as follows:
def init():
print ("Init")
pointDict[1].set_data([],[])
pointDict[2].set_data([],[])
return pointDict.values()
def animate(i):
print ("Frame {0}".format(i))
if i==0:
print ("Init")
x,y = m(30, 73)
pointDict[1].set_data(x,y)
x,y = m(31, 73)
pointDict[2].set_data(x,y)
if i==2:
l=pointDict.pop(1)
print ("Removing {0}".format(l.get_label()))
l.remove()
del l
return pointDict.values()
anim = animation.FuncAnimation(plt.gcf(), animate, init_func=init,
frames=10, interval=1000, blit=True)
plt.show()

Related

Why does my matplotlib animation give an empty axis?

I am trying to animate a simple demonstration of Benfold's Law. I am expecting an animated bar graph from this code:
import matplotlib.animation as animation
fig = plt.figure()
plt.xticks(np.arange(1,10))
def animate(i):
plt.title("Iteration: " + str(i))
plt.plot(np.arange(1,10,1),1000*benford[1:], linestyle="", marker="d",color='r')
plt.bar(all_leads[i].keys(), all_leads[i].values())
ani = animation.FuncAnimation(fig, animate, interval=100)
plt.show()
I get an empty plot.
Empty Plot
The animate(i) function works to give a correct individual plot
animate(10)
Image of correctly produced plot
Any ideas what I am doing wrong.
Because I don't have your data, I made a mock animation as best I could from what was provided. First, you have to plot your graph outside of the animate function, then you have to update the x and y data within the animate function, lastly - to loop your animation, you have to set frames to some value (we will go with 10 for your case). While this all isn't perfectly in line with your graphs (again, I don't have your data), this should get you started. You can also take a look at my other answer for an additional example.
%matplotlib notebook # If you are working in jupyter notebook
import matplotlib.animation as animation
fig,ax = plt.subplots()
plt.xticks(np.arange(1,10))
plot, = ax.plot(np.arange(1,10,1),np.arange(1,10,1), linestyle="", marker="d",color='r')
def animate(i):
plot.set_ydata(np.arange(1,10,1)[i:i+3])
plot.set_xdata(np.arange(1,10,1)[i:i+3])
plt.title("Iteration: " + str(i))
ani = animation.FuncAnimation(fig, animate, interval=100, frames=10)
plt.show()
You can also add an xlim argument in the animate function to follow your animation across the x-axis:
def animate(i):
plot.set_ydata(np.arange(1,10,1)[i:i+3])
plot.set_xdata(np.arange(1,10,1)[i:i+3])
plt.title("Iteration: " + str(i))
plt.xlim(i, i+4)
Gives:

Blit Behaviour in FuncAnimate -Want to keep previous data

I'm trying to animate a figure using matplotlib->FuncAnimate function. However, I'm having trouble understanding how Blit works. With each frame, I want to draw only the new data point on top of the old one. It says that using Blit it should automatically update only the values that changed. Thus, if I turn it on (blit=True) the previous data points should remain in my figure. But this is not the case. The previous data get deleted and the figure gets redraw from scratch.
In the documentation, it says that I have to return "iterable_of_artists" and the algorithm will know which data has changed. I want to just pass the new data and just plot on top of the old one. By the way, what is an "iterable_of_artists", is that just a list of objects that can be drawn? if someone could point me out to the definition, I would appreciate it.
Anyway, I have worked several base examples that show the odd behavior. In the first example, I'm turning Blit=True and drawing only the new data using the animate function. This in theory should draw on top of the old ones, but is not the case, only the new data is drawn.
import time
import random
import numpy
import matplotlib
import matplotlib.pyplot as pyplot
from matplotlib.animation import FuncAnimation
def livePlot():
fig, ax = pyplot.subplots(1,1)
ax = pyplot.axes(xlim=(0, 2), ylim=(0, 100))
line, = ax.plot([], [], 'ro') #ax.plot will return a tupple
def init():
line.set_data(0, 50)
return line, #Return is not necessary when blit=False
def animate(frame):
x = frame
y = random.randint(0, 100)
line.set_data(x,y)
return line, #Return is not necessary when blit=False
animation = FuncAnimation(
fig, animate,
init_func = init,
frames= [0.5, 1, 1.5, 2.0],
interval=1000,
repeat=False,
blit=True, # Turning on Blit
cache_frame_data = True)
pyplot.show()
if __name__ == "__main__":
livePlot()
I was able to achieve my goal by tricking the FuncAnimate method. I can use the ax and plot in each frame the new data. If I do that, the old data remains and only the new data is drawn. However, I can do that with Blit=True or Blit=False, it has no effect. So, I'm so confused on how Blit works and what would be the correct way to plot only the new data without having to create a list with all the data to plot. Passing a large list will create a large variable in memory if I have a long set of data points. Here is my workaround but I'm not sure if this is the correct way to do it or if there is a better ways of using Blit=True and just redraw the new data.
import time
import random
import numpy
import matplotlib
import matplotlib.pyplot as pyplot
from matplotlib.animation import FuncAnimation
def livePlot():
fig, ax = pyplot.subplots(1,1)
ax = pyplot.axes(xlim=(0, 2), ylim=(0, 100))
def init():
ax.plot(0, 50, 'ro')
return []
def animate(frame):
x = frame
y = random.randint(0, 100)
ax.plot(x, y, 'ro') # plotting directly on the axis. This keeps the old data
return [] # fooling the blit algorithm with an empty stream
animation = FuncAnimation(
fig, animate,
init_func = init,
frames= [0.5, 1, 1.5, 2.0],
interval=1000,
repeat=False,
blit=True,
cache_frame_data = True)
pyplot.show()
if __name__ == "__main__":
livePlot()

How to save an animation without showing the previous frames in the video?

I would like to save an animation using Python but I get the frames superposed! I want to get the frames displayed individually.
Please here what I used:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
from numpy import pi, cos, sin
fig = plt.figure()
plt.axis([-1.5, 1.5,-1.5, 1.5])
ax = plt.gca()
ax.set_aspect(1)
N=100
xp = [None] * N
yp = [None] * N
def init():
# initialize an empty list of cirlces
return []
def animate(i):
xp[i]=sin(i*pi/10)
yp[i]=cos(i*pi/10)
patches = []
patches.append(ax.add_patch( plt.Circle((xp[i],yp[i]),0.02,color='b') ))
return patches
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=N-1, interval=20, blit=True)
anim.save("example.avi")
plt.show()
There are some things I'm not sure about and it really seems to be that the axis.plot() behavior and FuncAnimate() behavior are different. However, the code below works for both.
Use only one patch (in your case)
The key point from your code is that you are adding a new circle in addition to the old circles every iteration:
patches = []
patches.append(ax.add_patch( plt.Circle((xp[i],yp[i]),0.02,color='b') ))
Even though you clear the patches list, they are still stored in the axis.
Instead, just create one circle and change its position.
Clear first frame with init()
Also, init() needs to clear the patch from the base frame.
Standalone Example
from matplotlib import pyplot as plt
from matplotlib import animation
from numpy import pi, cos, sin
fig = plt.figure()
plt.axis([-1.5, 1.5, -1.5, 1.5])
ax = plt.gca()
ax.set_aspect(1)
N = 100
xp = []
yp = []
# add one patch at the beginning and then change the position
patch = plt.Circle((0, 0), 0.02, color='b')
ax.add_patch(patch)
def init():
patch.set_visible(False)
# return what you want to be cleared when axes are reset
# this actually clears even if patch not returned it so I'm not sure
# what it really does
return tuple()
def animate(i):
patch.set_visible(True) # there is probably a more efficient way to do this
# just change the position of the patch
x, y = sin(i*pi/10), cos(i*pi/10)
patch.center = x, y
# I left this. I guess you need a history of positions.
xp.append(x)
yp.append(y)
# again return what you want to be cleared after each frame
# this actually clears even if patch not returned it so I'm not sure
# what it really does
return tuple()
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=N-1, interval=20, blit=True)
# for anyone else, if you get strange errors, make sure you have ffmpeg
# on your system and its bin folder in your path or use whatever
# writer you have as: writer=animation.MencoderWriter etc...
# and then passing it to save(.., writer=writer)
anim.save('example.mp4')
plt.show()
Return values???
Regarding the return values of init() and animate(), It doesn't seem to matter what is returned. The single patch still gets moved around and drawn correctly without clearing previous ones.

Matplotlib animate does not update tick labels

I am trying to modify and example by making the animation run on increasing x values. I want update the x axis tick labels to update according to the x values.
I am trying to use the animation features (specifically FuncAnimation) in 1.2. I can set the xlimit but the tick labels are not updating. I tried explicitly setting the tick labels too and this does not work.
I saw this: Animating matplotlib axes/ticks and
I tried to adjust the bbox in animation.py but it did not work. I am fairly new to matplotlib and do not know enough about what is really going on to address this issue so I would appreciate any help.
Thank you
"""
Matplotlib Animation Example
author: Jake Vanderplas
email: vanderplas#astro.washington.edu
website: http://jakevdp.github.com
license: BSD
Please feel free to use and modify this, but keep the above information. Thanks!
"""
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = plt.axes(ylim=(-2, 2))
line, = ax.plot([], [], lw=2)
# initialization function: plot the background of each frame
def init():
line.set_data([], [])
return line,
# animation function. This is called sequentially
def animate(i):
x = np.linspace(i, i+2, 1000)
y = np.sin(2 * np.pi * (x - 0.01 * i))
line.set_data(x, y)
ax.set_xlim(i, i+2)
return line,
# call the animator. blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=200, interval=20, blit=True)
plt.show()
See Animating matplotlib axes/ticks, python matplotlib blit to axes or sides of the figure?, and Animated title in matplotlib
The simple answer is remove blit=True
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=200, interval=20)
If you have blit = True only artists that have changed are re-drawn (rather than re-drawing all of the artists) which makes the rendering more efficient. Artists are marked as changed if they are returned from the update-function (in this case animate). The other detail is that the artists must be with in the axes bounding box with the way the code works in animation.py. See one of the links at the top for how to deal with this.

Python - Matplotlib FuncAnimation - Mac - Not returning points

Using the following code I am attempting to have points iteratively added to a graph. Since I am on OSX, I am not using blit=True. I can get a single point at the original to plot and can see output from my update function (fed from a generator). I can also see that my coordinates are being appended to the array of coordinates to be plotted. What am I missing in getting my generated / updated points visualized?
the stromboli function, called by data_gen() returns a pair of coordinates. It could be a random x,y for all intents and purposes.
#Visualization Imports
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def update(coord):
print coord[0], coord[1]
pt.set_xdata(numpy.append(pt.get_xdata(),coord[0]))
pt.set_ydata(numpy.append(pt.get_ydata(),coord[1]))
print pt.get_xdata()
return pt,
def data_gen():
while True:
yield stromboli(args.velocity)
#Visualization
fig = plt.figure()
ax = plt.axes()
pt, = ax.plot([], [],'ro')
ani = animation.FuncAnimation(fig, update, data_gen, interval=100)
plt.plot(0,0,'b*')
plt.show()
I suspect you might be getting something funny with effective closures (are you running this in an interactive environment?) in that pt will be defined from a previous code execution, which is what gets grabbed when def update runs. You then make a new version of pt and but that is not what is getting updated by update
Either move pt, = ax.plot([],[],'ro') above the definition of update or try passing in pt as an argument.
ex:
def update(coord,pt):
print coord[0], coord[1]
pt.set_xdata(numpy.append(pt.get_xdata(),coord[0]))
pt.set_ydata(numpy.append(pt.get_ydata(),coord[1]))
print pt.get_xdata()
return pt,
....
ani = animation.FuncAnimation(fig, update, data_gen, fargs = (pt,),interval=100)

Categories

Resources