簡體   English   中英

如何在python中用“matplotlib”繪制帶有表面的切片平面

[英]How to plot a slicing plane with a surface with “matplotlib” in python

我想知道如何創建兩個帶有兩個二維圖形表面的切片平面。

例如,我創建了一個曲面,如下所示:

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 平面的切割線的一個二維圖形將類似於下面紅色框中的圖形,但沒有曲面,只有紅色曲線。

二維圖

實際上,只需要將它們繪制成兩個二維圖形

# 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM