statspai.plots¶
plots ¶
Visualization module for StatsPAI.
Provides publication-quality academic plots: - binscatter: Binned scatter plots with residualization (Cattaneo et al. 2024) - coefplot: Coefficient comparison forest plots - event_study_plot: DID event study (via CausalResult) - rdplot: RD visualization (via rd module) - marginsplot: Marginal effects (via postestimation) - interactive: Interactive plot editor with data protection
counterfactual_data ¶
Normalise a fitted result into a tidy counterfactual frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
object
|
A StatsPAI result carrying an observed-vs-counterfactual time series:
a |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Columns |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Examples:
counterfactual_plot ¶
counterfactual_plot(result: Any, *, ax: Any = None, figsize: Sequence[float] = (10.0, 7.0), bands: bool = True, show_effect: bool = True, title: Optional[str] = None) -> Any
Plot observed vs counterfactual with an uncertainty band.
The figure has a trajectory panel (observed solid, counterfactual dashed, shaded band, intervention marker) and, by default, a lower panel of the pointwise treatment effect with a zero reference line.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
object
|
Any result supported by :func: |
required |
ax
|
Axes
|
Draw the trajectory panel into this axis instead of a new figure. When given, the effect panel is suppressed (single-axis mode). |
None
|
figsize
|
sequence of float
|
Figure size when a new figure is created. |
(10, 7)
|
bands
|
bool
|
Shade the counterfactual / effect uncertainty bands when available. |
True
|
show_effect
|
bool
|
Add the lower pointwise-effect panel. |
True
|
title
|
str
|
Figure title. Defaults to the result's method. |
None
|
Returns:
| Type | Description |
|---|---|
Figure
|
The figure (or the parent figure of |
Examples:
set_theme ¶
Set global matplotlib theme for publication-quality plots.
Supports three theme sources:
- StatsPAI:
'academic','aea','minimal','cn_journal' - matplotlib:
'ggplot','fivethirtyeight','bmh','dark_background','grayscale','classic', etc. - seaborn:
'seaborn-whitegrid','seaborn-darkgrid','seaborn-ticks','seaborn-paper','seaborn-talk', etc.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Theme name. Use |
'academic'
|
palette
|
str
|
Color palette name. Defaults to matching the theme. Only applies to StatsPAI themes. |
None
|
font_scale
|
float
|
Scale factor for all font sizes. |
1.0
|
Examples:
>>> import statspai as sp
>>> sp.set_theme('academic') # clean serif, no top/right spines
>>> sp.set_theme('aea') # AER journal specifications
>>> sp.set_theme('ggplot') # R ggplot2 style
>>> sp.set_theme('seaborn-whitegrid') # seaborn white grid
>>> sp.set_theme('fivethirtyeight') # FiveThirtyEight journalism style
>>> sp.set_theme('dark_background') # dark theme for slides
>>> sp.set_theme('default') # reset to matplotlib defaults
list_themes ¶
use_chinese ¶
One-line fix for Chinese text rendering in matplotlib.
Call this before creating any plots. Automatically detects the best Chinese font on your system (macOS, Windows, Linux).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
style
|
str
|
|
'auto'
|
Returns:
| Type | Description |
|---|---|
str
|
The font name that was configured. |
Examples: