diff_diff.plot_bacon#
- diff_diff.plot_bacon(results, *, plot_type='scatter', figsize=(10, 6), title=None, xlabel='2x2 DiD Estimate', ylabel='Weight', colors=None, marker='o', markersize=80, alpha=0.7, show_weighted_avg=True, show_twfe_line=True, ax=None, show=True, backend='matplotlib')[source]
Visualize Goodman-Bacon decomposition results.
Creates either a scatter plot showing the weight and estimate for each 2x2 comparison, or a stacked bar chart showing total weight by comparison type.
- Parameters:
results (BaconDecompositionResults) – Results from BaconDecomposition.fit() or bacon_decompose().
plot_type (str, default="scatter") – Type of plot to create: - “scatter”: Scatter plot with estimates on x-axis, weights on y-axis - “bar”: Stacked bar chart of weights by comparison type
figsize (tuple, default=(10, 6)) – Figure size (width, height) in inches.
title (str, optional) – Plot title. If None, uses a default based on plot_type.
xlabel (str, default="2x2 DiD Estimate") – X-axis label (scatter plot only).
ylabel (str, default="Weight") – Y-axis label.
colors (dict, optional) – Dictionary mapping comparison types to colors. Keys are: “treated_vs_never”, “earlier_vs_later”, “later_vs_earlier”. If None, uses default colors.
marker (str, default="o") – Marker style for scatter plot.
markersize (int, default=80) – Marker size for scatter plot.
alpha (float, default=0.7) – Transparency for markers/bars.
show_weighted_avg (bool, default=True) – Whether to show weighted average lines for each comparison type (scatter plot only).
show_twfe_line (bool, default=True) – Whether to show a vertical line at the TWFE estimate (scatter plot only).
ax (matplotlib.axes.Axes, optional) – Axes to plot on. If None, creates new figure.
show (bool, default=True) – Whether to call plt.show() at the end.
backend (str, default="matplotlib") – Plotting backend:
"matplotlib"or"plotly".
- Returns:
The axes object (matplotlib) or figure (plotly).
- Return type:
matplotlib.axes.Axes or plotly.graph_objects.Figure
Examples
Scatter plot (default):
>>> from diff_diff import bacon_decompose, plot_bacon >>> results = bacon_decompose(data, outcome='y', unit='id', ... time='t', first_treat='first_treat') >>> plot_bacon(results)
Bar chart of weights by type:
>>> plot_bacon(results, plot_type='bar')
Notes
The scatter plot is particularly useful for understanding:
Distribution of estimates: Are 2x2 estimates clustered or spread? Wide spread suggests heterogeneous treatment effects.
Weight concentration: Do a few comparisons dominate the TWFE? Points with high weights have more influence.
Forbidden comparison problem: Red points (later_vs_earlier) show comparisons using already-treated units as controls. If these have different estimates than clean comparisons, TWFE may be biased.
See also
bacon_decomposePerform the decomposition
BaconDecompositionClass-based interface