问题描述
我有一堆要以3D形式堆叠的2D直方图(方形2D numpy数组),如下所示:
I have a bunch of 2D histograms (square 2D numpy arrays) that I want to stack in 3D like so:
(图片来自:Cardenas,Alfredo E.等人,"N-乙酰基-L-色氨酸酰胺通过膜的无辅助运输:动力学的实验和模拟." 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.)
有人有什么好主意怎么做吗?我已经从 Python图-堆叠图像切片中尝试了contourf方法,但是结果不尽人意.
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.
谢谢.
推荐答案
我建议您使用Axes3D.plot_surface
绘制平面,并使用facecolor
参数为它们着色,如下所示:
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));
这篇关于matplotlib pyplot中的3D堆叠2D直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!