简体   繁体   English

如何在python的交互式绘图动画中设置颜色图

[英]How to set a colormap in interactive plot animations in python

The code below creates an animation of 600k points by scatter plotting 30k of them per frame.下面的代码通过每帧散点图绘制 30k 个点来创建 600k 个点的动画。 The animation works flawlessly, except for the fact that I don't know how to include my colormap (Heatintensity) in the animation.动画完美无缺,除了我不知道如何在动画中包含我的颜色图(热度)。 The Xs and Ys are changing but the color of the points is just blue. 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)

In order to change the colors, you need to use为了改变颜色,你需要使用

sc.set_array(Heatintensity[(i*30000):(i*30000)+30000])

in addition to changing the offsets.除了改变偏移量。

In order for the colors to represent the same numerical values for each animation step, the scatter must be normalized to all data,为了使每个动画步骤的颜色代表相同的数值,必须将散点标准化为所有数据,

norm = plt.Normalize(Heatintensity.min(), Heatintensity.max())

Complete example:完整示例:

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()

The same can be achieved using a FuncAnimation :使用FuncAnimation可以实现相同的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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM