matplotlib pyplot中的3D堆积2D直方图

时间:2013-12-06 16:05:26

标签: numpy matplotlib plot

我有一堆2D直方图(方形2D numpy数组),我想像3D那样堆叠:

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.

(图片来自: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。)

有没有人有任何好主意如何做到这一点?我已经尝试过来自Python plot - stacked image slices的contourf方法,但结果并不理想。

感谢。

2 个答案:

答案 0 :(得分:4)

我建议您使用Axes3D.plot_surface绘制平面,并使用facecolor参数为它们着色,如下所示:

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

答案 1 :(得分:0)

为了补充@ Bence的答案,可以使用here的代码片段添加额外的垂直颜色条。具体来说,我使用了以下代码,其中' xxx'是我的数据集,其中Bence的图中显示的每个图层都是from pylab import * import matplotlib.animation as animation from mpl_toolkits.mplot3d import Axes3D fig = figure() ax = Axes3D(fig,azim=-40,elev=30) sphere=Bloch(axes=ax) def animate(i): sphere.clear() sphere.add_vectors([sin(theta),0,cos(theta)]) sphere.add_points([sx[:i+1],sy[:i+1],sz[:i+1]]) sphere.make_sphere() return ax def init(): sphere.vector_color = ['r'] return ax ani = animation.FuncAnimation(fig, animate, np.arange(len(sx)), init_func=init, blit=True, repeat=False) ani.save('bloch_sphere.mp4', fps=20, clear_temp=True) &n; dumpy数组中的单独z图层。 :

dstack