0

The code below creates an animation of 600k points by scatter plotting 30k of them per frame. 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.

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)
1
  • I tried your code and it works for random data that I created. Are you sure that whatever is in Heatintensity is of the right dtype and of different values? Commented Nov 15, 2017 at 9:33

1 Answer 1

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:

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()
Sign up to request clarification or add additional context in comments.

1 Comment

Wow thank you for going above and beyond with the extra info about FuncAnimation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.