![1792073523830]()
![image]()
import matplotlib.pyplot as plt
import numpy as np
# 设置字体
plt.rcParams['font.sans-serif'] = ['SimHei', 'Arial Unicode MS']
plt.rcParams['axes.unicode_minus'] = False
fig = plt.figure(figsize=(10, 8), dpi=150)
ax = fig.add_subplot(111, projection='3d')
ax.set_axis_off()
# 定义不同性质曲线的颜色
color_surface = '#1f77b4' # 曲面轮廓线:蓝色
color_axis = 'black' # 坐标轴:黑色
color_boundary = '#d62728' # 边界线 (y=a 与斜线):红色
color_shadow = '#7f7f7f' # 阴影区域:灰色
color_intersect = '#6a0dad' # 曲面截交线:深紫色
color_edge = '#2a5a8a' # 曲面网格线:深蓝
color_cut1 = '#d62728' # 切面1(y=a):红色
color_cut2 = '#ff8c00' # 切面2(斜线):橙色
# ================= 1. 设定视角 =================
ax.view_init(elev=25, azim=-45)
# ================= 2. 绘制三维正态分布曲面 =================
peak_z = 4.2
sigma = 2.2
# 2.1 生成曲面网格
x_surf = np.linspace(-6, 6, 100)
y_surf = np.linspace(-6, 6, 100)
X_surf, Y_surf = np.meshgrid(x_surf, y_surf)
Z_surf = peak_z * np.exp(-(X_surf**2 + Y_surf**2) / (2 * sigma**2))
ax.plot_surface(X_surf, Y_surf, Z_surf,
color=color_surface,
alpha=0.35,
edgecolor=color_edge,
linewidth=0.3,
rstride=2, cstride=2,
antialiased=True)
# 2.2 水平等高线
theta = np.linspace(0, 2*np.pi, 120)
levels = [0.3, 0.8, 1.5, 2.3, 3.1, 3.8]
for level in levels:
r = sigma * np.sqrt(-2 * np.log(level / peak_z))
x_c = r * np.cos(theta)
y_c = r * np.sin(theta)
z_c = np.full_like(x_c, level)
ax.plot(x_c, y_c, z_c, color=color_edge, lw=0.8, alpha=0.5)
# ================= 3. 绘制两个切面(只在 x ≥ 0 一侧,高度压低) =================
a_val = -4.0
slope = 0.15
# 切面高度上限:压低到 1.5(原来到 peak_z=4.2)
cut_height = 1.5
# 3.1 切面1:垂直平面 y = a,只在 x ≥ 0 一侧
x_cut1 = np.linspace(0, 6, 40)
z_cut1 = np.linspace(0, cut_height, 40) # ← 高度压低
X_cut1, Z_cut1 = np.meshgrid(x_cut1, z_cut1)
Y_cut1 = np.full_like(X_cut1, a_val)
ax.plot_surface(X_cut1, Y_cut1, Z_cut1,
color=color_cut1, alpha=0.18,
edgecolor=color_cut1, linewidth=0.4,
linestyle='--')
# 3.2 切面2:垂直斜面(沿斜线方向),只在 x ≥ 0 一侧
x_cut2 = np.linspace(0, 6, 40)
z_cut2 = np.linspace(0, cut_height, 40) # ← 高度压低
X_cut2, Z_cut2 = np.meshgrid(x_cut2, z_cut2)
Y_cut2 = a_val + slope * X_cut2
ax.plot_surface(X_cut2, Y_cut2, Z_cut2,
color=color_cut2, alpha=0.18,
edgecolor=color_cut2, linewidth=0.4,
linestyle='--')
# ================= 4. 绘制坐标轴 =================
ax.quiver(0, 0, 0, 0, 0, 6.5, color=color_axis, lw=1.5, arrow_length_ratio=0.1)
ax.text(0, 0, 6.8, r"$p(y, \dot{y})$", fontsize=16, fontfamily='serif', ha='center')
ax.quiver(0, 0, 0, 0, -8.0, 0, color=color_axis, lw=1, arrow_length_ratio=0.1)
ax.plot([0, 0], [0, 8.0], [0, 0], color=color_axis, lw=1)
ax.text(0, -8.5, 0, r"$y$", fontsize=16, fontfamily='serif')
ax.quiver(0, 0, 0, 8.0, 0, 0, color=color_axis, lw=1, arrow_length_ratio=0.1)
ax.plot([-8.0, 0], [0, 0], [0, 0], color=color_axis, lw=1)
ax.text(8.5, 0, 0, r"$\dot{y}$", fontsize=16, fontfamily='serif')
# ================= 5. 绘制底面的两条边界线与阴影 =================
x_line_a = np.linspace(0, 6.0, 50)
y_line_a = np.full_like(x_line_a, a_val)
z_line_a = np.zeros_like(x_line_a)
ax.plot(x_line_a, y_line_a, z_line_a, color=color_boundary, lw=2.5)
y_line_slope = a_val + slope * x_line_a
ax.plot(x_line_a, y_line_slope, z_line_a, color=color_boundary, lw=2.5, ls='--')
for x_val in np.linspace(0, 4.0, 30):
y_start = a_val
y_end = a_val + slope * x_val
ax.plot([x_val, x_val], [y_start, y_end], [0.01, 0.01],
color=color_shadow, lw=1)
# ================= 6. 绘制曲面上的截交线 =================
x_cross_a = np.linspace(0, 6.0, 100)
y_cross_a = np.full_like(x_cross_a, a_val)
z_cross_a = peak_z * np.exp(-(x_cross_a**2 + a_val**2) / (2 * sigma**2))
ax.plot(x_cross_a, y_cross_a, z_cross_a, color=color_intersect, lw=2.5)
ax.plot([0, 0], [a_val, a_val], [0, z_cross_a[0]],
color=color_intersect, lw=2.5)
ax.plot([6, 6], [a_val, a_val], [z_cross_a[-1], 0],
color=color_intersect, lw=2.5)
x_cross_slope = np.linspace(0, 6.0, 100)
y_cross_slope = a_val + slope * x_cross_slope
z_cross_slope = peak_z * np.exp(-(x_cross_slope**2 + y_cross_slope**2) / (2 * sigma**2))
ax.plot(x_cross_slope, y_cross_slope, z_cross_slope,
color=color_intersect, lw=2.5, ls='--')
ax.plot([0, 0], [a_val, a_val], [0, z_cross_slope[0]],
color=color_intersect, lw=2.5, ls='--')
ax.plot([6, 6], [y_cross_slope[-1], y_cross_slope[-1]],
[z_cross_slope[-1], 0], color=color_intersect, lw=2.5, ls='--')
# ================= 7. 标注 =================
ax.scatter(0, a_val, 0, color=color_boundary, s=25)
ax.text(0, a_val - 0.5, 0, r"$a$", fontsize=16, fontfamily='serif')
ax.text(4, a_val - 1.5, 0, r"$y=a$", fontsize=14, fontfamily='serif')
ax.text(0.5, a_val + 0.4, 0, r"$\alpha$", fontsize=16, fontfamily='serif')
ax.text2D(0.05, 0.9, r"$(a)$", transform=ax.transAxes,
fontsize=18, fontfamily='serif')
ax.set_xlim([-6.0, 6.0])
ax.set_ylim([-6.0, 6.0])
ax.set_zlim([0, 6.5])
plt.tight_layout()
plt.show()