Skip to content

Commit b8fab59

Browse files
committed
Add custom fonts directly to Matplotlib
This removes the need to manually install them system- or user-wide.
1 parent f5e0210 commit b8fab59

42 files changed

Lines changed: 126 additions & 15 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/main.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ jobs:
5555
sudo sed -i '/disable ghostscript format types/,+6d' /etc/ImageMagick-6/policy.xml
5656
#
5757
make -C fonts/
58-
cp -r fonts/ /usr/share/fonts/
59-
fc-cache
6058
make all
6159
- name: Run checks
6260
run: |

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ handout-*.png
1616
# ----------------------------------
1717
figures/*.pdf
1818
fonts/**/*.[ot]tf
19+
scripts/__pycache__/
1920

2021
# html build
2122
docs/_build/*

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
SRC := $(wildcard *.tex)
2+
SCRIPTS := $(filter-out _%, $(notdir $(wildcard scripts/*.py)))
23
CONVERTFLAGS = -density 150 -alpha remove -depth 8
34

45
.PHONY: default
@@ -10,7 +11,7 @@ all: figures cheatsheets handouts docs
1011
.PHONY: figures
1112
figures:
1213
# generate the figures
13-
cd scripts && for script in *.py; do echo $$script; MPLBACKEND="agg" python $$script; done
14+
cd scripts && for script in $(SCRIPTS); do echo $$script; MPLBACKEND="agg" python $$script; done
1415
# crop some of the figures
1516
cd figures && pdfcrop adjustments.pdf adjustments.pdf
1617
cd figures && pdfcrop annotate.pdf annotate.pdf

README.md

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Beginner handout [(download pdf)](https://matplotlib.org/cheatsheets/handout-beg
1515

1616
## How to compile
1717

18-
1. You need to create a `fonts` repository with:
18+
1. You need to fill the `fonts` directory with:
1919

2020
* `fonts/roboto/*` : See https://fonts.google.com/specimen/Roboto
2121
or https://github.com/googlefonts/roboto/tree/master/src/hinted
@@ -34,17 +34,6 @@ On Linux, with `make` installed, the fonts can be set up with the following comm
3434
make -C fonts
3535
```
3636

37-
The fonts can be made discoverable by `matplotlib` (through `fontconfig`) by creating the following in `$HOME/.config/fontconfig/fonts.conf` (see [here](https://www.freedesktop.org/software/fontconfig/fontconfig-user.html)):
38-
39-
```xml
40-
<?xml version="1.0"?>
41-
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
42-
<fontconfig>
43-
<dir>/path/to/cheatsheets/fonts/</dir>
44-
...
45-
</fontconfig>
46-
```
47-
4837

4938
2. You need to generate all the figures:
5039

scripts/_custom_fonts.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from pathlib import Path
2+
3+
from matplotlib.font_manager import fontManager
4+
5+
6+
def setup():
7+
root_dir = Path(__file__).parent.parent.resolve()
8+
for font in root_dir.glob('fonts/*/*.[ot]tf'):
9+
fontManager.addfont(font)

scripts/adjustements.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
import matplotlib.patches as mpatches
1111
from matplotlib.collections import PatchCollection
1212

13+
import _custom_fonts
14+
1315

1416
ROOT_DIR = pathlib.Path(__file__).parent.parent
1517

18+
_custom_fonts.setup()
1619
mpl.style.use([
1720
ROOT_DIR / 'styles/base.mplstyle',
1821
])

scripts/advanced-plots.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
import matplotlib as mpl
1111
import matplotlib.pyplot as plt
1212

13+
import _custom_fonts
14+
1315

1416
ROOT_DIR = pathlib.Path(__file__).parent.parent
1517

18+
_custom_fonts.setup()
1619
mpl.style.use([
1720
ROOT_DIR / 'styles/base.mplstyle',
1821
ROOT_DIR / 'styles/plotlet.mplstyle',

scripts/anatomy.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
import matplotlib.pyplot as plt
1111
from matplotlib.ticker import AutoMinorLocator, MultipleLocator, FuncFormatter
1212

13+
import _custom_fonts
14+
1315

1416
ROOT_DIR = pathlib.Path(__file__).parent.parent
1517

18+
_custom_fonts.setup()
1619
mpl.style.use([
1720
ROOT_DIR / 'styles/base.mplstyle',
1821
])

scripts/annotate.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
import matplotlib as mpl
99
import matplotlib.pyplot as plt
1010

11+
import _custom_fonts
12+
1113

1214
ROOT_DIR = pathlib.Path(__file__).parent.parent
15+
_custom_fonts.setup()
1316

1417
fig = plt.figure(figsize=(6, 1))
1518
# ax = plt.subplot(111, frameon=False, aspect=.1)

scripts/annotation-arrow-styles.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
import matplotlib.pyplot as plt
44
import matplotlib.patches as mpatches
55

6+
import _custom_fonts
7+
68

79
ROOT_DIR = pathlib.Path(__file__).parent.parent
10+
_custom_fonts.setup()
811

912
styles = mpatches.ArrowStyle.get_styles()
1013

0 commit comments

Comments
 (0)