Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to plot a multidimensional array in a 3D plot?

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 hw to do this in Python?

Thanks in advance

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you want N x M arrays as "heatmaps" stacked above each other along the z-axis this is one way to do it:

import numpy as np
import matplotlib.pyplot as plt

# Generate some dummy arrays
P, N, M = 5, 10, 10
data = np.random.rand(P, N, M)

# Create a 3D figure
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Create meshgrid for x, y values
x, y = np.meshgrid(np.arange(M), np.arange(N))

# Plot each N x M array as a heatmap at different heights along the z-axis
for p in range(P):
    heatmap = data[p]
    ax.plot_surface(x, y, np.full_like(heatmap, p), facecolors=plt.cm.viridis(heatmap), rstride=1, cstride=1, antialiased=True, shade=False)

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('P')
ax.set_title('Stacked Heatmaps')
plt.show()

Result:

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading