Skip to content

Commit

Permalink
Add custom fonts directly to Matplotlib
Browse files Browse the repository at this point in the history
This removes the need to manually install them system- or user-wide.
  • Loading branch information
QuLogic committed Mar 29, 2023
1 parent acd606e commit 2a6abf5
Show file tree
Hide file tree
Showing 40 changed files with 156 additions and 20 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ jobs:
sudo sed -i '/disable ghostscript format types/,+6d' /etc/ImageMagick-6/policy.xml
#
make -C fonts/
cp -r fonts/ /usr/share/fonts/
fc-cache
make all
- name: Run checks
run: |
Expand Down
13 changes: 1 addition & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Beginner handout [(download pdf)](https://matplotlib.org/cheatsheets/handout-beg

## How to compile

1. You need to create a `fonts` repository with:
1. You need to fill the `fonts` directory with:

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

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

```xml
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<dir>/path/to/cheatsheets/fonts/</dir>
...
</fontconfig>
```


2. You need to generate all the figures:

Expand Down
8 changes: 8 additions & 0 deletions fonts/custom_fonts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from pathlib import Path

from matplotlib.font_manager import fontManager


HERE = Path(__file__).parent.resolve()
for font in HERE.glob('*/*.[ot]tf'):
fontManager.addfont(font)
4 changes: 4 additions & 0 deletions scripts/adjustements.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Released under the BSD License
# -----------------------------------------------------------------------------

import sys
from pathlib import Path

import numpy as np
Expand All @@ -12,6 +13,9 @@


REPO = Path(__file__).parent.parent
sys.path.append(str(REPO / "fonts"))
import custom_fonts # noqa


fig = plt.figure(figsize=(4.25, 4.25 * 95/115))
ax = fig.add_axes([0, 0, 1, 1], frameon=False, aspect=1,
Expand Down
4 changes: 4 additions & 0 deletions scripts/advanced-plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# -----------------------------------------------------------------------------

# Scripts to generate all the basic plots
import sys
from pathlib import Path

import numpy as np
Expand All @@ -12,6 +13,9 @@


REPO = Path(__file__).parent.parent
sys.path.append(str(REPO / "fonts"))
import custom_fonts # noqa


fig = plt.figure(figsize=(0.4, 0.4))
mpl.rcParams['axes.linewidth'] = 0.5
Expand Down
4 changes: 4 additions & 0 deletions scripts/anatomy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# License: BSD
# ----------------------------------------------------------------------------

import sys
from pathlib import Path

import numpy as np
Expand All @@ -12,6 +13,9 @@


REPO = Path(__file__).parent.parent
sys.path.append(str(REPO / "fonts"))
import custom_fonts # noqa


np.random.seed(123)

Expand Down
4 changes: 4 additions & 0 deletions scripts/annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Matplotlib cheat sheet
# Released under the BSD License
# -----------------------------------------------------------------------------
import sys
from pathlib import Path

import numpy as np
Expand All @@ -10,6 +11,9 @@


REPO = Path(__file__).parent.parent
sys.path.append(str(REPO / "fonts"))
import custom_fonts # noqa


fig = plt.figure(figsize=(6, 1))
# ax = plt.subplot(111, frameon=False, aspect=.1)
Expand Down
4 changes: 4 additions & 0 deletions scripts/annotation-arrow-styles.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import sys
from pathlib import Path

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches


REPO = Path(__file__).parent.parent
sys.path.append(str(REPO / "fonts"))
import custom_fonts # noqa


styles = mpatches.ArrowStyle.get_styles()

Expand Down
3 changes: 3 additions & 0 deletions scripts/annotation-connection-styles.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import sys
from pathlib import Path

import matplotlib.pyplot as plt


REPO = Path(__file__).parent.parent
sys.path.append(str(REPO / "fonts"))
import custom_fonts # noqa


def demo_con_style(ax, connectionstyle):
Expand Down
4 changes: 4 additions & 0 deletions scripts/basic-plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# -----------------------------------------------------------------------------

# Scripts to generate all the basic plots
import sys
from pathlib import Path

import numpy as np
Expand All @@ -12,6 +13,9 @@


REPO = Path(__file__).parent.parent
sys.path.append(str(REPO / "fonts"))
import custom_fonts # noqa


fig = plt.figure(figsize=(0.4, 0.4))
mpl.rcParams['axes.linewidth'] = 0.5
Expand Down
4 changes: 4 additions & 0 deletions scripts/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Released under the BSD License
# -----------------------------------------------------------------------------

import sys
from pathlib import Path

import numpy as np
Expand All @@ -11,6 +12,9 @@


REPO = Path(__file__).parent.parent
sys.path.append(str(REPO / "fonts"))
import custom_fonts # noqa


fig = plt.figure(figsize=(6, .65))
# ax = plt.subplot(111, frameon=False, aspect=.1)
Expand Down
4 changes: 4 additions & 0 deletions scripts/colormaps.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import sys
from pathlib import Path

import numpy as np
import matplotlib.pyplot as plt


REPO = Path(__file__).parent.parent
sys.path.append(str(REPO / "fonts"))
import custom_fonts # noqa


figsize = 4.0, 0.25
fig = plt.figure(figsize=figsize)
Expand Down
4 changes: 4 additions & 0 deletions scripts/colornames.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
Simple plot example with the named colors and its visual representation.
"""

import sys
from pathlib import Path

import matplotlib.pyplot as plt
from matplotlib import colors as mcolors


REPO = Path(__file__).parent.parent
sys.path.append(str(REPO / "fonts"))
import custom_fonts # noqa


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

Expand Down
4 changes: 4 additions & 0 deletions scripts/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Released under the BSD License
# -----------------------------------------------------------------------------

import sys
from pathlib import Path

import numpy as np
Expand All @@ -11,6 +12,9 @@


REPO = Path(__file__).parent.parent
sys.path.append(str(REPO / "fonts"))
import custom_fonts # noqa


figsize = 4.0, 0.25
fig = plt.figure(figsize=figsize)
Expand Down
4 changes: 4 additions & 0 deletions scripts/extents.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Released under the BSD License
# -----------------------------------------------------------------------------

import sys
from pathlib import Path

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


REPO = Path(__file__).parent.parent
sys.path.append(str(REPO / "fonts"))
import custom_fonts # noqa


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

Expand Down
12 changes: 8 additions & 4 deletions scripts/fonts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
# Matplotlib cheat sheet
# Released under the BSD License
# -----------------------------------------------------------------------------
import sys
from pathlib import Path

import matplotlib.pyplot as plt


REPO = Path(__file__).parent.parent
sys.path.append(str(REPO / "fonts"))
import custom_fonts # noqa


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

# -----------------------------------------------------------------------------
variants = {
"normal" : "../fonts/eb-garamond/EBGaramond08-Regular.otf",
"small-caps" : "../fonts/eb-garamond/EBGaramondSC08-Regular.otf"
"normal" : "EB Garamond",
"small-caps" : "EB Garamond SC"
}

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

ax.text(39, y, variant,
color="0.25", va="center", ha="right",
Expand Down
4 changes: 4 additions & 0 deletions scripts/interpolations.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import sys
from pathlib import Path

import matplotlib.pyplot as plt
import numpy as np


REPO = Path(__file__).parent.parent
sys.path.append(str(REPO / "fonts"))
import custom_fonts # noqa


methods = [None, 'none', 'nearest', 'bilinear', 'bicubic', 'spline16',
'spline36', 'hanning', 'hamming', 'hermite', 'kaiser', 'quadric',
Expand Down
4 changes: 4 additions & 0 deletions scripts/layouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Matplotlib cheat sheet
# Released under the BSD License
# -----------------------------------------------------------------------------
import sys
from pathlib import Path

import numpy as np
Expand All @@ -11,6 +12,9 @@


REPO = Path(__file__).parent.parent
sys.path.append(str(REPO / "fonts"))
import custom_fonts # noqa


fig = plt.figure(figsize=(0.4, 0.4))
margin = 0.01
Expand Down
4 changes: 4 additions & 0 deletions scripts/legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Matplotlib cheat sheet
# Released under the BSD License
# -----------------------------------------------------------------------------
import sys
from pathlib import Path

import numpy as np
Expand All @@ -10,6 +11,9 @@


REPO = Path(__file__).parent.parent
sys.path.append(str(REPO / "fonts"))
import custom_fonts # noqa


fig = plt.figure(figsize=(4, 4))
ax = fig.add_axes([0.15, 0.15, .7, .7], frameon=True, aspect=1,
Expand Down
4 changes: 4 additions & 0 deletions scripts/linestyles.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
# Matplotlib cheat sheet
# Released under the BSD License
# -----------------------------------------------------------------------------
import sys
from pathlib import Path

import numpy as np
import matplotlib.pyplot as plt


REPO = Path(__file__).parent.parent
sys.path.append(str(REPO / "fonts"))
import custom_fonts # noqa


fig = plt.figure(figsize=(4.25, 2*.55))
ax = fig.add_axes([0, 0, 1, 1], xlim=[0.75, 10.25], ylim=[0.5, 2.5], frameon=False,
Expand Down
4 changes: 4 additions & 0 deletions scripts/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
# Matplotlib cheat sheet
# Released under the BSD License
# -----------------------------------------------------------------------------
import sys
from pathlib import Path

import numpy as np
import matplotlib.pyplot as plt


REPO = Path(__file__).parent.parent
sys.path.append(str(REPO / "fonts"))
import custom_fonts # noqa


# Markers
# -----------------------------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions scripts/plot-variations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# -----------------------------------------------------------------------------

# Scripts to generate all the basic plots
import sys
from pathlib import Path

import numpy as np
Expand All @@ -12,6 +13,9 @@


REPO = Path(__file__).parent.parent
sys.path.append(str(REPO / "fonts"))
import custom_fonts # noqa


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

0 comments on commit 2a6abf5

Please sign in to comment.