
代码如下:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy import stats
import pandas as pd
import matplotlib
# 1. 设置Matplotlib后端(解决AttributeError)
matplotlib.use('TkAgg') # 或 'Qt5Agg'
# 2. 解决中文乱码(使用系统支持的字体)
try:
# plt.rcParams['font.sans-serif'] = ['Microsoft YaHei'] # Windows
plt.rcParams['font.sans-serif'] = ['Songti SC'] # macOS
except:
plt.rcParams['font.sans-serif'] = ['SimHei'] # 备用方案
plt.rcParams['axes.unicode_minus'] = False
# 设置随机种子
np.random.seed(42)
# 生成三种偏态分布数据
distributions = [
("右偏", np.random.lognormal(mean=0, sigma=0.5, size=1000), 'skyblue'),
("对称", np.random.normal(loc=0, scale=1, size=1000), 'lightgreen'),
("左偏", -np.random.gamma(2, 2, 1000), 'salmon')
]
# 创建3x3子图布局
fig, axes = plt.subplots(3, 3, figsize=(18, 15),
gridspec_kw={'height_ratios': [3, 3, 3]})
# --------------------------
# 遍历每个分布绘制三列图表
# --------------------------
for row_idx, (label, data, color) in enumerate(distributions):
# 当前行的三个子图
ax_hist = axes[row_idx, 0] # 直方图
ax_box = axes[row_idx, 1] # 箱线图
ax_qq = axes[row_idx, 2] # QQ图
# --------------------------
# 1. 直方图(第一列)
# --------------------------
sns.histplot(data, bins=30, kde=True, color=color, ax=ax_hist)
ax_hist.set_title(f'{label}分布直方图', fontsize=12)
ax_hist.set_xlabel('数值')
ax_hist.set_ylabel('频数')
# --------------------------
# 2. 箱线图(第二列)
# --------------------------
df = pd.DataFrame({'数值': data, '分布类型': [label] * len(data)})
sns.boxplot(
x='数值',
y='分布类型',
data=df,
color=color,
orient='h',
width=0.6,
ax=ax_box
)
ax_box.set_title(f'{label}分布箱线图', fontsize=12)
ax_box.set_xlabel('数值')
ax_box.set_ylabel('')
ax_box.set_yticklabels([]) # 隐藏y轴标签
# --------------------------
# 3. QQ图(第三列)
# --------------------------
(quantiles, values), (slope, intercept, _) = stats.probplot(data, dist="norm")
ax_qq.scatter(quantiles, values, color=color, s=20, alpha=0.6)
ax_qq.plot(quantiles, slope * quantiles + intercept,
color='red', linestyle='--', linewidth=1)
ax_qq.set_title(f'{label}分布QQ图', fontsize=12)
ax_qq.set_xlabel('理论分位数')
ax_qq.set_ylabel('样本分位数')
# --------------------------
# 全局布局调整
# --------------------------
plt.tight_layout()
plt.subplots_adjust(hspace=0.4, wspace=0.3) # 调整行间距和列间距
fig.suptitle('偏态分布特征可视化 (3x3布局)', y=1.02, fontsize=14)
plt.show()
系统当前共有 481 篇文章