绘制线和面以及正确的遮挡

Plotting a line and surface together with correct occlusion

我希望使用 Python 和 matplotlib 在单个图中绘制二维概率分布及其边缘之一。我快到了,但是图中的线总是画在表面前面,而不是被正确遮挡。我该如何解决这个问题?

import numpy as np

import scipy.stats as stats

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d

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

delta = 0.05
f = 0.5

X, Y = np.meshgrid(np.arange(-3.0, 3.0, delta),
                   np.arange(-3.0, 3.0, delta))
xy = np.hstack((X.flatten()[:, None], Y.flatten()[:, None]))

p1 = stats.multivariate_normal.pdf(xy, mean=[1, -1], cov=(np.eye(2) * 0.28 * f))
p2 = stats.multivariate_normal.pdf(xy, mean=[-1, 1], cov=(np.eye(2) * 0.5 * f))

p = 0.3 * p1 + 0.7 * p2
Z = p.reshape(len(X), len(X))

plt.plot(X[0, :], np.zeros(len(X)) + 3, np.sum(Z, 0) * 0.05) # , color='red')

ax.plot_surface(X, Y, Z, alpha=1.0, cmap='jet', linewidth=0.1, rstride=2, cstride=2)

ax.set_xlabel('Object colour')
ax.set_ylabel('Illumination colour')
ax.set_zlabel('Probability density')
ax.set_zlim(min(cont_offset, np.min(Z)), max(np.max(Z), cont_offset))

plt.show()

内置的contour函数至少得到了正确的z-order;如果你不想要完整的东西,你可以用计算出来的 Z 作弊。首先,将您对 plt.plot 的调用替换为:

from matplotlib import cm
cset = ax.contour(X, Y, Z, zdir='y', offset=3, cmap='binary')
cset = ax.contour(X, Y, Z, zdir='x', offset=-3, cmap='Blues')

为轮廓伪造 Z,一种方法:

from matplotlib import cm
Zys = np.zeros_like(Z)
Zys[60,:] = Z.max(0)
cset = ax.contour(X, Y, Zys, zdir='y', offset=3, cmap='binary')
Zys = np.zeros_like(Z)
Zys[:,60] = Z.max(1)
cset = ax.contour(X, Y, Zys, zdir='x', offset=-3, cmap='Blues')

更雄心勃勃的是,在 contour 代码的某处,他们正在计算 z 顺序...