use*_*754 15 python 3d matplotlib 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()
Run Code Online (Sandbox Code Playgroud)
Sue*_*ver 12
您需要告诉轴你想要新的图添加到轴上的当前图而不是覆盖它们.为此,您需要使用axes.hold(True)
# plot the surface
plt3d = plt.figure().gca(projection='3d')
plt3d.plot_surface(xx, yy, z, alpha=0.2)
# Ensure that the next plot doesn't overwrite the first plot
ax = plt.gca()
ax.hold(True)
ax.scatter(points2[0], point2[1], point2[2], color='green')
Run Code Online (Sandbox Code Playgroud)
UPDATE
正如@tcaswell在评论中指出的那样,他们正在考虑停止支持hold
.因此,更好的方法可能是直接使用轴来添加更多的绘图,如@ tom的答案.
tmd*_*son 12
只是为了添加@ 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')
Run Code Online (Sandbox Code Playgroud)
San*_*Dey 11
只是详细说明一下数学部分(及其工作原理),可能对某人有用,包含点a的单位法向量n的平面方程如下所示:
因此这里平面的方程是 x + y + 2*z = 9 并且可以简单地使用以下代码来绘制给定的平面:
# create the figure
fig = plt.figure()
# add axes
ax = fig.add_subplot(111,projection='3d')
xx, yy = np.meshgrid(range(10), range(10))
z = (9 - xx - yy) / 2
# plot the plane
ax.plot_surface(xx, yy, z, alpha=0.5)
plt.show()
Run Code Online (Sandbox Code Playgroud)
绘制点很简单scatter()