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

Lines 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: |

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
@@ -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

Lines 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

Lines 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
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines 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
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy