Skip to content

Commit 2a6abf5

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

40 files changed

+156
-20
lines changed

.github/workflows/main.yaml

-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ jobs:
4848
sudo sed -i '/disable ghostscript format types/,+6d' /etc/ImageMagick-6/policy.xml
4949
#
5050
make -C fonts/
51-
cp -r fonts/ /usr/share/fonts/
52-
fc-cache
5351
make all
5452
- name: Run checks
5553
run: |

README.md

+1-12
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
@@ -35,17 +35,6 @@ On Linux, with `make` installed, the fonts can be set up with the following comm
3535
make -C fonts
3636
```
3737

38-
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)):
39-
40-
```xml
41-
<?xml version="1.0"?>
42-
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
43-
<fontconfig>
44-
<dir>/path/to/cheatsheets/fonts/</dir>
45-
...
46-
</fontconfig>
47-
```
48-
4938

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

fonts/custom_fonts.py

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

scripts/adjustements.py

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Released under the BSD License
44
# -----------------------------------------------------------------------------
55

6+
import sys
67
from pathlib import Path
78

89
import numpy as np
@@ -12,6 +13,9 @@
1213

1314

1415
REPO = Path(__file__).parent.parent
16+
sys.path.append(str(REPO / "fonts"))
17+
import custom_fonts # noqa
18+
1519

1620
fig = plt.figure(figsize=(4.25, 4.25 * 95/115))
1721
ax = fig.add_axes([0, 0, 1, 1], frameon=False, aspect=1,

scripts/advanced-plots.py

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# -----------------------------------------------------------------------------
55

66
# Scripts to generate all the basic plots
7+
import sys
78
from pathlib import Path
89

910
import numpy as np
@@ -12,6 +13,9 @@
1213

1314

1415
REPO = Path(__file__).parent.parent
16+
sys.path.append(str(REPO / "fonts"))
17+
import custom_fonts # noqa
18+
1519

1620
fig = plt.figure(figsize=(0.4, 0.4))
1721
mpl.rcParams['axes.linewidth'] = 0.5

scripts/anatomy.py

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# License: BSD
55
# ----------------------------------------------------------------------------
66

7+
import sys
78
from pathlib import Path
89

910
import numpy as np
@@ -12,6 +13,9 @@
1213

1314

1415
REPO = Path(__file__).parent.parent
16+
sys.path.append(str(REPO / "fonts"))
17+
import custom_fonts # noqa
18+
1519

1620
np.random.seed(123)
1721

scripts/annotate.py

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Matplotlib cheat sheet
33
# Released under the BSD License
44
# -----------------------------------------------------------------------------
5+
import sys
56
from pathlib import Path
67

78
import numpy as np
@@ -10,6 +11,9 @@
1011

1112

1213
REPO = Path(__file__).parent.parent
14+
sys.path.append(str(REPO / "fonts"))
15+
import custom_fonts # noqa
16+
1317

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

scripts/annotation-arrow-styles.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
import sys
12
from pathlib import Path
23

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

67

78
REPO = Path(__file__).parent.parent
9+
sys.path.append(str(REPO / "fonts"))
10+
import custom_fonts # noqa
11+
812

913
styles = mpatches.ArrowStyle.get_styles()
1014

scripts/annotation-connection-styles.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import sys
12
from pathlib import Path
23

34
import matplotlib.pyplot as plt
45

56

67
REPO = Path(__file__).parent.parent
8+
sys.path.append(str(REPO / "fonts"))
9+
import custom_fonts # noqa
710

811

912
def demo_con_style(ax, connectionstyle):

scripts/basic-plots.py

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# -----------------------------------------------------------------------------
55

66
# Scripts to generate all the basic plots
7+
import sys
78
from pathlib import Path
89

910
import numpy as np
@@ -12,6 +13,9 @@
1213

1314

1415
REPO = Path(__file__).parent.parent
16+
sys.path.append(str(REPO / "fonts"))
17+
import custom_fonts # noqa
18+
1519

1620
fig = plt.figure(figsize=(0.4, 0.4))
1721
mpl.rcParams['axes.linewidth'] = 0.5

scripts/colorbar.py

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Released under the BSD License
44
# -----------------------------------------------------------------------------
55

6+
import sys
67
from pathlib import Path
78

89
import numpy as np
@@ -11,6 +12,9 @@
1112

1213

1314
REPO = Path(__file__).parent.parent
15+
sys.path.append(str(REPO / "fonts"))
16+
import custom_fonts # noqa
17+
1418

1519
fig = plt.figure(figsize=(6, .65))
1620
# ax = plt.subplot(111, frameon=False, aspect=.1)

scripts/colormaps.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
import sys
12
from pathlib import Path
23

34
import numpy as np
45
import matplotlib.pyplot as plt
56

67

78
REPO = Path(__file__).parent.parent
9+
sys.path.append(str(REPO / "fonts"))
10+
import custom_fonts # noqa
11+
812

913
figsize = 4.0, 0.25
1014
fig = plt.figure(figsize=figsize)

scripts/colornames.py

+4
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66
Simple plot example with the named colors and its visual representation.
77
"""
88

9+
import sys
910
from pathlib import Path
1011

1112
import matplotlib.pyplot as plt
1213
from matplotlib import colors as mcolors
1314

1415

1516
REPO = Path(__file__).parent.parent
17+
sys.path.append(str(REPO / "fonts"))
18+
import custom_fonts # noqa
19+
1620

1721
colors = dict(mcolors.BASE_COLORS, **mcolors.CSS4_COLORS)
1822

scripts/colors.py

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Released under the BSD License
44
# -----------------------------------------------------------------------------
55

6+
import sys
67
from pathlib import Path
78

89
import numpy as np
@@ -11,6 +12,9 @@
1112

1213

1314
REPO = Path(__file__).parent.parent
15+
sys.path.append(str(REPO / "fonts"))
16+
import custom_fonts # noqa
17+
1418

1519
figsize = 4.0, 0.25
1620
fig = plt.figure(figsize=figsize)

scripts/extents.py

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Released under the BSD License
44
# -----------------------------------------------------------------------------
55

6+
import sys
67
from pathlib import Path
78

89
# Scripts to generate all the basic plots
@@ -12,6 +13,9 @@
1213

1314

1415
REPO = Path(__file__).parent.parent
16+
sys.path.append(str(REPO / "fonts"))
17+
import custom_fonts # noqa
18+
1519

1620
Z = np.arange(5*5).reshape(5, 5)
1721

scripts/fonts.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22
# Matplotlib cheat sheet
33
# Released under the BSD License
44
# -----------------------------------------------------------------------------
5+
import sys
56
from pathlib import Path
67

78
import matplotlib.pyplot as plt
89

910

1011
REPO = Path(__file__).parent.parent
12+
sys.path.append(str(REPO / "fonts"))
13+
import custom_fonts # noqa
14+
1115

1216
fig = plt.figure(figsize=(4.25, 3.8))
1317
ax = fig.add_axes([0, 0, 1, 1], frameon=False, xticks=[], yticks=[],
@@ -17,13 +21,13 @@
1721

1822
# -----------------------------------------------------------------------------
1923
variants = {
20-
"normal" : "../fonts/eb-garamond/EBGaramond08-Regular.otf",
21-
"small-caps" : "../fonts/eb-garamond/EBGaramondSC08-Regular.otf"
24+
"normal" : "EB Garamond",
25+
"small-caps" : "EB Garamond SC"
2226
}
2327

2428
text = "The quick brown fox jumps over the lazy dog"
25-
for i, (variant, file) in enumerate(variants.items()):
26-
ax.text(1, y, text, size=9, va="center", font=Path(file).resolve())
29+
for i, (variant, family) in enumerate(variants.items()):
30+
ax.text(1, y, text, size=9, va="center", family=family)
2731

2832
ax.text(39, y, variant,
2933
color="0.25", va="center", ha="right",

scripts/interpolations.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
import sys
12
from pathlib import Path
23

34
import matplotlib.pyplot as plt
45
import numpy as np
56

67

78
REPO = Path(__file__).parent.parent
9+
sys.path.append(str(REPO / "fonts"))
10+
import custom_fonts # noqa
11+
812

913
methods = [None, 'none', 'nearest', 'bilinear', 'bicubic', 'spline16',
1014
'spline36', 'hanning', 'hamming', 'hermite', 'kaiser', 'quadric',

scripts/layouts.py

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Matplotlib cheat sheet
33
# Released under the BSD License
44
# -----------------------------------------------------------------------------
5+
import sys
56
from pathlib import Path
67

78
import numpy as np
@@ -11,6 +12,9 @@
1112

1213

1314
REPO = Path(__file__).parent.parent
15+
sys.path.append(str(REPO / "fonts"))
16+
import custom_fonts # noqa
17+
1418

1519
fig = plt.figure(figsize=(0.4, 0.4))
1620
margin = 0.01

scripts/legend.py

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Matplotlib cheat sheet
33
# Released under the BSD License
44
# -----------------------------------------------------------------------------
5+
import sys
56
from pathlib import Path
67

78
import numpy as np
@@ -10,6 +11,9 @@
1011

1112

1213
REPO = Path(__file__).parent.parent
14+
sys.path.append(str(REPO / "fonts"))
15+
import custom_fonts # noqa
16+
1317

1418
fig = plt.figure(figsize=(4, 4))
1519
ax = fig.add_axes([0.15, 0.15, .7, .7], frameon=True, aspect=1,

scripts/linestyles.py

+4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
# Matplotlib cheat sheet
33
# Released under the BSD License
44
# -----------------------------------------------------------------------------
5+
import sys
56
from pathlib import Path
67

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

1011

1112
REPO = Path(__file__).parent.parent
13+
sys.path.append(str(REPO / "fonts"))
14+
import custom_fonts # noqa
15+
1216

1317
fig = plt.figure(figsize=(4.25, 2*.55))
1418
ax = fig.add_axes([0, 0, 1, 1], xlim=[0.75, 10.25], ylim=[0.5, 2.5], frameon=False,

scripts/markers.py

+4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
# Matplotlib cheat sheet
33
# Released under the BSD License
44
# -----------------------------------------------------------------------------
5+
import sys
56
from pathlib import Path
67

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

1011

1112
REPO = Path(__file__).parent.parent
13+
sys.path.append(str(REPO / "fonts"))
14+
import custom_fonts # noqa
15+
1216

1317
# Markers
1418
# -----------------------------------------------------------------------------

scripts/plot-variations.py

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# -----------------------------------------------------------------------------
55

66
# Scripts to generate all the basic plots
7+
import sys
78
from pathlib import Path
89

910
import numpy as np
@@ -12,6 +13,9 @@
1213

1314

1415
REPO = Path(__file__).parent.parent
16+
sys.path.append(str(REPO / "fonts"))
17+
import custom_fonts # noqa
18+
1519

1620
fig = plt.figure(figsize=(0.4, 0.4))
1721
mpl.rcParams['axes.linewidth'] = 0.5

0 commit comments

Comments
 (0)