inferno
inferno

Reputation: 420

How to plot horizontal stack of heatmaps or a stack of grid?

I want to plot a stack of heatmaps, contour, or grid computed over time. The plot should like this,

stack of grid

I have tried this:

from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.gca(projection='3d')

x = np.linspace(0, 1, 100)

X, Z = np.meshgrid(x, x)
Y = np.sin(X)*np.sin(Z)


levels = np.linspace(-1, 1, 40)

ax.contourf(X, Y, Z, zdir='y')
ax.contourf(X, Y+3, Z, zdir='y')
ax.contourf(X, Y+7, Z, zdir='y')



ax.legend()

ax.view_init(15,155)
plt.show()

For one my plot looks ugly. It also does not look like what I want. I cannot make a grid there, and the 2d surfaces are tilted.

Any help is really appreciated! I am struggling with this.

my result

Related stackoverflow:

[1] Python plot - stacked image slices

[2] Stack of 2D plot

Upvotes: 3

Views: 1423

Answers (1)

CT Zhu
CT Zhu

Reputation: 54330

How about making a series of 3d surface plots, with the data your wish to present in contour plotted as facecolor?

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator
import numpy as np


fig = plt.figure()
ax = fig.gca(projection='3d')

X = np.arange(-5, 5, 0.25)
Z = np.arange(-5, 5, 0.25)
X, Z = np.meshgrid(X, Z)
C = np.random.random(size=40*40*3).reshape((40, 40, 3))
ax.plot_surface(X, np.ones(shape=X.shape)-1, Z, facecolors=C, linewidth=0)
ax.plot_surface(X, np.ones(shape=X.shape), Z, facecolors=C, linewidth=0)
ax.plot_surface(X, np.ones(shape=X.shape)+1, Z, facecolors=C, linewidth=0)

enter image description here

Upvotes: 4

Related Questions