Skip to main content

A simple way to load fonts for matplotlib

Project description

PyFonts

pyfonts logo

A simple way to load fonts for matplotlib.


Check out the online documentation.

Table of content:



Installation

You can install pyfonts directly from PyPI with:

pip install pyfonts

Alternatively you can install the development version with:

pip install git+https://github.com/JosephBARBIERDARNAL/pyfonts.git



Quick start

from pyfonts import load_font
import matplotlib.pyplot as plt

# load font
font = load_font(
    font_url="https://github.com/google/fonts/raw/main/apache/ultra/Ultra-Regular.ttf"
)

# check how the font looks on a minimalist example
fig, ax = plt.subplots(figsize=(10, 6))
ax.text(
    x=0.5,
    y=0.5,
    s=f"What an easy way to load fonts, isn't it?",
    font=font,
    fontsize=20,
    ha="center",
)
plt.show()

output of quick start



How to find fonts?

Google font

There are many fonts available on the web. The easiest way to find one is to follow these steps:

  • Browse Google Font website to find a font that you like. Let's say you want to use Amaranth.
  • Go to Google font github repository and type the name of your desired font in the search bar. We find that Urbanist font file in Bold is named https://github.com/google/fonts/blob/main/ofl/amaranth/Amaranth-Bold.ttf.
  • copy the url and add ?raw=true at the end, which gives us https://github.com/google/fonts/blob/main/ofl/amaranth/Amaranth-Bold.ttf?raw=true
  • and that's it! Just pass this to load_font() to use it in your matplotlib charts
from pyfonts import load_font
import matplotlib.pyplot as plt

font = load_font(
    font_url="https://github.com/google/fonts/blob/main/ofl/amaranth/Amaranth-Bold.ttf?raw=true"
)

fig, ax = plt.subplots(figsize=(10, 6))
ax.text(
    x=0.5,
    y=0.5,
    s=f"Congrats, you now have a cool font!",
    font=font,
    fontsize=20,
    ha="center",
)
plt.show()

For the url to be readable by PyFonts when using a Github url, it must be in one of (the following 3 urls are completely equivalent):

  • https://github.com/google/fonts/blob/main/apache/ultra/Ultra-Regular.ttf?raw=true
  • https://github.com/google/fonts/raw/main/apache/ultra/Ultra-Regular.ttf
  • https://raw.githubusercontent.com/google/fonts/raw/main/apache/ultra/Ultra-Regular.ttf

The recommended is the first (https://github.com/google/fonts/blob/main/apache/ultra/Ultra-Regular.ttf?raw=true) because you just need to add ?raw=true after the end of the Github url.

Other places for fonts

Github is the ideal place to find fonts under a free licence. You can find many fonts on the web. Just make sure that the licence of the font allows you to use it in your project.

You can find other fonts at:



PyFonts and Matplotlib

How it works

In order to work with any font, PyFonts creates a temporary file and uses this file to create a FontProperties object. Once the object has been created with your font, the program deletes the temporary file as it no longer needs it.

Different weight and style

When you load a font, you don't load all its extensions: bold, italic, thin etc, but only the one from the url. If you want to be able to use a font and its bold version, for example, you need to load both fonts:

from pyfonts import load_font
import matplotlib.pyplot as plt

font = load_font(
    font_url="https://github.com/google/fonts/blob/main/ofl/amaranth/Amaranth-Regular.ttf?raw=true"
)
bold_font = load_font(
    font_url="https://github.com/google/fonts/blob/main/ofl/amaranth/Amaranth-Bold.ttf?raw=true"
)

fig, ax = plt.subplots(figsize=(10, 6))
ax.text(
    x=0.5,
    y=0.5,
    s=f"Congrats, you now have a cool font!",
    font=font,
    fontsize=20,
    ha="center",
)
ax.text(x=0.5, y=0.3, s=f"And now it's bold", font=bold_font, fontsize=20, ha="center")
plt.show()

combine regular and bold font



Other feature: download a font locally

If for some reason you want to store the fonts you're working with, simply use the download_font() function. It just needs the arguments font_url (as described above) and destination_path (where you want to store them, by default in the current directory).

You can suppress the output message by adding verbose=False to it.

from pyfonts import download_font

download_font(
    font_url="https://github.com/google/fonts/raw/main/apache/ultra/Ultra-Regular.ttf",
    destination_path="/Users/josephbarbier/Desktop/myfont.ttf",  # optional
)

Font installed at: /Users/josephbarbier/Desktop/myfont.ttf



Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyfonts-0.0.1.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

pyfonts-0.0.1-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file pyfonts-0.0.1.tar.gz.

File metadata

  • Download URL: pyfonts-0.0.1.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.5

File hashes

Hashes for pyfonts-0.0.1.tar.gz
Algorithm Hash digest
SHA256 61306278d602571be41421fd8a93f7820215602c1df047976c9e5e343fd977d0
MD5 43f0c0cca9959342a847172c55c348cd
BLAKE2b-256 5fd5eee41aac890a6067ddfbacc37e9d5a2c6b4f922c93d2ff5099d82c7d87dc

See more details on using hashes here.

File details

Details for the file pyfonts-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: pyfonts-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.5

File hashes

Hashes for pyfonts-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f5a4c9f6121db2bdd6997960e39c409d90bab655f9547f4790262681076d8810
MD5 b93c482d407075ba013a11c32599b474
BLAKE2b-256 2c8ce629d3504735b525a0102e2a5ea35720e82a8c0e5f9c530d925ecd99e1ab

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page