I have a bunch of 2D histograms (square 2D numpy arrays) that I want to stack in 3D like so:
I recommend that you use Axes3D.plot_surface
to draw flat surfaces, and use the facecolor
argument to color them, like this:
import numpy;
from matplotlib import pyplot;
from matplotlib import cm;
from mpl_toolkits.mplot3d import Axes3D;
pyplot.interactive(True);
# Creat mesh.
X = numpy.arange(-1, 1, 0.1);
Y = numpy.arange(-1, 1, 0.1);
X, Y = numpy.meshgrid(X, Y);
# Create some data to plot.
A = numpy.copy(X);
B = numpy.copy(Y);
C = numpy.sqrt(X**2 + Y**2);
D = numpy.cos(C);
# Normalize data for colormap use.
A -= numpy.min(A); A /= numpy.max(A);
B -= numpy.min(B); B /= numpy.max(B);
C -= numpy.min(C); C /= numpy.max(C);
D -= numpy.min(D); D /= numpy.max(D);
# Create flat surface.
Z = numpy.zeros_like(X);
# Plot
fig = pyplot.figure();
ax = fig.gca(projection='3d');
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors = cm.coolwarm(A));
ax.plot_surface(X, Y, Z+0.1, rstride=1, cstride=1, facecolors = cm.coolwarm(B));
ax.plot_surface(X, Y, Z+0.2, rstride=1, cstride=1, facecolors = cm.coolwarm(C));
ax.plot_surface(X, Y, Z+0.3, rstride=1, cstride=1, facecolors = cm.coolwarm(D));
To supplement @Bence's answer, the additional vertical colorbar can be added using a snippet of code from here. Specifically, I used the following code, where 'xxx' is my data set, where each layer shown in Bence's plot is a separate z layer in a dstack
'd numpy array. :
m = cm.ScalarMappable(cmap=cm.jet)
m.set_array(xxx)
pyplot.colorbar(m)
pyplot.show()