python - Matplotlib - 同时在 3D 中绘制平面和点

标签 python matplotlib 3d plane

我正在尝试使用 Matplotlib 在 3D 中同时绘制一个平面和一些点。 我没有错误只是点不会出现。 我可以在不同时间绘制一些点和平面,但绝不能同时绘制。 部分代码如下所示:

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

point  = np.array([1, 2, 3])
normal = np.array([1, 1, 2])

point2 = np.array([10, 50, 50])

# a plane is a*x+b*y+c*z+d=0
# [a,b,c] is the normal. Thus, we have to calculate
# d and we're set
d = -point.dot(normal)

# create x,y
xx, yy = np.meshgrid(range(10), range(10))

# calculate corresponding z
z = (-normal[0] * xx - normal[1] * yy - d) * 1. /normal[2]

# plot the surface
plt3d = plt.figure().gca(projection='3d')
plt3d.plot_surface(xx, yy, z, alpha=0.2)


#and i would like to plot this point : 
ax.scatter(point2[0] , point2[1] , point2[2],  color='green')

plt.show()

最佳答案

只是添加到@suever 的回答中,您没有理由不能创建Axes 然后在其上绘制曲面和散点。那就不用ax.hold()了:

# Create the figure
fig = plt.figure()

# Add an axes
ax = fig.add_subplot(111,projection='3d')

# plot the surface
ax.plot_surface(xx, yy, z, alpha=0.2)

# and plot the point 
ax.scatter(point2[0] , point2[1] , point2[2],  color='green')

关于python - Matplotlib - 同时在 3D 中绘制平面和点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36060933/

相关文章:

python - 如何比较持有 numpy.ndarray 的数据类的相等性(bool(a==b) 引发 ValueError)?

javascript - 如何将 python 中的字符串变量转换为 javascript 文字数组?

python - 在python中将列表输出为 "matrix"类型格式

ios - 如何在 scenekit 中创建 3D 网格

iphone - OpenGL ES : Rotating 3d model around itself

javascript - Threejs 基于相机旋转的指南针

python - 如何限制 Django admin 中的选择数量

python - 如何在matplotlib中调整暂停动画的坐标范围?

python - pylab : different behavior in shell and script

python - 在 python 中绘制,在键盘点击时更新