djrhails-graphs 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- djrhails_graphs-0.1.0/LICENSE +21 -0
- djrhails_graphs-0.1.0/PKG-INFO +91 -0
- djrhails_graphs-0.1.0/README.md +77 -0
- djrhails_graphs-0.1.0/djrhails_graphs.egg-info/PKG-INFO +91 -0
- djrhails_graphs-0.1.0/djrhails_graphs.egg-info/SOURCES.txt +21 -0
- djrhails_graphs-0.1.0/djrhails_graphs.egg-info/dependency_links.txt +1 -0
- djrhails_graphs-0.1.0/djrhails_graphs.egg-info/requires.txt +2 -0
- djrhails_graphs-0.1.0/djrhails_graphs.egg-info/top_level.txt +3 -0
- djrhails_graphs-0.1.0/examples/bar_chart.py +38 -0
- djrhails_graphs-0.1.0/examples/dumbbell_chart.py +72 -0
- djrhails_graphs-0.1.0/examples/faceted_chart.py +86 -0
- djrhails_graphs-0.1.0/examples/line_chart.py +50 -0
- djrhails_graphs-0.1.0/graphs/__init__.py +52 -0
- djrhails_graphs-0.1.0/graphs/_charts.py +114 -0
- djrhails_graphs-0.1.0/graphs/_finalize.py +254 -0
- djrhails_graphs-0.1.0/graphs/_fonts.py +66 -0
- djrhails_graphs-0.1.0/graphs/_labels.py +197 -0
- djrhails_graphs-0.1.0/graphs/_legend.py +145 -0
- djrhails_graphs-0.1.0/graphs/_palette.py +22 -0
- djrhails_graphs-0.1.0/graphs/_theme.py +70 -0
- djrhails_graphs-0.1.0/pyproject.toml +44 -0
- djrhails_graphs-0.1.0/requirements.txt +2 -0
- djrhails_graphs-0.1.0/setup.cfg +4 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Daniel Hails
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: djrhails-graphs
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Economist-style chart theme for matplotlib/seaborn
|
|
5
|
+
Author-email: Daniel Hails <graphs@hails.info>
|
|
6
|
+
Project-URL: Homepage, https://github.com/DJRHails/graphs
|
|
7
|
+
Project-URL: Bug Tracker, https://github.com/DJRHails/graphs/issues
|
|
8
|
+
Requires-Python: >=3.10
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Requires-Dist: matplotlib>=3.7
|
|
12
|
+
Requires-Dist: numpy>=1.24
|
|
13
|
+
Dynamic: license-file
|
|
14
|
+
|
|
15
|
+
# graphs
|
|
16
|
+
|
|
17
|
+
Economist-style chart theme for matplotlib and seaborn — global theme, title-stack
|
|
18
|
+
finaliser, direct line labels, CI bands, horizontal bars, and dumbbell charts.
|
|
19
|
+
Uses IBM Plex Sans typography and a curated 8-colour palette.
|
|
20
|
+
|
|
21
|
+
Version: 0.1.0
|
|
22
|
+
|
|
23
|
+
## Install
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pip install djrhails-graphs
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
PyPI distribution is `djrhails-graphs` because the bare name `graphs` is taken.
|
|
30
|
+
The import package is `graphs`.
|
|
31
|
+
|
|
32
|
+
## Quick start
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
import matplotlib.pyplot as plt
|
|
36
|
+
from graphs import ci_fill, finalize, label_lines, set_theme
|
|
37
|
+
|
|
38
|
+
set_theme()
|
|
39
|
+
|
|
40
|
+
fig, ax = plt.subplots()
|
|
41
|
+
fig.subplots_adjust(top=0.68, bottom=0.14, left=0.06, right=0.88)
|
|
42
|
+
|
|
43
|
+
ax.plot(x, y, label="Series A")
|
|
44
|
+
label_lines(ax)
|
|
45
|
+
finalize(
|
|
46
|
+
ax,
|
|
47
|
+
title="Bold chart title",
|
|
48
|
+
descriptor="Country, metric, unit",
|
|
49
|
+
source="Source: Organisation",
|
|
50
|
+
)
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
See `examples/` for runnable scripts:
|
|
54
|
+
|
|
55
|
+
- `line_chart.py` — multi-series line with CI bands + direct labels
|
|
56
|
+
- `faceted_chart.py` — three-panel faceted layout with panel labels
|
|
57
|
+
- `bar_chart.py` — horizontal bar with max-value highlight
|
|
58
|
+
- `dumbbell_chart.py` — before/after comparison with legend
|
|
59
|
+
|
|
60
|
+
## Public API
|
|
61
|
+
|
|
62
|
+
| Function | Purpose |
|
|
63
|
+
|----------|---------|
|
|
64
|
+
| `set_theme()` | Apply global rcParams (figure, axes, ticks, fonts, palette) |
|
|
65
|
+
| `finalize(ax, title, descriptor, source, *, y_axis_right, title_x, y_start, autoscale_y)` | Title stack, red rule, source line, y-axis tidy-up |
|
|
66
|
+
| `label_lines(ax, ...)` | Direct end-of-line labels with collision avoidance |
|
|
67
|
+
| `smart_legend(ax, ...)` | Pick the emptiest corner of the axes |
|
|
68
|
+
| `ci_fill(ax, x, y_lo, y_hi, *, color)` | Confidence-interval band |
|
|
69
|
+
| `bar_h(ax, categories, values, *, highlight_max)` | Horizontal bar chart |
|
|
70
|
+
| `dumbbell(ax, categories, start, end, ...)` | Dot-and-line before/after chart |
|
|
71
|
+
| `panel_label(ax, label)` | Bold panel sub-heading for facets |
|
|
72
|
+
|
|
73
|
+
### Palette
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
from graphs import colors, C_BG, C_RED, C_SPINE, C_GRID, C_LABEL, C_TEXT, C_CI
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Sequence `colors` (red primary, blue, teal, green, yellow, mauve, slate, coral).
|
|
80
|
+
|
|
81
|
+
### Typography
|
|
82
|
+
|
|
83
|
+
IBM Plex Sans is loaded automatically. If already registered in matplotlib's
|
|
84
|
+
font manager, no download occurs. Otherwise TTFs are fetched from
|
|
85
|
+
`github.com/IBM/plex` on first use and cached inside the installed package.
|
|
86
|
+
|
|
87
|
+
Fallback chain: IBM Plex Sans → Verdana → Arial → DejaVu Sans.
|
|
88
|
+
|
|
89
|
+
## License
|
|
90
|
+
|
|
91
|
+
MIT. See [LICENSE](./LICENSE).
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# graphs
|
|
2
|
+
|
|
3
|
+
Economist-style chart theme for matplotlib and seaborn — global theme, title-stack
|
|
4
|
+
finaliser, direct line labels, CI bands, horizontal bars, and dumbbell charts.
|
|
5
|
+
Uses IBM Plex Sans typography and a curated 8-colour palette.
|
|
6
|
+
|
|
7
|
+
Version: 0.1.0
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
pip install djrhails-graphs
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
PyPI distribution is `djrhails-graphs` because the bare name `graphs` is taken.
|
|
16
|
+
The import package is `graphs`.
|
|
17
|
+
|
|
18
|
+
## Quick start
|
|
19
|
+
|
|
20
|
+
```python
|
|
21
|
+
import matplotlib.pyplot as plt
|
|
22
|
+
from graphs import ci_fill, finalize, label_lines, set_theme
|
|
23
|
+
|
|
24
|
+
set_theme()
|
|
25
|
+
|
|
26
|
+
fig, ax = plt.subplots()
|
|
27
|
+
fig.subplots_adjust(top=0.68, bottom=0.14, left=0.06, right=0.88)
|
|
28
|
+
|
|
29
|
+
ax.plot(x, y, label="Series A")
|
|
30
|
+
label_lines(ax)
|
|
31
|
+
finalize(
|
|
32
|
+
ax,
|
|
33
|
+
title="Bold chart title",
|
|
34
|
+
descriptor="Country, metric, unit",
|
|
35
|
+
source="Source: Organisation",
|
|
36
|
+
)
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
See `examples/` for runnable scripts:
|
|
40
|
+
|
|
41
|
+
- `line_chart.py` — multi-series line with CI bands + direct labels
|
|
42
|
+
- `faceted_chart.py` — three-panel faceted layout with panel labels
|
|
43
|
+
- `bar_chart.py` — horizontal bar with max-value highlight
|
|
44
|
+
- `dumbbell_chart.py` — before/after comparison with legend
|
|
45
|
+
|
|
46
|
+
## Public API
|
|
47
|
+
|
|
48
|
+
| Function | Purpose |
|
|
49
|
+
|----------|---------|
|
|
50
|
+
| `set_theme()` | Apply global rcParams (figure, axes, ticks, fonts, palette) |
|
|
51
|
+
| `finalize(ax, title, descriptor, source, *, y_axis_right, title_x, y_start, autoscale_y)` | Title stack, red rule, source line, y-axis tidy-up |
|
|
52
|
+
| `label_lines(ax, ...)` | Direct end-of-line labels with collision avoidance |
|
|
53
|
+
| `smart_legend(ax, ...)` | Pick the emptiest corner of the axes |
|
|
54
|
+
| `ci_fill(ax, x, y_lo, y_hi, *, color)` | Confidence-interval band |
|
|
55
|
+
| `bar_h(ax, categories, values, *, highlight_max)` | Horizontal bar chart |
|
|
56
|
+
| `dumbbell(ax, categories, start, end, ...)` | Dot-and-line before/after chart |
|
|
57
|
+
| `panel_label(ax, label)` | Bold panel sub-heading for facets |
|
|
58
|
+
|
|
59
|
+
### Palette
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
from graphs import colors, C_BG, C_RED, C_SPINE, C_GRID, C_LABEL, C_TEXT, C_CI
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Sequence `colors` (red primary, blue, teal, green, yellow, mauve, slate, coral).
|
|
66
|
+
|
|
67
|
+
### Typography
|
|
68
|
+
|
|
69
|
+
IBM Plex Sans is loaded automatically. If already registered in matplotlib's
|
|
70
|
+
font manager, no download occurs. Otherwise TTFs are fetched from
|
|
71
|
+
`github.com/IBM/plex` on first use and cached inside the installed package.
|
|
72
|
+
|
|
73
|
+
Fallback chain: IBM Plex Sans → Verdana → Arial → DejaVu Sans.
|
|
74
|
+
|
|
75
|
+
## License
|
|
76
|
+
|
|
77
|
+
MIT. See [LICENSE](./LICENSE).
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: djrhails-graphs
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Economist-style chart theme for matplotlib/seaborn
|
|
5
|
+
Author-email: Daniel Hails <graphs@hails.info>
|
|
6
|
+
Project-URL: Homepage, https://github.com/DJRHails/graphs
|
|
7
|
+
Project-URL: Bug Tracker, https://github.com/DJRHails/graphs/issues
|
|
8
|
+
Requires-Python: >=3.10
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Requires-Dist: matplotlib>=3.7
|
|
12
|
+
Requires-Dist: numpy>=1.24
|
|
13
|
+
Dynamic: license-file
|
|
14
|
+
|
|
15
|
+
# graphs
|
|
16
|
+
|
|
17
|
+
Economist-style chart theme for matplotlib and seaborn — global theme, title-stack
|
|
18
|
+
finaliser, direct line labels, CI bands, horizontal bars, and dumbbell charts.
|
|
19
|
+
Uses IBM Plex Sans typography and a curated 8-colour palette.
|
|
20
|
+
|
|
21
|
+
Version: 0.1.0
|
|
22
|
+
|
|
23
|
+
## Install
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pip install djrhails-graphs
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
PyPI distribution is `djrhails-graphs` because the bare name `graphs` is taken.
|
|
30
|
+
The import package is `graphs`.
|
|
31
|
+
|
|
32
|
+
## Quick start
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
import matplotlib.pyplot as plt
|
|
36
|
+
from graphs import ci_fill, finalize, label_lines, set_theme
|
|
37
|
+
|
|
38
|
+
set_theme()
|
|
39
|
+
|
|
40
|
+
fig, ax = plt.subplots()
|
|
41
|
+
fig.subplots_adjust(top=0.68, bottom=0.14, left=0.06, right=0.88)
|
|
42
|
+
|
|
43
|
+
ax.plot(x, y, label="Series A")
|
|
44
|
+
label_lines(ax)
|
|
45
|
+
finalize(
|
|
46
|
+
ax,
|
|
47
|
+
title="Bold chart title",
|
|
48
|
+
descriptor="Country, metric, unit",
|
|
49
|
+
source="Source: Organisation",
|
|
50
|
+
)
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
See `examples/` for runnable scripts:
|
|
54
|
+
|
|
55
|
+
- `line_chart.py` — multi-series line with CI bands + direct labels
|
|
56
|
+
- `faceted_chart.py` — three-panel faceted layout with panel labels
|
|
57
|
+
- `bar_chart.py` — horizontal bar with max-value highlight
|
|
58
|
+
- `dumbbell_chart.py` — before/after comparison with legend
|
|
59
|
+
|
|
60
|
+
## Public API
|
|
61
|
+
|
|
62
|
+
| Function | Purpose |
|
|
63
|
+
|----------|---------|
|
|
64
|
+
| `set_theme()` | Apply global rcParams (figure, axes, ticks, fonts, palette) |
|
|
65
|
+
| `finalize(ax, title, descriptor, source, *, y_axis_right, title_x, y_start, autoscale_y)` | Title stack, red rule, source line, y-axis tidy-up |
|
|
66
|
+
| `label_lines(ax, ...)` | Direct end-of-line labels with collision avoidance |
|
|
67
|
+
| `smart_legend(ax, ...)` | Pick the emptiest corner of the axes |
|
|
68
|
+
| `ci_fill(ax, x, y_lo, y_hi, *, color)` | Confidence-interval band |
|
|
69
|
+
| `bar_h(ax, categories, values, *, highlight_max)` | Horizontal bar chart |
|
|
70
|
+
| `dumbbell(ax, categories, start, end, ...)` | Dot-and-line before/after chart |
|
|
71
|
+
| `panel_label(ax, label)` | Bold panel sub-heading for facets |
|
|
72
|
+
|
|
73
|
+
### Palette
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
from graphs import colors, C_BG, C_RED, C_SPINE, C_GRID, C_LABEL, C_TEXT, C_CI
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Sequence `colors` (red primary, blue, teal, green, yellow, mauve, slate, coral).
|
|
80
|
+
|
|
81
|
+
### Typography
|
|
82
|
+
|
|
83
|
+
IBM Plex Sans is loaded automatically. If already registered in matplotlib's
|
|
84
|
+
font manager, no download occurs. Otherwise TTFs are fetched from
|
|
85
|
+
`github.com/IBM/plex` on first use and cached inside the installed package.
|
|
86
|
+
|
|
87
|
+
Fallback chain: IBM Plex Sans → Verdana → Arial → DejaVu Sans.
|
|
88
|
+
|
|
89
|
+
## License
|
|
90
|
+
|
|
91
|
+
MIT. See [LICENSE](./LICENSE).
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
requirements.txt
|
|
5
|
+
djrhails_graphs.egg-info/PKG-INFO
|
|
6
|
+
djrhails_graphs.egg-info/SOURCES.txt
|
|
7
|
+
djrhails_graphs.egg-info/dependency_links.txt
|
|
8
|
+
djrhails_graphs.egg-info/requires.txt
|
|
9
|
+
djrhails_graphs.egg-info/top_level.txt
|
|
10
|
+
examples/bar_chart.py
|
|
11
|
+
examples/dumbbell_chart.py
|
|
12
|
+
examples/faceted_chart.py
|
|
13
|
+
examples/line_chart.py
|
|
14
|
+
graphs/__init__.py
|
|
15
|
+
graphs/_charts.py
|
|
16
|
+
graphs/_finalize.py
|
|
17
|
+
graphs/_fonts.py
|
|
18
|
+
graphs/_labels.py
|
|
19
|
+
graphs/_legend.py
|
|
20
|
+
graphs/_palette.py
|
|
21
|
+
graphs/_theme.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# /// script
|
|
2
|
+
# requires-python = ">=3.12"
|
|
3
|
+
# dependencies = ["matplotlib"]
|
|
4
|
+
# ///
|
|
5
|
+
"""Horizontal bar chart — The Economist style."""
|
|
6
|
+
|
|
7
|
+
import sys
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
|
11
|
+
|
|
12
|
+
import matplotlib.pyplot as plt
|
|
13
|
+
|
|
14
|
+
from graphs import bar_h, finalize, set_theme
|
|
15
|
+
|
|
16
|
+
set_theme()
|
|
17
|
+
|
|
18
|
+
categories = ["Germany", "France", "Italy", "Spain", "Poland", "Sweden"]
|
|
19
|
+
values = [3.7, 2.4, 1.8, 2.1, 4.2, 3.1]
|
|
20
|
+
|
|
21
|
+
fig, ax = plt.subplots(figsize=(7, 4))
|
|
22
|
+
fig.subplots_adjust(top=0.62, bottom=0.10, left=0.14, right=0.90)
|
|
23
|
+
|
|
24
|
+
bar_h(ax, categories, values)
|
|
25
|
+
|
|
26
|
+
finalize(
|
|
27
|
+
ax,
|
|
28
|
+
title="Eastern promise",
|
|
29
|
+
descriptor="GDP growth rate, %, 2023",
|
|
30
|
+
source="Source: Eurostat",
|
|
31
|
+
y_axis_right=False,
|
|
32
|
+
title_x=0.02,
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
out = Path(__file__).resolve().parent / "economist_bar.png"
|
|
36
|
+
plt.savefig(out, bbox_inches="tight", dpi=150)
|
|
37
|
+
plt.close()
|
|
38
|
+
print("Saved bar chart")
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# /// script
|
|
2
|
+
# requires-python = ">=3.12"
|
|
3
|
+
# dependencies = ["matplotlib"]
|
|
4
|
+
# ///
|
|
5
|
+
"""Dumbbell chart — The Economist style."""
|
|
6
|
+
|
|
7
|
+
import sys
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
|
11
|
+
|
|
12
|
+
import matplotlib.pyplot as plt
|
|
13
|
+
import matplotlib.ticker as ticker
|
|
14
|
+
|
|
15
|
+
from graphs import dumbbell, finalize, set_theme
|
|
16
|
+
|
|
17
|
+
set_theme()
|
|
18
|
+
|
|
19
|
+
countries = [
|
|
20
|
+
"United States",
|
|
21
|
+
"China",
|
|
22
|
+
"Germany",
|
|
23
|
+
"Japan",
|
|
24
|
+
"India",
|
|
25
|
+
"Brazil",
|
|
26
|
+
]
|
|
27
|
+
gdp_2000 = [10.25, 1.21, 1.95, 4.72, 0.47, 0.65]
|
|
28
|
+
gdp_2020 = [20.93, 14.72, 3.84, 5.06, 2.62, 1.44]
|
|
29
|
+
|
|
30
|
+
fig, ax = plt.subplots(figsize=(8, 4.5))
|
|
31
|
+
fig.subplots_adjust(top=0.62, bottom=0.12, left=0.18, right=0.88)
|
|
32
|
+
|
|
33
|
+
dumbbell(
|
|
34
|
+
ax,
|
|
35
|
+
countries,
|
|
36
|
+
gdp_2000,
|
|
37
|
+
gdp_2020,
|
|
38
|
+
label_start="2000",
|
|
39
|
+
label_end="2020",
|
|
40
|
+
)
|
|
41
|
+
ax.set_xlim(-0.5, 24)
|
|
42
|
+
ax.xaxis.set_major_formatter(
|
|
43
|
+
ticker.FuncFormatter(lambda v, _: f"${v:.0f}tn")
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
finalize(
|
|
47
|
+
ax,
|
|
48
|
+
title="The great divergence",
|
|
49
|
+
descriptor="GDP in current US dollars, selected economies",
|
|
50
|
+
source="Source: World Bank",
|
|
51
|
+
y_axis_right=False,
|
|
52
|
+
title_x=0.02,
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
bbox = ax.get_position()
|
|
56
|
+
fig.legend(
|
|
57
|
+
handles=ax._dumbbell_handles,
|
|
58
|
+
labels=["2000", "2020"],
|
|
59
|
+
loc="upper right",
|
|
60
|
+
bbox_to_anchor=(bbox.x1, bbox.y1 + 0.005),
|
|
61
|
+
bbox_transform=fig.transFigure,
|
|
62
|
+
frameon=False,
|
|
63
|
+
fontsize=8.5,
|
|
64
|
+
ncol=2,
|
|
65
|
+
handletextpad=0.4,
|
|
66
|
+
columnspacing=1.0,
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
out = Path(__file__).resolve().parent / "economist_dumbbell.png"
|
|
70
|
+
plt.savefig(out, bbox_inches="tight", dpi=150)
|
|
71
|
+
plt.close()
|
|
72
|
+
print("Saved dumbbell chart")
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# /// script
|
|
2
|
+
# requires-python = ">=3.12"
|
|
3
|
+
# dependencies = ["matplotlib", "numpy"]
|
|
4
|
+
# ///
|
|
5
|
+
"""Faceted line chart with panel labels — The Economist style."""
|
|
6
|
+
|
|
7
|
+
import sys
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
|
11
|
+
|
|
12
|
+
import matplotlib.pyplot as plt
|
|
13
|
+
import matplotlib.ticker as ticker
|
|
14
|
+
import numpy as np
|
|
15
|
+
|
|
16
|
+
from graphs import (
|
|
17
|
+
C_BG,
|
|
18
|
+
C_LABEL,
|
|
19
|
+
C_RED,
|
|
20
|
+
C_SPINE,
|
|
21
|
+
ci_fill,
|
|
22
|
+
finalize,
|
|
23
|
+
panel_label,
|
|
24
|
+
set_theme,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
set_theme()
|
|
28
|
+
|
|
29
|
+
np.random.seed(7)
|
|
30
|
+
x = np.linspace(-6, 10, 80)
|
|
31
|
+
panels = {
|
|
32
|
+
"Economic": np.where(
|
|
33
|
+
x < 0, x * 0.01, np.cumsum(np.random.randn(80) * 0.015)
|
|
34
|
+
),
|
|
35
|
+
"Violent": np.cumsum(np.random.randn(80) * 0.008),
|
|
36
|
+
"Sexual": np.cumsum(np.random.randn(80) * 0.006),
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
fig, axes = plt.subplots(1, 3, figsize=(10, 5.5), sharey=False)
|
|
40
|
+
fig.subplots_adjust(
|
|
41
|
+
top=0.72, bottom=0.16, left=0.04, right=0.97, wspace=0.35
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
for ax, (panel_name, y) in zip(axes, panels.items()):
|
|
45
|
+
ci = np.abs(np.random.randn(len(x))) * 0.025 + 0.01
|
|
46
|
+
ci_fill(ax, x, y - ci, y + ci)
|
|
47
|
+
ax.plot(x, y, color=C_RED, linewidth=2)
|
|
48
|
+
ax.axhline(0, color=C_SPINE, linewidth=0.8, zorder=3)
|
|
49
|
+
ax.scatter([0], [0], color=C_SPINE, s=40, zorder=5)
|
|
50
|
+
|
|
51
|
+
ax.yaxis.set_label_position("right")
|
|
52
|
+
ax.yaxis.tick_right()
|
|
53
|
+
ax.spines["right"].set_visible(True)
|
|
54
|
+
ax.spines["right"].set_color(C_BG)
|
|
55
|
+
ax.spines["bottom"].set_color(C_SPINE)
|
|
56
|
+
ax.spines[["top", "left"]].set_visible(False)
|
|
57
|
+
ax.yaxis.set_tick_params(pad=-2, labelsize=8.5)
|
|
58
|
+
ax.set_ylim(-0.22, 0.22)
|
|
59
|
+
ax.yaxis.set_major_formatter(ticker.FormatStrFormatter("%.1f"))
|
|
60
|
+
ax.set_xlabel(
|
|
61
|
+
"Years since cancer diagnosis (1980-2018)",
|
|
62
|
+
fontsize=8,
|
|
63
|
+
color=C_LABEL,
|
|
64
|
+
)
|
|
65
|
+
panel_label(ax, panel_name)
|
|
66
|
+
|
|
67
|
+
finalize(
|
|
68
|
+
axes[0],
|
|
69
|
+
title=(
|
|
70
|
+
"Denmark, criminal-conviction rate since\n"
|
|
71
|
+
"cancer diagnosis, by type of offense"
|
|
72
|
+
),
|
|
73
|
+
descriptor="Percentage-point change from baseline",
|
|
74
|
+
source=(
|
|
75
|
+
'Source: "Breaking bad", '
|
|
76
|
+
"American Economics Journal, 2026"
|
|
77
|
+
),
|
|
78
|
+
y_axis_right=False,
|
|
79
|
+
title_x=0.04,
|
|
80
|
+
y_start=0.075,
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
out = Path(__file__).resolve().parent / "economist_facet.png"
|
|
84
|
+
plt.savefig(out, bbox_inches="tight", dpi=150)
|
|
85
|
+
plt.close()
|
|
86
|
+
print("Saved facet chart")
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# /// script
|
|
2
|
+
# requires-python = ">=3.12"
|
|
3
|
+
# dependencies = ["matplotlib", "numpy"]
|
|
4
|
+
# ///
|
|
5
|
+
"""Line chart with CI bands and direct labels — The Economist style."""
|
|
6
|
+
|
|
7
|
+
import sys
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
|
11
|
+
|
|
12
|
+
import matplotlib.pyplot as plt
|
|
13
|
+
import matplotlib.ticker as ticker
|
|
14
|
+
import numpy as np
|
|
15
|
+
|
|
16
|
+
from graphs import ci_fill, finalize, label_lines, set_theme
|
|
17
|
+
|
|
18
|
+
set_theme()
|
|
19
|
+
|
|
20
|
+
np.random.seed(42)
|
|
21
|
+
years = np.arange(2000, 2024)
|
|
22
|
+
data = {
|
|
23
|
+
"United States": np.cumsum(np.random.randn(24) * 1.5) + 100,
|
|
24
|
+
"China": np.cumsum(np.random.randn(24) * 2.0) + 100,
|
|
25
|
+
"Germany": np.cumsum(np.random.randn(24) * 1.0) + 100,
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
fig, ax = plt.subplots()
|
|
29
|
+
fig.subplots_adjust(top=0.68, bottom=0.14, left=0.06, right=0.88)
|
|
30
|
+
|
|
31
|
+
for col, y in data.items():
|
|
32
|
+
ci = np.abs(np.random.randn(len(years))) * 2.5 + 0.5
|
|
33
|
+
ci_fill(ax, years, y - ci, y + ci)
|
|
34
|
+
ax.plot(years, y, label=col)
|
|
35
|
+
|
|
36
|
+
label_lines(ax, stroke=True, min_sep_pct=6.0)
|
|
37
|
+
ax.yaxis.set_major_formatter(ticker.FormatStrFormatter("%.0f"))
|
|
38
|
+
|
|
39
|
+
finalize(
|
|
40
|
+
ax,
|
|
41
|
+
title="GDP across major economies",
|
|
42
|
+
descriptor="Index, 2000=100",
|
|
43
|
+
source="Sources: IMF; World Bank",
|
|
44
|
+
y_axis_right=True,
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
out = Path(__file__).resolve().parent / "economist_line.png"
|
|
48
|
+
plt.savefig(out, bbox_inches="tight", dpi=150)
|
|
49
|
+
plt.close()
|
|
50
|
+
print("Saved line chart")
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"""graphs — Economist-style chart theme for matplotlib/seaborn.
|
|
2
|
+
|
|
3
|
+
Usage::
|
|
4
|
+
|
|
5
|
+
from graphs import set_theme, finalize, colors
|
|
6
|
+
set_theme()
|
|
7
|
+
|
|
8
|
+
fig, ax = plt.subplots()
|
|
9
|
+
fig.subplots_adjust(top=0.68, bottom=0.14, left=0.06, right=0.88)
|
|
10
|
+
ax.plot(...)
|
|
11
|
+
finalize(ax, title="Bold headline", descriptor="Country, metric, unit",
|
|
12
|
+
source="Source: Organisation")
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from graphs._charts import bar_h, ci_fill, dumbbell
|
|
16
|
+
from graphs._finalize import finalize, panel_label
|
|
17
|
+
from graphs._fonts import _get_font as get_font
|
|
18
|
+
from graphs._labels import label_lines
|
|
19
|
+
from graphs._legend import smart_legend
|
|
20
|
+
from graphs._palette import (
|
|
21
|
+
C_BG,
|
|
22
|
+
C_CI,
|
|
23
|
+
C_GRID,
|
|
24
|
+
C_LABEL,
|
|
25
|
+
C_RED,
|
|
26
|
+
C_SPINE,
|
|
27
|
+
C_TEXT,
|
|
28
|
+
colors,
|
|
29
|
+
)
|
|
30
|
+
from graphs._theme import set_theme
|
|
31
|
+
|
|
32
|
+
__version__ = "0.1.0"
|
|
33
|
+
|
|
34
|
+
__all__ = [
|
|
35
|
+
"bar_h",
|
|
36
|
+
"ci_fill",
|
|
37
|
+
"colors",
|
|
38
|
+
"C_BG",
|
|
39
|
+
"C_CI",
|
|
40
|
+
"C_GRID",
|
|
41
|
+
"C_LABEL",
|
|
42
|
+
"C_RED",
|
|
43
|
+
"C_SPINE",
|
|
44
|
+
"C_TEXT",
|
|
45
|
+
"dumbbell",
|
|
46
|
+
"finalize",
|
|
47
|
+
"get_font",
|
|
48
|
+
"label_lines",
|
|
49
|
+
"panel_label",
|
|
50
|
+
"set_theme",
|
|
51
|
+
"smart_legend",
|
|
52
|
+
]
|