How do you add a colormap to a matplotlib Animatio

2019-08-20 06:41发布

问题:

Suppose I am trying to visualize an objects position using its X and Y axis positions, and using some other variable, 'Z' as the color map. This simplified example below illustrates how I am currently doing this.

import numpy as np
from matplotlib import pyplot as plt

dataX = np.linspace(-50,50,1000)
dataY = np.linspace(-50,50,1000)
dataZ = np.linspace(-50,50,1000)

plt.scatter(dataX, dataY, c=dataZ, cmap='winter', edgecolors='none')
plt.colorbar()
plt.show()

and the result:

I want to add a live animation to this instead of just showing a static image, but I am struggling to add the colormap to it. The code below shows how I am doing it without a colormap.

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
import Tkinter
import tkMessageBox

def restart():
    root = Tkinter.Tk()
    root.withdraw()
    result = tkMessageBox.askyesno("Restart", "Would you like to restart the animation?")
    if result:
        ani.frame_seq = ani.new_frame_seq() 
        ani.event_source.start()
    else:
        plt.close()

dataX = np.linspace(-50,50,1000)
dataY = np.linspace(-50,50,1000)
dataZ = np.linspace(-50,50,1000)


def function(num, dataX,dataY, line):
    line.set_data(dataX[..., :num],dataY[..., :num])
    if num == dataX.size :
        restart()
    return line,

fig = plt.figure()

l, = plt.plot([], [], 'ro', markeredgewidth=0.0)
limitsX = [min(dataX)-100,max(dataX)+100]
limitsY = [min(dataY)-100, max(dataY)+100]
plt.xlim(limitsX[0],limitsX[1] )
plt.ylim(limitsY[0],limitsY[1] )
plt.xlabel('x')
plt.ylabel('y')
plt.title('test')

ani = animation.FuncAnimation(fig, function, (dataX.size+1), fargs=(dataX,dataY,l),
                              interval=10, blit = True, repeat = False)

plt.show()

Re-asking the question: How do I add a colormap to my animation?

回答1:

This answer is loosely based on my answer to a similar problem, although I felt that the specific case of handling a PathCollection (as returned by scatter()) warrants a new answer.

Here is the way I would approach the problem. The trick is to generate a separate, static colorbar on a second axe in the figure. Then, when updating the PathCollection properties, use that colorbar and normalization to update the color of the points.

dataX = np.linspace(-50,50,1000)
dataY = np.linspace(-50,50,1000)
dataZ = np.linspace(-50,50,1000)


def animate(num):
    data = np.hstack((dataX[:num,np.newaxis], dataY[:num, np.newaxis]))
    art.set_offsets(data)
    art.set_color(cmap(norm(dataZ[:num]))) # update colors using the colorbar and its normalization defined below
    return art,


fig,[ax,cax] = plt.subplots(1,2, gridspec_kw={"width_ratios":[50,1]})
# Set the colormap and norm to correspond to the data for which
# the colorbar will be used.
cmap = matplotlib.cm.winter
norm = matplotlib.colors.Normalize(vmin=-50, vmax=50)

cb1 = matplotlib.colorbar.ColorbarBase(cax, cmap=cmap,
                                norm=norm,
                                orientation='vertical')

ax.set_xlim(-60,60)
ax.set_ylim(-60,60)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title('test')
art = ax.scatter([],[],c=[])

ani = animation.FuncAnimation(fig, animate,interval=2, blit=True, repeat=True)

plt.show()