4

I have spent some considerable amount of time reading through a lot of posts on how to generate an animated scatter graph and how to color code them. Ideally, I want to have the scatter plot of X,Y,Z coordinate and each would have a size according to their corresponding M value and a color according to their corresponding t value. The color code must be fixed from the beginning according to the maximum and minimum time of vector t, and the points appear with the color associated with their time value (i.e. t vector). The last part of the post (the code at the end) is capable to generate a scatter plot with different sizes according to M but only one color.

More details: About the dataset, I have the X,Y,Z coordinate of each point and each event point are assigned a value (M) and each happened at a specific time (t). I have the size of each point to be proportional to their value (i.e., M), now I want to add the color to each point so that it also shows the time of occurrence.

Solution (1) I tried: I know I have to use .set_color(c) but c value expects a tuple value. I tried to normalize the values of the time to map the color from this post. However, there is something that I miss because the code is not working to color the points with related time. I used the following code

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.animation as animation
from IPython.display import HTML # Animation on jupyter lab 
from matplotlib.animation import PillowWriter # For GIF animation 
#####Data Generation####

# Space Coordinate
X = np.random.random((100,)) * 255 * 2 - 255
Y = np.random.random((100,)) * 255 * 2 - 255
Z = np.random.random((100,)) * 255 * 2 - 255

# Magnitude of each point
# M = np.random.random((100,))*-1+0.5
M = np.random.randint(1,70, size=100)
# Time
t = np.sort(np.random.random((100,))*10)



x = []
y = []
z = []
m = []
cmap = plt.cm.Spectral
norm = plt.Normalize(vmin=min(t), vmax=max(t))
# plt.scatter(x,y, c = cmap(norm(t)))
def update_lines(i):
#     for i in range (df_IS["EASTING [m]"].size):
    dx = X[i]
    dy = Y[i]
    dz = Z[i]
    dm = M[i]
#     text.set_text("{:d}: [{:.0f}] Mw[{:.2f}]".format(ID[i], t[i],ID[i]))  # for debugging
    x.append(dx) 
    y.append(dy) 
    z.append(dz)
    m.append(dm)
    graph._offsets3d = (x, y, z)
    graph.set_sizes(m)
    return graph,

fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(111, projection="3d")
graph = ax.scatter(X, Y, Z, s=M, c = cmap(norm(t)))  # s argument here 
text = fig.text(0, 1, "TEXT", va='top')  # for debugging

ax.set_xlim3d(X.min(), X.max())
ax.set_ylim3d(Y.min(), Y.max())
ax.set_zlim3d(Z.min(), Z.max())

# Creating the Animation object
ani = animation.FuncAnimation(fig, update_lines, frames=30, interval=500, blit=False, repeat=False)
# plt.show()
ani.save('test3Dscatter.gif', writer='pillow')
plt.close()
HTML(ani.to_html5_video())

Solution (2) I tried: I also looked into this post, based on this post I added:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.animation as animation
from IPython.display import HTML # Animation on jupyter lab 
from matplotlib.animation import PillowWriter # For GIF animation 
#####Data Generation####
X = np.random.random((100,)) * 255 * 2 - 255
Y = np.random.random((100,)) * 255 * 2 - 255
Z = np.random.random((100,)) * 255 * 2 - 255

# Magnitude of each point
# M = np.random.random((100,))*-1+0.5
M = np.random.randint(1,70, size=100)
# Time
t = np.sort(np.random.random((100,))*10)


x = []
y = []
z = []
m = []
t = []
###=====edited part=====####
def get_colour(t): 
    '''
    with this function I tried to generate a new color for each point and then use the graph.set- 
    color to plot it but it is not successful
    '''
    cmap = matplotlib.cm.get_cmap('Spectral');
    return cmap(t);

def update_lines(i):
#     for i in range (df_IS["EASTING [m]"].size):
    dx = X[i]
    dy = Y[i]
    dz = Z[i]
    dm = M[i]
    dt = t[i]
    #     text.set_text("{:d}: [{:.0f}] Mw[{:.2f}]".format(ID[i], t[i],ID[i]))  # for debugging
    x.append(dx) 
    y.append(dy) 
    z.append(dz)
    m.append(dm)
    t.append(dt)
    graph._offsets3d = (x, y, z)
    graph.set_sizes(m)
    graph.set_color(c=get_colour(t))
    return graph,

I would appreciate it if someone could share their experiences, and see how I could add the color bar and the color mapped points to the graph? Here is the code which is generating the scatter plot in 3D with different size but uniform color.

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.animation as animation
from IPython.display import HTML # Animation on jupyter lab 
from matplotlib.animation import PillowWriter # For GIF animation 
#####Data Generation####

# Space Coordinate
X = np.random.random((100,)) * 255 * 2 - 255
Y = np.random.random((100,)) * 255 * 2 - 255
Z = np.random.random((100,)) * 255 * 2 - 255

# Magnitude of each point
# M = np.random.random((100,))*-1+0.5
M = np.random.randint(1,70, size=100)
# Time
t = np.sort(np.random.random((100,))*10)

#ID each point should be color coded. Moreover, each point belongs to a cluster `ID`
ID = np.sort(np.round([np.random.random((100,))*5]))

x = []
y = []
z = []
m = []

def update_lines(i):
#     for i in range (df_IS["EASTING [m]"].size):
    dx = X[i]
    dy = Y[i]
    dz = Z[i]
    dm = M[i]
#     text.set_text("{:d}: [{:.0f}] Mw[{:.2f}]".format(ID[i], t[i],ID[i]))  # for debugging
    x.append(dx) 
    y.append(dy) 
    z.append(dz)
    m.append(dm)
    graph._offsets3d = (x, y, z)
    graph.set_sizes(m)
    return graph,

fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(111, projection="3d")
graph = ax.scatter(X, Y, Z, s=M, color='orange')  # s argument here 
text = fig.text(0, 1, "TEXT", va='top')  # for debugging

ax.set_xlim3d(X.min(), X.max())
ax.set_ylim3d(Y.min(), Y.max())
ax.set_zlim3d(Z.min(), Z.max())

# Creating the Animation object
ani = animation.FuncAnimation(fig, update_lines, frames=30, interval=500, blit=False, repeat=False)
# plt.show()
ani.save('test3Dscatter.gif', writer='pillow')
plt.close()
HTML(ani.to_html5_video())
pygineer
  • 41
  • 3

1 Answers1

0
frame    = ax.contourf(curVals, vmax=vmax, vmin=vmin, levels=levels)
cbar     = fig.colorbar(frame) 

For more Details refer this link : How to animate the colorbar in matplotlib