3D-Stacked 2D histograms

4.1k views Asked by At

I have a bunch of 2D histograms (square 2D numpy arrays) that I want to stack in 3D like so:

Image from Cardenas, Alfredo E., et al. "Unassisted transport of N-acetyl-L-tryptophanamide through membrane: experiment and simulation of kinetics." The Journal of Physical Chemistry B 116.9 (2012): 2739-2750.

(Image from: Cardenas, Alfredo E., et al. "Unassisted transport of N-acetyl-L-tryptophanamide through membrane: experiment and simulation of kinetics." The Journal of Physical Chemistry B 116.9 (2012): 2739-2750.)

Does anyone have any good ideas how to do this? I've already tried the contourf approach from Python plot - stacked image slices, but the result is less than optimal.

2

There are 2 answers

0
Bence On

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));

output of example code

0
AaronJPung On

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()