sn曲线三维图形

image


import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# 中文、负号显示
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

# ===================== 基础参数 =====================
np.random.seed(123)
group_num = 5        # 5个应力等级
point_num = 5        # 每个应力5个试验样本

# 标准正态分位数 z 值
z10 = -1.282
z50 = 0.0
z90 = 1.282

# Y轴:应力等级 S
s_levels = np.array([1, 2, 3, 4, 5])
# 对数正态分布参数 (ln(N) ~ N(mu, sigma²))
mu_list = np.array([2.1, 1.8, 1.5, 1.1, 0.7])
sigma_list = np.array([0.22, 0.20, 0.18, 0.16, 0.14])
colors = ['red', 'green', 'blue', 'orange', 'purple']

# 存储理论分位寿命值
n_10 = []
n_50 = []
n_90 = []

# ===================== 创建3D画布 =====================
fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(111, projection='3d')

for i in range(group_num):
    s = s_levels[i]
    mu = mu_list[i]
    sigma = sigma_list[i]
    c = colors[i]

    # 1. 计算对数正态理论分位数
    q10 = np.exp(mu + z10 * sigma)
    q50 = np.exp(mu + z50 * sigma)
    q90 = np.exp(mu + z90 * sigma)
    n_10.append(q10)
    n_50.append(q50)
    n_90.append(q90)

    # 2. 生成对数正态试验样本点 (Z=0 平面)
    n_points = np.random.lognormal(mean=mu, sigma=sigma, size=point_num)
    s_points = np.full_like(n_points, s)
    z_points = np.zeros_like(n_points)
    ax.scatter(n_points, s_points, z_points, c=c, s=120, alpha=0.7, label=f'应力等级 {i+1}')

    # 3. 绘制对数正态概率密度曲线(取合理范围,保留少量尾部)
    x_left = np.exp(mu - 2.5 * sigma)
    x_right = np.exp(mu + 2.5 * sigma)
    n_curve = np.linspace(x_left, x_right, 200)
    s_curve = np.full_like(n_curve, s)

    # 对数正态概率密度公式
    pdf = (1 / (n_curve * sigma * np.sqrt(2 * np.pi))) * \
          np.exp(-(np.log(n_curve) - mu) ** 2 / (2 * sigma ** 2))
    z_curve = pdf

    ax.plot(n_curve, s_curve, z_curve, c=c, linewidth=3, alpha=0.6)

# ===================== 绘制分位点平滑连线(SN平面 Z=0) =====================
ax.plot(n_10, s_levels, np.zeros_like(s_levels),
        color='black', linestyle='--', linewidth=2, label='10% 分位曲线')
ax.plot(n_50, s_levels, np.zeros_like(s_levels),
        color='black', linestyle='-', linewidth=3, label='50% 中位数曲线')
ax.plot(n_90, s_levels, np.zeros_like(s_levels),
        color='black', linestyle='--', linewidth=2, label='90% 分位曲线')

# ===================== 坐标轴与标题 =====================
ax.set_xlabel('寿命 N', fontsize=12)
ax.set_ylabel('应力 S', fontsize=12)
ax.set_zlabel('概率密度', fontsize=12)
ax.set_title('SN三维分布 | 对数正态分布 + 理论分位平滑曲线', fontsize=14)

ax.legend(loc='upper left')
plt.tight_layout()
plt.show()
```pyhton
posted @ 2026-06-10 00:01  redufa  阅读(4)  评论(0)    收藏  举报