如何在 python 中的交互式绘图动画中设置颜色图
How to set a colormap in interactive plot animations in python
下面的代码通过散点图每帧绘制 30k 个点来创建一个 600k 点的动画。动画完美无缺,除了我不知道如何在动画中包含我的颜色图(Heatintensity)。 Xs 和 Ys 正在变化,但点的颜色只是蓝色。
import numpy as np
import matplotlib.pyplot as plt
Heatintensity=workdata[0:600000] #Values controlling scatter colormap
Xs=xCoord[0:600000]
Ys=yCoord[0:600000]
plt.ion()
fig, ax = plt.subplots()
sc = ax.scatter(Xs, Ys, c=Heatintensity, cmap=cm.jet, s=5)
plt.draw()
for i in range(20):
sc.set_offsets(np.c_[Xs[(i*30000):(i*30000)+30000],\
Ys[(i*30000):(i*30000)+30000]])
fig.canvas.draw_idle()
plt.pause(0.1)
要更改颜色,您需要使用
sc.set_array(Heatintensity[(i*30000):(i*30000)+30000])
除了更改偏移量。
为了让颜色在每个动画步骤中代表相同的数值,必须将散点归一化为所有数据,
norm = plt.Normalize(Heatintensity.min(), Heatintensity.max())
完整示例:
import numpy as np
import matplotlib.pyplot as plt
Heatintensity=np.random.rand(600000) #Values controlling scatter colormap
Xs=np.random.rand(600000)
Ys=np.random.rand(600000)
plt.ion()
fig, ax = plt.subplots()
norm = plt.Normalize(Heatintensity.min(), Heatintensity.max())
sc = ax.scatter(Xs, Ys, c=Heatintensity, s=5, cmap=plt.cm.jet, norm=norm)
plt.draw()
for i in range(20):
# set coordinates
sc.set_offsets(np.c_[Xs[(i*30000):(i*30000)+30000],\
Ys[(i*30000):(i*30000)+30000]])
# set colors
sc.set_array(Heatintensity[(i*30000):(i*30000)+30000])
# draw and make pause
plt.pause(0.1)
plt.ioff()
plt.show()
同样可以使用 FuncAnimation
:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
Heatintensity=np.random.rand(600000) #Values controlling scatter colormap
Xs=np.random.rand(600000)
Ys=np.random.rand(600000)
fig, ax = plt.subplots()
norm = plt.Normalize(Heatintensity.min(), Heatintensity.max())
sc = ax.scatter(Xs, Ys, c=Heatintensity, s=5, cmap=plt.cm.jet, norm=norm)
def update(i):
# set coordinates
sc.set_offsets(np.c_[Xs[(i*30000):(i*30000)+30000],\
Ys[(i*30000):(i*30000)+30000]])
# set colors
sc.set_array(Heatintensity[(i*30000):(i*30000)+30000])
ani = animation.FuncAnimation(fig, update, frames=range(20), interval=100)
plt.show()
下面的代码通过散点图每帧绘制 30k 个点来创建一个 600k 点的动画。动画完美无缺,除了我不知道如何在动画中包含我的颜色图(Heatintensity)。 Xs 和 Ys 正在变化,但点的颜色只是蓝色。
import numpy as np
import matplotlib.pyplot as plt
Heatintensity=workdata[0:600000] #Values controlling scatter colormap
Xs=xCoord[0:600000]
Ys=yCoord[0:600000]
plt.ion()
fig, ax = plt.subplots()
sc = ax.scatter(Xs, Ys, c=Heatintensity, cmap=cm.jet, s=5)
plt.draw()
for i in range(20):
sc.set_offsets(np.c_[Xs[(i*30000):(i*30000)+30000],\
Ys[(i*30000):(i*30000)+30000]])
fig.canvas.draw_idle()
plt.pause(0.1)
要更改颜色,您需要使用
sc.set_array(Heatintensity[(i*30000):(i*30000)+30000])
除了更改偏移量。
为了让颜色在每个动画步骤中代表相同的数值,必须将散点归一化为所有数据,
norm = plt.Normalize(Heatintensity.min(), Heatintensity.max())
完整示例:
import numpy as np
import matplotlib.pyplot as plt
Heatintensity=np.random.rand(600000) #Values controlling scatter colormap
Xs=np.random.rand(600000)
Ys=np.random.rand(600000)
plt.ion()
fig, ax = plt.subplots()
norm = plt.Normalize(Heatintensity.min(), Heatintensity.max())
sc = ax.scatter(Xs, Ys, c=Heatintensity, s=5, cmap=plt.cm.jet, norm=norm)
plt.draw()
for i in range(20):
# set coordinates
sc.set_offsets(np.c_[Xs[(i*30000):(i*30000)+30000],\
Ys[(i*30000):(i*30000)+30000]])
# set colors
sc.set_array(Heatintensity[(i*30000):(i*30000)+30000])
# draw and make pause
plt.pause(0.1)
plt.ioff()
plt.show()
同样可以使用 FuncAnimation
:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
Heatintensity=np.random.rand(600000) #Values controlling scatter colormap
Xs=np.random.rand(600000)
Ys=np.random.rand(600000)
fig, ax = plt.subplots()
norm = plt.Normalize(Heatintensity.min(), Heatintensity.max())
sc = ax.scatter(Xs, Ys, c=Heatintensity, s=5, cmap=plt.cm.jet, norm=norm)
def update(i):
# set coordinates
sc.set_offsets(np.c_[Xs[(i*30000):(i*30000)+30000],\
Ys[(i*30000):(i*30000)+30000]])
# set colors
sc.set_array(Heatintensity[(i*30000):(i*30000)+30000])
ani = animation.FuncAnimation(fig, update, frames=range(20), interval=100)
plt.show()