Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

3D-Stacked 2D histograms in matplotlib pyplot

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.

Thanks.

like image 819
user1873329 Avatar asked Dec 06 '13 16:12

user1873329


1 Answers

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

like image 194
Bence Avatar answered Sep 25 '22 13:09

Bence