Metadata-Version: 2.5
Name: chartaudit
Version: 0.1.0
Summary: Catch misleading charts and misleading statistics: Tufte's lie factor and data-ink ratio for matplotlib, plus Anscombe-style checks on your data.
Project-URL: Homepage, https://github.com/AnmolAdhikari/chartaudit
Project-URL: Issues, https://github.com/AnmolAdhikari/chartaudit/issues
Project-URL: Changelog, https://github.com/AnmolAdhikari/chartaudit/blob/main/CHANGELOG.md
Author-email: Anmol Adhikari <anmoladhikari03@gmail.com>
License-Expression: MIT
License-File: LICENSE
Keywords: anscombe,chart-linter,data-ink-ratio,data-literacy,data-visualization,lie-factor,matplotlib,statistics,tufte
Classifier: Development Status :: 4 - Beta
Classifier: Framework :: Matplotlib
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Education
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Education
Classifier: Topic :: Scientific/Engineering :: Visualization
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: matplotlib>=3.7
Requires-Dist: numpy>=1.23
Provides-Extra: dev
Requires-Dist: build; extra == 'dev'
Requires-Dist: pytest>=7; extra == 'dev'
Requires-Dist: ruff; extra == 'dev'
Requires-Dist: twine; extra == 'dev'
Description-Content-Type: text/markdown

# chartaudit

**Catch misleading charts and misleading statistics before your audience does.**

`chartaudit` turns three classic ideas from statistics and visualization research into automatic checks:

- **Tufte's lie factor:** does the chart exaggerate the difference in the data?
- **Tufte's data-ink ratio:** how much of the ink actually shows data?
- **Anscombe's quartet:** does your correlation or regression line hide a curve, an outlier, or a single point doing all the work?

```bash
pip install chartaudit
```

![A truncated bar chart with lie factor 32.7 next to an honest version with lie factor 1.0](https://raw.githubusercontent.com/AnmolAdhikari/chartaudit/main/docs/lie_factor_example.svg)

## Check a chart

```python
import matplotlib.pyplot as plt
import chartaudit

fig, ax = plt.subplots()
ax.bar(["Us", "Them"], [98, 102])
ax.set_ylim(95, 104)
ax.set_title("Customer satisfaction")

chartaudit.check(fig)
```

```
ChartReport
------------------------------------------------------------
axes            1
lie_factor      32.667
data_ink_ratio  0.955

Issues
  [ERROR] lie_factor: 'Customer satisfaction': lie factor 32.67. The largest bar looks 2.33x the smallest but the data ratio is 1.04x (102 vs 98) because the y-axis starts at 95, not 0.
  [INFO ] missing_axis_label: 'Customer satisfaction': no x or y axis label.
  [INFO ] data_ink_ratio: 96% of the ink shows data.
```

A 4% difference drawn as a 133% difference: the chart exaggerates the effect **32.7 times**. Tufte's tolerance is 0.95 to 1.05.

## Check your data (Anscombe's lesson, automated)

All four datasets in Anscombe's quartet share the same means, variances, correlation (r = 0.816) and regression line, yet they look completely different. `check_data` finds out which one you have:

```python
x, y = chartaudit.load_anscombe()["III"]
chartaudit.check_data(x, y)
```

```
DataReport
------------------------------------------------------------
n             11
mean_x        9.000
mean_y        7.500
sd_x          3.317
sd_y          2.030
pearson_r     0.816
spearman_rho  0.991
slope         0.500
intercept     3.002
r2_linear     0.666
r2_quadratic  0.685

Issues
  [WARN ] influential_outlier: removing the point (x=13, y=12.74) changes Pearson's r from 0.816 to 1.000. One point is shaping the summary.
```

| Anscombe dataset | What `check_data` reports |
|---|---|
| I | no issues: the summary is fair |
| II | `nonlinear`: a curve fits far better (R² 0.67 → 1.00) |
| III | `influential_outlier`: one point moves r from 0.816 to 1.000 |
| IV | `high_leverage`: the whole correlation comes from the point at x = 19 |

`check(fig)` runs these data checks automatically on every scatter plot in your figure.

## What gets checked

| Code | Severity | Meaning |
|---|---|---|
| `lie_factor` | error / warning | bar lengths exaggerate or shrink differences (truncated or clipped value axis) |
| `data_ink_ratio` | info / warning | share of ink that shows data, with concrete tips to raise it |
| `dual_axis` | warning | two y-axes on one plot (`twinx`) invite comparisons across unrelated scales |
| `three_d` | warning | 3D projection distorts lengths and angles |
| `pie_slices` | warning | a pie with more than 5 slices |
| `rainbow_colormap` | warning | `jet`, `rainbow` and similar colormaps create false boundaries |
| `missing_axis_label` | info | an axis with data but no label |
| `nonlinear` | warning | a straight-line summary of a curved relationship |
| `influential_outlier` | warning | one point changes Pearson's r by more than 0.1 |
| `high_leverage` | warning | the regression line is forced through a single point |

## Use it in tests or CI

```python
def test_report_charts_are_honest():
    fig = make_quarterly_chart()
    chartaudit.check(fig).raise_for_issues()            # fails on errors
    chartaudit.check(fig).raise_for_issues(strict=True) # fails on warnings too
```

Reports also render as a table in Jupyter and export with `report.to_dict()`.

## How it works

- **Lie factor** is `visual effect / data effect` (Tufte, 1983), where an effect is the relative difference between the smallest and largest bar. The visual size of each bar is computed from the axis limits, so truncated and clipped axes are measured exactly. Stacked, mixed-sign and log-scale bars are skipped, because the formula does not apply to them.
- **Data-ink ratio** renders the figure twice, once as is and once with only the data marks, and compares the ink (each pixel's colour distance from the background). Axes, ticks, gridlines, labels, legends, titles, colorbars and coloured backgrounds count as non-data ink.
- **Anscombe checks** compare linear and quadratic fits, compute leave-one-out correlations (in O(n)) and regression leverage, using NumPy only.

## References

- Anscombe, F. J. (1973). Graphs in Statistical Analysis. *The American Statistician*, 27(1), 17–21.
- Tufte, E. R. (1983). *The Visual Display of Quantitative Information*. Graphics Press.

## Development

```bash
pip install -e ".[dev]"
pytest
```

## License

MIT
