如何在Python中使用“ matplotlib”绘制具有表面的切片平面 [英] How to plot a slicing plane with a surface with “matplotlib” in python

查看:296
本文介绍了如何在Python中使用“ matplotlib”绘制具有表面的切片平面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何创建两个具有两个2d图形的表面的切片平面。

I wonder how to create two slicing planes with a surface to two 2d figures.

例如,我创建了一个如下所示的表面:

For example, I created a surface as below:

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

以上代码创建的表面看起来像这样

The surface created by the above code looks like this

此后,我想添加两个切片平面,即x = y和x = -y平面。并将两个平面和曲面的切割线绘制为两个不同的2d图形。

After this, I want to add two slicing planes, which are x = y and x = -y planes. And plot the cutting lines of the two planes and the surface to two different 2d figures.

例如,曲面的切割线和x = y平面的一个2d图形将类似于下面红色框中的图形,但没有曲面,

For example, one 2d figure of the cutting line of the surface and the x = y plane would be something like the figure in the red box below, but no surface, just the red curve.

推荐答案

实际上,只需将它们绘制在两个2d图形中

Actually, just need to plot them in two 2d figures

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

这篇关于如何在Python中使用“ matplotlib”绘制具有表面的切片平面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆