Skip to content

Commit 9bc14f4

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 9bc14f4

41 files changed

Lines changed: 192 additions & 14 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+
fonts/__pycache__/
1920

2021
# html build
2122
docs/_build/*

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

fonts/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+
here = Path(__file__).parent.resolve()
8+
for font in here.glob('*/*.[ot]tf'):
9+
fontManager.addfont(font)

scripts/adjustements.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Released under the BSD License
44
# -----------------------------------------------------------------------------
55
import pathlib
6+
import sys
67

78
import numpy as np
89
import matplotlib as mpl
@@ -12,6 +13,10 @@
1213

1314

1415
ROOT_DIR = pathlib.Path(__file__).parent.parent
16+
sys.path.append(str(ROOT_DIR / "fonts"))
17+
import custom_fonts # noqa
18+
custom_fonts.setup()
19+
1520

1621
mpl.style.use([
1722
ROOT_DIR / 'styles/base.mplstyle',

scripts/advanced-plots.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@
55

66
# Script to generate all the advanced plots
77
import pathlib
8+
import sys
89

910
import numpy as np
1011
import matplotlib as mpl
1112
import matplotlib.pyplot as plt
1213

1314

1415
ROOT_DIR = pathlib.Path(__file__).parent.parent
16+
sys.path.append(str(ROOT_DIR / "fonts"))
17+
import custom_fonts # noqa
18+
custom_fonts.setup()
19+
1520

1621
mpl.style.use([
1722
ROOT_DIR / 'styles/base.mplstyle',

scripts/anatomy.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# License: BSD
55
# ----------------------------------------------------------------------------
66
import pathlib
7+
import sys
78

89
import numpy as np
910
import matplotlib as mpl
@@ -12,6 +13,10 @@
1213

1314

1415
ROOT_DIR = pathlib.Path(__file__).parent.parent
16+
sys.path.append(str(ROOT_DIR / "fonts"))
17+
import custom_fonts # noqa
18+
custom_fonts.setup()
19+
1520

1621
mpl.style.use([
1722
ROOT_DIR / 'styles/base.mplstyle',

scripts/annotate.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@
33
# Released under the BSD License
44
# -----------------------------------------------------------------------------
55
import pathlib
6+
import sys
67

78
import numpy as np
89
import matplotlib as mpl
910
import matplotlib.pyplot as plt
1011

1112

1213
ROOT_DIR = pathlib.Path(__file__).parent.parent
14+
sys.path.append(str(ROOT_DIR / "fonts"))
15+
import custom_fonts # noqa
16+
custom_fonts.setup()
17+
1318

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

scripts/annotation-arrow-styles.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import pathlib
2+
import sys
23

34
import matplotlib.pyplot as plt
45
import matplotlib.patches as mpatches
56

67

78
ROOT_DIR = pathlib.Path(__file__).parent.parent
9+
sys.path.append(str(ROOT_DIR / "fonts"))
10+
import custom_fonts # noqa
11+
custom_fonts.setup()
12+
813

914
styles = mpatches.ArrowStyle.get_styles()
1015

scripts/annotation-connection-styles.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import pathlib
2+
import sys
23

34
import matplotlib as mpl
45
import matplotlib.pyplot as plt
56

67

78
ROOT_DIR = pathlib.Path(__file__).parent.parent
9+
sys.path.append(str(ROOT_DIR / "fonts"))
10+
import custom_fonts # noqa
11+
custom_fonts.setup()
12+
813

914
mpl.style.use([
1015
ROOT_DIR / 'styles/base.mplstyle',

0 commit comments

Comments
 (0)