如何将颜色图添加到matplotlib动画中? [英] How do you add a colormap to a matplotlib Animation?
问题描述
假设我正在尝试使用对象的X和Y轴位置以及其他一些变量 Z作为颜色映射形象化对象的位置。下面的简化示例说明了我当前的操作方式。
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()
结果为:
我想为此添加一个实时动画,而不仅仅是显示静态图像,但是我正在努力为它添加色彩图。下面的代码显示了我如何没有使用颜色图。
import matplotlib.pyplot as plt
导入matplotlib.animation为动画
导入numpy为np
导入Tkinter
导入tkMessageBox
def restart():
root = Tkinter .Tk()
root.withdraw()
结果= tkMessageBox.askyesno(重新启动,您想重新启动动画吗?)
如果结果:
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函数(num,dataX,dataY,line):
line.set_data(dataX [...,:num],dataY [...,:num])
如果num == dataX .size:
restart()
返回行,
图= plt.figure()
l,= plt.plot([],[ ],'ro',markeredgewidth = 0.0)
limitX = [min(dataX)- 100,max(dataX)+100]
limitY = [min(dataY)-100,max(dataY)+100]
plt.xlim(limitsX [0],limitsX [1])$ b $ b plt.ylim(limitsY [0],limitsY [1])$ b $ b 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,重复= False)
plt.show()
重新询问问题:如何向动画中添加色彩图?
此答案大致基于
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?
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()
这篇关于如何将颜色图添加到matplotlib动画中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!