Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit db4bb3a

Browse filesBrowse files
committed
Add custom fonts directly to Matplotlib
This removes the need to manually install them system- or user-wide.
1 parent 05d47a5 commit db4bb3a
Copy full SHA for db4bb3a

Some content is hidden

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

41 files changed

+158
-18
lines changed

‎.github/workflows/main.yaml

Copy file name to clipboardExpand all lines: .github/workflows/main.yaml
-2Lines changed: 0 additions & 2 deletions
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: |

‎.gitignore

Copy file name to clipboardExpand all lines: .gitignore
+1Lines 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

Copy file name to clipboardExpand all lines: README.md
+1-12Lines 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
@@ -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

Copy file name to clipboard
+8Lines changed: 8 additions & 0 deletions
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

Copy file name to clipboardExpand all lines: scripts/adjustements.py
+4Lines changed: 4 additions & 0 deletions
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
import pathlib
67

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

1314

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

1620
mpl.style.use([
1721
ROOT_DIR / 'styles/base.mplstyle',

‎scripts/advanced-plots.py

Copy file name to clipboardExpand all lines: scripts/advanced-plots.py
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# -----------------------------------------------------------------------------
55

66
# Script to generate all the advanced plots
7+
import sys
78
import pathlib
89

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

1314

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

1620
mpl.style.use([
1721
ROOT_DIR / 'styles/base.mplstyle',

‎scripts/anatomy.py

Copy file name to clipboardExpand all lines: scripts/anatomy.py
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Author: Nicolas P. Rougier
44
# License: BSD
55
# ----------------------------------------------------------------------------
6+
import sys
67
import pathlib
78

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

1314

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

1620
mpl.style.use([
1721
ROOT_DIR / 'styles/base.mplstyle',

‎scripts/annotate.py

Copy file name to clipboardExpand all lines: scripts/annotate.py
+4Lines changed: 4 additions & 0 deletions
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
import pathlib
67

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

1112

1213
ROOT_DIR = pathlib.Path(__file__).parent.parent
14+
sys.path.append(str(ROOT_DIR / "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

Copy file name to clipboardExpand all lines: scripts/annotation-arrow-styles.py
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
import sys
12
import pathlib
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+
812

913
styles = mpatches.ArrowStyle.get_styles()
1014

‎scripts/annotation-connection-styles.py

Copy file name to clipboardExpand all lines: scripts/annotation-connection-styles.py
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
import sys
12
import pathlib
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+
812

913
mpl.style.use([
1014
ROOT_DIR / 'styles/base.mplstyle',

‎scripts/basic-plots.py

Copy file name to clipboardExpand all lines: scripts/basic-plots.py
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# -----------------------------------------------------------------------------
55

66
# Script to generate all the basic plots
7+
import sys
78
import pathlib
89

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

1314

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

1620
mpl.style.use([
1721
ROOT_DIR / 'styles/base.mplstyle',

‎scripts/colorbar.py

Copy file name to clipboardExpand all lines: scripts/colorbar.py
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Released under the BSD License
44
# -----------------------------------------------------------------------------
55

6+
import sys
67
import pathlib
78

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

1213

1314
ROOT_DIR = pathlib.Path(__file__).parent.parent
15+
sys.path.append(str(ROOT_DIR / "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

Copy file name to clipboardExpand all lines: scripts/colormaps.py
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
import sys
12
import pathlib
23

34
import numpy as np
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+
812

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

‎scripts/colornames.py

Copy file name to clipboardExpand all lines: scripts/colornames.py
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55
66
Simple plot example with the named colors and its visual representation.
77
"""
8+
import sys
89
import pathlib
910

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+
1519

1620
mpl.style.use([
1721
ROOT_DIR / 'styles/base.mplstyle',

‎scripts/colors.py

Copy file name to clipboardExpand all lines: scripts/colors.py
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Released under the BSD License
44
# -----------------------------------------------------------------------------
55

6+
import sys
67
import pathlib
78

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

1213

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

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

‎scripts/extents.py

Copy file name to clipboardExpand all lines: scripts/extents.py
+4Lines changed: 4 additions & 0 deletions
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
import pathlib
67

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

1112

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

1418
mpl.style.use([
1519
ROOT_DIR / 'styles/base.mplstyle',

‎scripts/fonts.py

Copy file name to clipboardExpand all lines: scripts/fonts.py
+8-4Lines changed: 8 additions & 4 deletions
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
import pathlib
67

78
import matplotlib.pyplot as plt
89

910

1011
ROOT_DIR = pathlib.Path(__file__).parent.parent
12+
sys.path.append(str(ROOT_DIR / "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=pathlib.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

Copy file name to clipboardExpand all lines: scripts/interpolations.py
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
import pathlib
23

34
import matplotlib as mpl
@@ -6,6 +7,9 @@
67

78

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

1014
mpl.style.use([
1115
ROOT_DIR / 'styles/base.mplstyle',

‎scripts/layouts.py

Copy file name to clipboardExpand all lines: scripts/layouts.py
+4Lines changed: 4 additions & 0 deletions
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
import pathlib
67

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

1213

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

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

‎scripts/legend.py

Copy file name to clipboardExpand all lines: scripts/legend.py
+4Lines changed: 4 additions & 0 deletions
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
import pathlib
67

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

1112

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

1418
mpl.style.use([
1519
ROOT_DIR / 'styles/base.mplstyle',

‎scripts/linestyles.py

Copy file name to clipboardExpand all lines: scripts/linestyles.py
+4Lines changed: 4 additions & 0 deletions
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
import pathlib
67

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

1011

1112
ROOT_DIR = pathlib.Path(__file__).parent.parent
13+
sys.path.append(str(ROOT_DIR / "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

Copy file name to clipboardExpand all lines: scripts/markers.py
+4Lines changed: 4 additions & 0 deletions
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
import pathlib
67

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

1011

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

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

‎scripts/plot-variations.py

Copy file name to clipboardExpand all lines: scripts/plot-variations.py
+4Lines changed: 4 additions & 0 deletions
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
import pathlib
67

78
# Scripts to generate all the basic plots
@@ -11,6 +12,9 @@
1112

1213

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

1519
mpl.style.use([
1620
ROOT_DIR / 'styles/base.mplstyle',

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.