【问题标题】:How to plot a slicing plane with a surface with “matplotlib” in python如何在 python 中使用“matplotlib”绘制具有曲面的切片平面
【发布时间】: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


    【解决方案1】:

    实际上,只需将它们绘制成两个二维图形

    # slicing figure 1
    fig2, ax2 = plt.subplots()
    ax2.plot(x, f(x, y, b1, b2, b3, b4, b5, con))
    ax2.set_title("x=y plane")
    ax2.set_xlim(-3, 3)
    ax2.set_ylim(1, 15)
    
    # slicing figure 2
    fig3, ax3 = plt.subplots()
    ax3.plot(x, f(x, -y, b1, b2, b3, b4, b5, con))
    ax3.set_title("x=-y plane")
    ax3.set_xlim(-3, 3)
    ax3.set_ylim(1, 15)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-21
      • 1970-01-01
      相关资源
      最近更新 更多