Codex 또는 Claude로 설치 이 Prompt를 복사해 Codex, Claude 또는 다른 어시스턴트에 붙여 넣으면 Skill 페이지를 검토하고 설치를 진행할 수 있습니다.
직접 명령은 검토 Prompt를 거치지 않습니다. 실행하기 전에 소스를 확인하세요.
npx skills add https://github.com/h-lu/statistics-agentic-coding --skill stat-viz명령은 한 줄로 유지됩니다. 복사하기 전에 가로로 스크롤해 전체 내용을 확인하세요.
로컬 사본을 원하시나요? SkillsMP에서 현재 제공할 수 있는 파일을 다운로드하세요.
SKILL.md 표시 중
SOC 직업 분류 기준
| name | stat-viz |
| description | 创建高质量统计图表的指南。包含图表类型选择、常见错误避免、中文字体配置、代码模板。 |
| argument-hint | <week_id> [chart_type] |
| allowed-tools | Read, Write, Edit, Grep, Glob, Bash, WebSearch |
| disable-model-invocation | true |
/stat-viz week_XX # 显示本周应该生成的图表类型
/stat-viz week_XX histogram # 生成特定类型的图表
为 chapters/week_XX/ 创建高质量的统计图表:
examples/NN_chart_xxx.py(可复现)images/xxx.png(嵌入正文)| 你想展示什么 | 推荐图表 | Seaborn 函数 |
|---|---|---|
| 单变量分布 | 直方图 + KDE | sns.histplot(data, kde=True) |
| 分布形状 + 离群点 | 箱线图 | sns.boxplot(x=data) |
| 多组分布对比 | 小提琴图 | sns.violinplot(x=group, y=value) |
| 两组变量关系 | 散点图 | sns.scatterplot(x=x, y=y) |
| 关系 + 趋势线 | 散点图 + 回归 | sns.regplot(x=x, y=y) |
| 多变量关系 | 相关矩阵热图 | sns.heatmap(corr, annot=True) |
| 分类变量分布 | 计数图 | sns.countplot(x=cat) |
| 时间序列趋势 | 折线图 | sns.lineplot(x=time, y=value) |
| 避免 | 原因 | 替代方案 |
|---|---|---|
| 3D 图表 | 透视 distort 数值感知 | 2D 图表 + 分面 |
| 饼图(>5类) | 人眼难以比较角度 | 条形图 |
| 双 Y 轴 | 制造假相关 | 分开两个图 |
| 过多颜色 | 认知过载 | 使用色调渐变或分组 |
参考来源:
# ❌ 错误:截断 Y 轴夸大差异
plt.ylim(95, 100) # 从 95 开始,5% 差距看起来很大
# ✅ 正确:从 0 开始(柱状图必须)
plt.ylim(0, None) # 或不设置,让 matplotlib 自动选择
例外:折线图可以不从 0 开始,但要在图注中说明。
# ❌ 错误:只展示 favorable 的数据
df[df['metric'] > threshold] # 只展示高于阈值的部分
# ✅ 正确:展示完整数据,标注关注区域
plt.axvspan(xmin, xmax, alpha=0.2, color='yellow', label='关注区域')
# ❌ 错误:3D 饼图或柱状图
# (seaborn 不支持 3D,避免用 matplotlib 的 3D)
# ✅ 正确:2D 分面图
g = sns.FacetGrid(df, col='category')
g.map(sns.histplot, 'value')
# ❌ 错误:半径与数值成正比(面积会平方放大)
plt.scatter(x, y, s=size) # s 是面积,不是半径!
# ✅ 正确:面积与数值成正比
plt.scatter(x, y, s=size**2) # 或直接用 s=size,但理解 s 是面积
# ❌ 错误:一张图展示所有东西
sns.scatterplot(data=df, x='a', y='b', hue='c', size='d', style='e')
# ✅ 正确:分面或分图
g = sns.relplot(data=df, x='a', y='b', hue='c', col='e')
import matplotlib.font_manager as fm
# 查找系统中所有包含 "Hei" 或 "CJK" 的字体
fonts = [f.name for f in fm.fontManager.ttflist
if 'Hei' in f.name or 'CJK' in f.name or 'Song' in f.name]
print(sorted(set(fonts)))
import matplotlib.pyplot as plt
import matplotlib as mpl
# 方案 1:SimHei(Windows/macOS 常见)
plt.rcParams['font.sans-serif'] = ['SimHei', 'Arial Unicode MS', 'DejaVu Sans']
plt.rcParams['axes.unicode_minus'] = False # 解决负号显示问题
# 方案 2:Noto Sans CJK(跨平台)
plt.rcParams['font.sans-serif'] = ['Noto Sans CJK SC', 'Noto Sans CJK JP']
plt.rcParams['axes.unicode_minus'] = False
# 方案 3:动态检测
def get_chinese_font():
"""自动检测可用的中文字体"""
chinese_fonts = ['SimHei', 'Noto Sans CJK SC', 'Arial Unicode MS',
'PingFang SC', 'Microsoft YaHei', 'WenQuanYi Micro Hei']
available = [f.name for f in fm.fontManager.ttflist]
for font in chinese_fonts:
if font in available:
return font
return 'DejaVu Sans' # fallback
plt.rcParams['font.sans-serif'] = [get_chinese_font()]
plt.rcParams['axes.unicode_minus'] = False
# 绘制测试图验证中文显示
fig, ax = plt.subplots(figsize=(6, 4))
ax.text(0.5, 0.5, '中文测试:均值、标准差、箱线图',
fontsize=20, ha='center', va='center')
ax.set_title('中文字体验证')
plt.savefig('test_chinese.png')
plt.close()
# 检查图片是否正确显示中文
"""
示例:生成月薪分布直方图(展示右偏分布)。
运行方式:python3 chapters/week_XX/examples/03_salary_histogram.py
预期输出:生成 images/salary_distribution.png
本代码演示:
1. 中文字体配置
2. 直方图 + KDE
3. 均值/中位数标注
4. 高质量导出设置
"""
from __future__ import annotations
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
import numpy as np
import pandas as pd
from pathlib import Path
def setup_chinese_font() -> str:
"""配置中文字体,返回使用的字体名称"""
chinese_fonts = ['SimHei', 'Noto Sans CJK SC', 'Arial Unicode MS',
'PingFang SC', 'Microsoft YaHei']
available = [f.name for f in fm.fontManager.ttflist]
for font in chinese_fonts:
if font in available:
plt.rcParams['font.sans-serif'] = [font]
plt.rcParams['axes.unicode_minus'] = False
return font
# Fallback
plt.rcParams['font.sans-serif'] = ['DejaVu Sans']
return 'DejaVu Sans'
def generate_sample_data() -> pd.DataFrame:
"""生成示例数据(右偏的月薪分布)"""
np.random.seed(42)
# 基础薪资 + 右偏噪声
base = np.random.lognormal(mean=10.5, sigma=0.3, size=)
high_earners = np.random.uniform(, , size=)
salaries = np.concatenate([base, high_earners])
pd.DataFrame({: salaries})
() -> :
font = setup_chinese_font()
()
df = generate_sample_data()
mean_salary = df[].mean()
median_salary = df[].median()
fig, ax = plt.subplots(figsize=(, ))
ax.hist(df[], bins=, density=, alpha=,
color=, edgecolor=, label=)
scipy stats
kde = stats.gaussian_kde(df[])
x_range = np.linspace(df[].(), df[].(), )
ax.plot(x_range, kde(x_range), , linewidth=, label=)
ax.axvline(mean_salary, color=, linestyle=, linewidth=,
label=)
ax.axvline(median_salary, color=, linestyle=, linewidth=,
label=)
ax.set_xlabel(, fontsize=)
ax.set_ylabel(, fontsize=)
ax.set_title(, fontsize=)
ax.legend(loc=, fontsize=)
output_dir = Path(__file__).parent.parent /
output_dir.mkdir(exist_ok=)
output_path = output_dir /
plt.savefig(output_path, dpi=, bbox_inches=,
facecolor=, edgecolor=)
plt.close()
()
()
()
__name__ == :
main()
| 阶段 | 周次 | 推荐图表 |
|---|---|---|
| 数据探索 | 01-04 | 直方图、箱线图、散点图、相关矩阵 |
| 统计推断 | 05-08 | Q-Q图、置信区间图、效应量森林图 |
| 预测建模 | 09-12 | 残差图、ROC曲线、学习曲线、特征重要性 |
| 高级专题 | 13-15 | DAG因果图、后验分布、SHAP图 |
当调用此 skill 时:
chapters/week_XX/CHAPTER.md 确定需要什么图表images/ 目录examples/NN_chart_xxx.pyimages/xxx.pngvalidate_week.py 仍然通过