I have a multidimensional array (P x N x M) and I want to plot each N x M array in a 3D plot in such a way the P images are stacked along the z-axis.
Do you know how to do this in Python?
I have a multidimensional array (P x N x M) and I want to plot each N x M array in a 3D plot in such a way the P images are stacked along the z-axis.
Do you know how to do this in Python?
You can achieve this by using Matplotlib's Axes3D module. This code will generate a 3D scatter plot where each 2D slice from the P x N x M array is stacked along the z-axis at a different height (controlled by the z variable). The color of each point in the scatter plot represents the values in the corresponding slice, and a colorbar is added to indicate the data values.
import numpy as np
import matplotlib.pyplot as plt
# Example multidimensional array of shape (P, N, M)
# Replace this with your actual data
P, N, M = 5, 10, 10
data = np.random.rand(P, N, M)
# Create a 3D scatter plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Create a meshgrid for the x and y values
x, y = np.meshgrid(range(N), range(M))
for p in range(P):
# Flatten the 2D slice and stack it at the height of p
z = np.full((N, M), p)
ax.scatter(x, y, z, c=data[p].ravel(), cmap='viridis')
# Set labels for each axis
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
# Customize the colorbar
norm = plt.Normalize(data.min(), data.max())
sm = plt.cm.ScalarMappable(cmap='viridis', norm=norm)
sm.set_array([])
fig.colorbar(sm, label='Data Values')
plt.show()
If you want N x M arrays as "heatmaps" stacked above each other along the z-axis this is one way to do it:
Result: