python绘制同切源代码,如何在Python中使用“ matplotlib”绘制具有表面的切片平面...

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

mnw19.png

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.

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.

RHCEL.png

解决方案

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值