【发布时间】:2020-06-16 10:07:21
【问题描述】:
我想知道如何创建两个具有两个 2d 图形表面的切片平面。
例如,我创建了一个曲面如下:
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import pyplot as plt
import numpy as np
def f(x1, x2):
return 0.5 * x1 + 0.6 * x2 + 0.2 * x1 * x1 + 0.1 * x1 * x2 + 0.3 * x2 * x2 + 4
x = np.linspace(-3, 3, 100)
y = np.linspace(-3, 3, 100)
xx, yy = np.meshgrid(x,y)
z = f(xx, yy)
# set up the figure
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.set_xlim(-3, 3)
ax.set_ylim(3, -3)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
# plot the figure
ax.plot_surface(xx, yy, z, cmap="spring", alpha = 0.7)
# add the x=y line to the ground plane
ax.plot([-3, 3], [-3, 3], color='grey', linewidth=1, linestyle='dashed')
# add the x=-y line to the ground plane
ax.plot([3, -3], [-3, 3], color='grey', linewidth=1, linestyle='dashed')
ax.plot(x, x, f(x, x), color='dodgerblue')
ax.plot(x, -x, f(x, -x), color='dodgerblue')
plt.show()
上面代码创建的表面是这样的
在此之后,我想添加两个切片平面,即 x = y 和 x = -y 平面。并将两个平面和曲面的切割线绘制成两个不同的二维图形。
例如,曲面的切割线和 x = y 平面的一个 2d 图形将类似于下面红色框中的图形,但没有曲面,只有红色曲线。
【问题讨论】:
标签: python matplotlib plot 3d surface