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

时间:2015-04-18 00:15:38

标签: python matplotlib surface

我希望使用Python和matplotlib在一个图中用一个边缘绘制2D概率分布。我几乎就在那里,但是情节中的线总是被绘制在表面前面,而不是被恰当地遮挡。我该如何解决这个问题?

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()

1 个答案:

答案 0 :(得分:2)

内置的contour函数至少可以使z顺序正确;如果你不想要完整的东西,你可以用计算的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')

enter image description here

用一种方式假装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')

enter image description here 更加雄心勃勃,在contour代码的某处,他们正在计算z​​顺序...