如何在python(matplotlib)中用3d绘制表面内的点?

时间:2016-09-24 20:03:17

标签: python matplotlib plot 3d

我可以在matplotlib中绘制3d中的曲面,但我还需要绘制一条线和曲面上的一个点。线条很细的表面,但由于某种原因,该点不会出现在表面上。这是代码:

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

fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.arange(-2.0, 2.0, 0.05)
Y = np.arange(-1.0, 3.0, 0.05)
X, Y = np.meshgrid(X, Y)
Z = (np.ones([np.shape(X)[0],np.shape(X)[1]])-X)**2+100*(Y-(X)**2)**2
Gx, Gy = np.gradient(Z) # gradients with respect to x and y
G = (Gx**2+Gy**2)**.5  # gradient magnitude
N = G/G.max()  # normalize 0..1
surf = ax.plot_surface(
    X, Y, Z, rstride=1, cstride=1,
    facecolors=cm.jet(N),
    linewidth=0, 
    antialiased=False, 
    shade=False)

plt.hold(True)
ax.hold(True)

# add the unit circle

x_1 = np.arange(-1.0, 1.0, 0.005)
x_2 = np.arange(-1.0, 1.0, 0.005)

y_1 = np.sqrt(np.ones(len(x_1)) - x_1**2)
y_2 = -np.sqrt(np.ones(len(x_2)) - x_2**2)

x = np.array(x_1.tolist() + x_2.tolist())
y = np.array(y_1.tolist() + y_2.tolist())

z = (np.ones(len(x))-x)**2+100*(y-(x)**2)**2

ax.plot(x, y, z, '-k')

plt.hold(True)
ax.hold(True)

ax.scatter(np.array([0.8]),
           np.array([0.6]),
           np.array([0.045]), 
           color='red',
           s=40
        )

# Get current rotation angle
print 'rotation angle is ', ax.azim

# Set rotation angle to 60 degrees
ax.view_init(azim=60)

plt.xlabel('x')
plt.ylabel('y')
plt.show()

问题在于表面上没有显示出这一点。现在,当我替换这段代码时:

ax.scatter(np.array([0.8]),
           np.array([0.6]),
           np.array([0.045]), 
           color='red',
           s=40
        )

...使用此代码(即只添加到最后一个值)...

ax.scatter(np.array([0.8]),
           np.array([0.6]),
           np.array([0.045+800]), 
           color='red',
           s=40
        )

......然后就出现了。但是,当我想在表面绘制实际值时,我无法想出它没有出现的原因。有人知道如何解决这个问题吗?

(顺便说一句,我喜欢摆脱我在表面上绘制的单位圆圈中间的奇怪线条。我似乎无法摆脱它。)< / p>

非常有责任!

0 个答案:

没有答案