关于 PyStatlas
基于真实社会科学数据、按所回答的问题组织的 Python 统计图表。机器学习(模型预测得多好、为什么这样预测)和空间数据(地图与空间自相关)与分布、组间比较、回归和生存分析并列收录;每张图都给出其背后的统计量——估计值、置信区间、检验和效应量。
PyStatlas 从研究者提出的问题出发,而不是从代码出发。它最初是为社会科学专业的学生编写的:他们熟悉自己的领域,需要用数据回答问题——这些组之间有差异吗?两个变量是否同步变化?模型预测了什么,为什么?每张图都把相关统计量标在图上,并附有一段基于 Python 科学计算生态、简短且自包含的脚本——与 R 中 ggstats 和 ggstatsplot 的理念一致。
设计原则
- 自成一体。每个脚本都可直接运行。数据为 statsmodels 和 lifelines 自带的真实社会科学数据集(选举、学校、犯罪、生育率、政治体制、再犯等)、手工录入的已发表小型表格,或少数情况下明确标注、设定随机种子的模拟。空间数据示例通过 libpysal 下载一次盖里的法国地图,之后使用缓存副本。
- 统计优先。每张图表都标注了关键数字:估计值、区间、检验统计量和效应量。
- 文件夹即页面。代码仓库的目录树与网站结构一一对应。每个示例都位于
gallery/<family>/<chart>/plot.py,并附有渲染好的plot.png和 README。
图表类别
| 类别 | 图表 | 内容概览 |
|---|---|---|
| 机器学习 | 9 | 模型对新数据的预测效果如何?原因何在?样本外评估、学习曲线与模型无关的解释——每一项都附带其不确定性。 |
| 空间数据 | 4 | 邻近地区数值高的地方,本地数值是否也更高?空间权重、全局与局部空间自相关,以及集聚的影响范围——以 1830 年代盖里的法国数据为例。 |
| 分布 | 5 | 看清数据的形态:直方图、密度图、山脊图、ECDF 与 Q–Q 图。 |
| 组间比较 | 5 | 以 ggstatsplot 的方式比较各组,将检验、效应量和 p 值直接标注在图上。 |
| 比例与问卷调查 | 5 | 比例、交叉表与 Likert 量表,延续 ggstats::gglikert 与 stat_prop 的风格。 |
| 相关与关联 | 4 | 相关矩阵、附带统计量的散点图以及变量两两之间的关系。 |
| 回归模型 | 6 | 系数图、优势比、模型比较与边际效应——Python 版的 ggcoef_model。 |
| 模型诊断 | 5 | 残差、杠杆值、校准与 ROC 曲线:先检验,再信任模型。 |
| 生存分析 | 4 | 附风险表的 Kaplan–Meier 曲线、累积风险以及 Cox 模型风险比。 |
| 时间序列 | 4 | 序列分解、自相关、带预测区间的预测以及随时间的变化。 |
| 多元分析与聚类 | 4 | 面向高维数据的 PCA 双标图、碎石图、聚类与热图。 |
| 推断与不确定性 | 6 | 自助法(Bootstrap)、置信区间、统计功效、一致性分析与元分析森林图。 |
作者
PyStatlas 由 Thierry Warin 创建并维护。更多关于数据科学、统计学与人工智能的作品,请访问:www.warin.ca。
如何引用
PyStatlas 由 Thierry Warin 创建并维护。如果它对你的工作有所帮助,敬请引用。
Warin, T. (2026). PyStatlas: A Python Gallery of Statistical Graphics for the Social Sciences. https://warint.github.io/pystatlas/
@misc{warin2026statlas,
author = {Warin, Thierry},
title = {PyStatlas: A Python Gallery of Statistical Graphics for the Social Sciences},
year = {2026},
howpublished = {\url{https://warint.github.io/pystatlas/}},
note = {Source code: https://github.com/warint/pystatlas}
}参与贡献
新建一个包含 plot.py 的文件夹,运行 python build.py,然后提交 pull request。详见 CONTRIBUTING.md。
致谢
PyStatlas 建立在开源软件、开放数据和 R 社区的成果之上。每个示例都使用了以下工作,或受其启发。
示例中使用的 Python 包
NumPy · pandas · Matplotlib · seaborn · SciPy · statsmodels · scikit-learn · lifelines · SHAP · GeoPandas · libpysal · esda
数据集
灵感来源
- The R Graph Gallery (Yan Holtz)
- ggstatsplot (Indrajeet Patil) —
ggbetweenstats(),ggwithinstats(),ggbarstats(),ggcorrmat(),ggscatterstats() - ggstats (Joseph Larmarange) —
gglikert(),stat_prop(),ggtable(),ggcoef_model(),ggcoef_compare() - ggdist (Matthew Kay) and ggrain (Nicholas Judd, Jordy van Langen) —
stat_halfeye(),geom_rain() - ggridges (Claus O. Wilke) —
geom_density_ridges() - survminer and factoextra (Alboukadel Kassambara) —
ggsurvplot(),ggforest(),ggadjustedcurves(),fviz_pca_biplot(),fviz_eig(),fviz_silhouette() - dabestr (Joses W. Ho and colleagues) —
dabest() - performance, easystats (Daniel Lüdecke and colleagues) —
check_model() - metafor (Wolfgang Viechtbauer) —
forest(),funnel(),regtest() - GGally (Barret Schloerke, Di Cook) —
ggpairs() - corrplot (Taiyun Wei, Viliam Simko) —
corrplot() - SHAP (Scott Lundberg and Su-In Lee, 2017) and shapviz (Michael Mayer) —
sv_importance(),sv_dependence(),sv_waterfall() - spdep (Roger Bivand) and GeoDa / rgeoda (Luc Anselin, Xun Li) —
poly2nb(),moran.plot(),moran.mc(),localmoran_perm(),sp.correlogram(),local_moran() - vip and pdp (Brandon Greenwell, Bradley Boehmke) and tidymodels' yardstick —
vip(),partial(),conf_mat(),pr_curve()
示例所对应的 R 包
BlandAltmanLeh · blandr · boot · car · caret · ComplexHeatmap · corrplot · dabestr · factoextra · feasts · forecast · GGally · ggdist · ggeffects · ggplot2 · ggpubr · ggrain · ggridges · ggstats · ggstatsplot · iml · interactions · marginaleffects · meta · metafor · pdp · performance · pheatmap · probably · pROC · pwr · qqplotr · quantreg · rgeoda · rms · shapviz · spdep · survminer · TeachingDemos · tune · vcd · vip · yardstick
网站构建
PyYAML · Pygments · Pillow · GitHub Pages
数据来源
几乎所有示例都使用已发表的数据,按 statsmodels、lifelines 或 R 的 metafor 所分发的形式原样复现;两个模拟示例已明确标注。每个条目给出原始文献、数据进入图库的途径,以及使用这些数据的图表。
- American National Election Study, 1996. Rosenstone, S. J., Kinder, D. R., Miller, W. E., & the National Election Studies. (2016). ANES 1996 Time Series Study (ICPSR 6896; Version 6) [Data set]. Inter-university Consortium for Political and Social Research. https://doi.org/10.3886/ICPSR06896.v6
Subset of 944 respondents distributed with statsmodels (sm.datasets.anes96); public domain.
使用于: 带指标的混淆矩阵, 交互作用图, Tukey HSD 区间, 按组堆叠的比例图, 95% 置信区间的含义 - California STAR test results, 1998 (303 school districts). Gill, J. (2001). Generalized Linear Models: A Unified Approach (Quantitative Applications in the Social Sciences, No. 134). Sage. https://www.statsmodels.org/stable/datasets/generated/star98.html
Original data from the California Department of Education's Standardized Testing and Reporting (STAR) program, 1998. Subset distributed with statsmodels (sm.datasets.star98) with the author's permission.
使用于: 交叉验证模型比较, 置换重要性, 带 ICE 曲线的部分依赖图, SHAP 汇总图(蜂群图), SHAP 依赖图, 单个预测的 SHAP 瀑布图, 组间比较, 附统计量的散点图, 附相关系数的配对图, 带聚类的相关图, 嵌套模型比较, 交互效应, 碎石图 - US state crime and social indicators, 2009. U.S. Census Bureau. Statistical Abstract of the United States, state tables for 2009 (crime rates from the FBI Uniform Crime Reporting program). https://www.statsmodels.org/stable/datasets/generated/statecrime.html
Compiled for statsmodels (sm.datasets.statecrime); public domain. See the statsmodels page for each variable's table and footnotes.
使用于: 估计图, 相关矩阵, 系数图, 残差诊断, 影响图, 添加变量图, PCA 双标图, k-means 轮廓图, 聚类热图, 自助法分布 - Redbook survey of married women, 1974 (Fair's affairs data). Fair, R. C. (1978). A theory of extramarital affairs. Journal of Political Economy, 86(1), 45–61. https://doi.org/10.1086/260646
Data from the 1974 Redbook magazine survey; distributed with statsmodels (sm.datasets.fair) with the author's permission.
使用于: 精确率–召回率曲线, 学习曲线, 带 Pearson 残差的马赛克图, 带置信区间的比例, 带标准化残差的交叉表, 优势比森林图, ROC 曲线, 校准图 - World Bank fertility rates by country, 1960–2013. World Bank. Fertility rate, total (births per woman), indicator SP.DYN.TFRT.IN. World Development Indicators. https://data.worldbank.org/indicator/SP.DYN.TFRT.IN
Primary sources: UN Population Division, UN Statistical Division, national statistical offices, Eurostat and others. Distributed with statsmodels (sm.datasets.fertility) under the World Bank terms of use.
使用于: 带密度曲线的直方图, 山脊图, 被试内比较 - Engel's Belgian household budgets (235 families). Koenker, R., & Bassett, G. (1982). Robust tests for heteroscedasticity based on regression quantiles. Econometrica, 50(1), 43–61. https://doi.org/10.2307/1912528
Engel's (1857) household budget data as used by Koenker and Bassett and distributed with R's quantreg; in statsmodels as sm.datasets.engel; public domain.
使用于: 带置信带的 Q–Q 图, 分位数回归过程 - Spector and Mazzeo's teaching-method experiment (32 students). Spector, L. C., & Mazzeo, M. (1980). Probit analysis and economic education. Journal of Economic Education, 11(2), 37–44. https://www.statsmodels.org/stable/datasets/generated/spector.html
Obtained from W. Greene's Econometric Analysis web site with the original author's permission; distributed with statsmodels (sm.datasets.spector).
使用于: 预测概率 - US quarterly macroeconomic series, 1959–2009. Federal Reserve Bank of St. Louis. FRED, Federal Reserve Economic Data; U.S. Bureau of Labor Statistics (unemployment rate). Accessed December 15, 2009. https://fred.stlouisfed.org/
Compiled by Skipper Seabold for statsmodels (sm.datasets.macrodata); public domain.
使用于: ACF 与 PACF - Euro-area electrical-equipment turnover, monthly, 1995–2016. Eurostat. Euro area (18, fixed composition) total turnover index, manufacture of electrical equipment (NACE Rev. 2), working-day adjusted, not seasonally adjusted. https://ec.europa.eu/eurostat
Distributed with statsmodels (statsmodels.datasets.elec_equip); public domain.
使用于: STL 分解, SARIMA 预测, 季节子序列图 - Democracy and Dictatorship: leader spells, 1946–2008. Cheibub, J. A., Gandhi, J., & Vreeland, J. R. (2010). Democracy and dictatorship revisited. Public Choice, 143(1–2), 67–101. https://doi.org/10.1007/s11127-009-9491-2
202 countries; distributed with lifelines (lifelines.datasets.load_dd).
使用于: 雨云图, Kaplan–Meier 生存曲线, 累积风险 - Rossi recidivism experiment (432 released prisoners). Rossi, P. H., Berk, R. A., & Lenihan, K. J. (1980). Money, Work, and Crime: Some Experimental Results. Academic Press. https://lifelines.readthedocs.io/en/latest/lifelines.datasets.html
Men released from Maryland state prisons in the 1970s, half randomly assigned to financial aid. Distributed with lifelines (lifelines.datasets.load_rossi).
使用于: ECDF 比较, Cox 风险比, 调整后的生存曲线 - Teacher-expectancy experiments (Raudenbush, 1984). Raudenbush, S. W. (1984). Magnitude of teacher expectancy effects on pupil IQ as a function of the credibility of expectancy induction: A synthesis of findings from 18 experiments. Journal of Educational Psychology, 76(1), 85–97. https://doi.org/10.1037/0022-0663.76.1.85
Effect sizes and variances typed in from dat.raudenbush1985 in R's metadat/metafor (Viechtbauer, 2010, Journal of Statistical Software, 36(3), https://doi.org/10.18637/jss.v036.i03); one of the 18 experiments contributes two rows.
使用于: 元分析森林图, 漏斗图 - Simulated data. Simulated data with a fixed random seed, created for this example.
No real respondents, documents or measurements: the numbers only illustrate how the chart and its statistics work.
使用于: 发散式 Likert 图, Bland–Altman 图 - Guerry's moral statistics of France, c. 1830 (85 départements). Guerry, A.-M. (1833). Essai sur la statistique morale de la France. Crochard. Data as compiled in Friendly, M. (2007). A.-M. Guerry's moral statistics of France: Challenges for multivariable spatial analysis. Statistical Science, 22(3), 368–399. https://doi.org/10.1214/07-STS241
Prepared by the Center for Spatial Data Science (University of Chicago) from the R package Guerry (Friendly and Dray), provided “as is”. Fetched once and cached by libpysal (examples.load_example("Guerry")). Variables come from Guerry's tables as translated by Whitt and Reinking (2002).
使用于: 带空间权重的分级统计地图, 莫兰散点图(全局自相关), LISA 聚类地图(局部自相关), 空间相关图
这些示例用于说明统计方法。图表标题描述的是该图在这些特定数据中呈现的内容——这些数据往往样本较小、年代较早或并不完整——而不是作者对实质问题的立场。关于解释、背景和数据质量,请以此处引用的原始来源为准。