Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom fonts directly to Matplotlib #129

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ handout-*.png
# ----------------------------------
figures/*.pdf
fonts/**/*.[ot]tf
fonts/__pycache__/

# html build
docs/_build/*
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 @@ -2,6 +2,7 @@
# Matplotlib cheat sheet
# Released under the BSD License
# -----------------------------------------------------------------------------
import sys
import pathlib

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


ROOT_DIR = pathlib.Path(__file__).parent.parent
sys.path.append(str(ROOT_DIR / "fonts"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we have to put the fonts directory on the path?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So that it may be imported, regardless of where you run the script from.

import custom_fonts # noqa


mpl.style.use([
ROOT_DIR / 'styles/base.mplstyle',
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 @@
# -----------------------------------------------------------------------------

# Script to generate all the advanced plots
import sys
import pathlib

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


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


mpl.style.use([
ROOT_DIR / 'styles/base.mplstyle',
Expand Down
4 changes: 4 additions & 0 deletions scripts/anatomy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Author: Nicolas P. Rougier
# License: BSD
# ----------------------------------------------------------------------------
import sys
import pathlib

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


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


mpl.style.use([
ROOT_DIR / 'styles/base.mplstyle',
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
import pathlib

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


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

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


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


styles = mpatches.ArrowStyle.get_styles()

Expand Down
4 changes: 4 additions & 0 deletions scripts/annotation-connection-styles.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import sys
import pathlib

import matplotlib as mpl
import matplotlib.pyplot as plt


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


mpl.style.use([
ROOT_DIR / 'styles/base.mplstyle',
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 @@
# -----------------------------------------------------------------------------

# Script to generate all the basic plots
import sys
import pathlib

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


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


mpl.style.use([
ROOT_DIR / 'styles/base.mplstyle',
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
import pathlib

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


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

import numpy as np
import matplotlib.pyplot as plt


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

Simple plot example with the named colors and its visual representation.
"""
import sys
import pathlib

import matplotlib as mpl
import matplotlib.pyplot as plt


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


mpl.style.use([
ROOT_DIR / 'styles/base.mplstyle',
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
import pathlib

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


ROOT_DIR = pathlib.Path(__file__).parent.parent
sys.path.append(str(ROOT_DIR / "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 @@ -2,6 +2,7 @@
# Matplotlib cheat sheet
# Released under the BSD License
# -----------------------------------------------------------------------------
import sys
import pathlib

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


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


mpl.style.use([
ROOT_DIR / 'styles/base.mplstyle',
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
import pathlib

import matplotlib.pyplot as plt


ROOT_DIR = pathlib.Path(__file__).parent.parent
sys.path.append(str(ROOT_DIR / "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=pathlib.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,3 +1,4 @@
import sys
import pathlib

import matplotlib as mpl
Expand All @@ -6,6 +7,9 @@


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


mpl.style.use([
ROOT_DIR / 'styles/base.mplstyle',
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
import pathlib

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


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

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


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


mpl.style.use([
ROOT_DIR / 'styles/base.mplstyle',
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
import pathlib

import numpy as np
import matplotlib.pyplot as plt


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

import numpy as np
import matplotlib.pyplot as plt


ROOT_DIR = pathlib.Path(__file__).parent.parent
sys.path.append(str(ROOT_DIR / "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 @@ -2,6 +2,7 @@
# Matplotlib cheat sheet
# Released under the BSD License
# -----------------------------------------------------------------------------
import sys
import pathlib

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


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


mpl.style.use([
ROOT_DIR / 'styles/base.mplstyle',
Expand Down
Loading
Loading