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?