在Matplotlib立方体的面上的Contourf

Chr*_*ris 4 python 3d matplotlib

我正在尝试使用Python Matplotlib使用contourf函数"绘制"多维数据集的面.这可能吗?

这与此处所做的类似,但显然我不能使用补丁.同样,我认为我不能像这样使用add_collection3d ,因为它只支持PolyCollection,LineColleciton和PatchCollection.

我一直在尝试使用contourf fig.gca(projection='3d').下面的玩具示例.

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

plt.close('all')
fig = plt.figure()
ax = fig.gca(projection='3d')

############################################
#  plotting the 'top' layer works okay...  #
############################################

X = np.linspace(-5, 5, 43)
Y = np.linspace(-5, 5, 28)
X, Y = np.meshgrid(X, Y)

varone=np.random.rand(75,28,43)
Z=varone[0,:,:]
cset = ax.contourf(X, Y, Z, zdir='z', offset=1,
                levels=np.linspace(np.min(Z),np.max(Z),30),cmap='jet')
#see [1]
plt.show()

#################################################
#  but now trying to plot a vertical slice....  #
#################################################

plt.close('all')
fig = plt.figure()
ax = fig.gca(projection='3d')

Z=varone[::-1,:,-1]
X = np.linspace(-5, 5, 28)
Y = np.linspace(-5, 5, 75)
X, Y = np.meshgrid(X, Y)

#this 'projection' doesn't result in what I want, I really just want to rotate it
cset = ax.contourf(X, Y, Z, offset=5,zdir='x',
                levels=np.linspace(np.min(Z),np.max(Z),30),cmap='jet')

#here's what it should look like....
ax=fig.add_subplot(1, 2,1)
cs1=ax.contourf(X,Y,Z,levels=np.linspace(np.min(Z),np.max(Z),30),cmap='jet')
#see [2]    
plt.show()
Run Code Online (Sandbox Code Playgroud)

1从示例中,顶部表面很容易:
情节示例#1

2但我不确定如何做到这两面.该图的左侧是该部分的外观(但是旋转)......
示例图#2

打开其他python方法.我实际绘制的数据是地球物理网络文件.

Sch*_*sch 5

您必须将数据分配给右轴.曲折的结果是,现在你处于x = const并且在 - z方向上有振荡(来自随机数据,它是在0和之间生成的1).
如果您在示例中为矩阵分配不同的结果,则最终得到所需的结果:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

plt.close('all')
fig = plt.figure()
ax = fig.gca(projection='3d')

X = np.linspace(-5, 5, 43)
Y = np.linspace(-5, 5, 28)
X, Y = np.meshgrid(X, Y)

varone=np.random.rand(75,28,43) * 5.0 - 10.0
Z=varone[0,:,:]

cset = [[],[],[]]

# this is the example that worked for you:
cset[0] = ax.contourf(X, Y, Z, zdir='z', offset=5,
                      levels=np.linspace(np.min(Z),np.max(Z),30),cmap='jet')

# now, for the x-constant face, assign the contour to the x-plot-variable:
cset[1] = ax.contourf(Z, Y, X, zdir='x', offset=5,
                      levels=np.linspace(np.min(Z),np.max(Z),30),cmap='jet')

# likewise, for the y-constant face, assign the contour to the y-plot-variable:
cset[2] = ax.contourf(X, Z, Y, zdir='y', offset=-5,
                      levels=np.linspace(np.min(Z),np.max(Z),30),cmap='jet')

# setting 3D-axis-limits:    
ax.set_xlim3d(-5,5)
ax.set_ylim3d(-5,5)
ax.set_zlim3d(-5,5)

plt.show()
Run Code Online (Sandbox Code Playgroud)

结果如下:

轮廓立方体