Skip to main content

Easy font management for matplotlib

Project description

mpl_fontkit

pypi

  • Use font from google fonts
  • Set the font and use it easily
  • Helpful error message to get your font name right

Install

pip install mpl_fontkit

Quick Start

from mpl_fontkit import FontKit

# https://fonts.google.com/specimen/Lato?query=Lato
fk = FontKit().get("Lato")

If the font does not available in matplotlib, this will download from Google and add it to matplotlib.

To check available styles for a font

fk.font_table("Lato")
  Name     Style    Variant    Weight   Stretch 
  ----     -----    -------    -----    ------- 
  Lato     normal    normal     250      normal 
  Lato     italic    normal     250      normal 
  Lato     normal    normal     300      normal 
  Lato     italic    normal     300      normal 
  Lato     normal    normal     400      normal 
  Lato     italic    normal     400      normal 
  Lato     normal    normal     700      normal 
  Lato     italic    normal     700      normal 
  Lato     normal    normal     900      normal 
  Lato     italic    normal     900      normal 

And then you are ready to use it in your plots

import matplotlib.pyplot as plt
_, ax = plt.subplots()
ax.set_title("Lato Font", fontdict={"style": "italic", 
                                    "weight": 700, 
                                    "size": 24})
show in plot

To set a font manually. This will update the rcParams for you.

from mpl_fontkit import FontKit
FontKit().set_global("Lato")

Most of the time when you can't get the font to work simply because the font name is not the same as the file name. You can just type a fuzzy name, we will try to find a similar name and show you in the error message.

from mpl_fontkit import FontKit
FontKit().set_global("Lat")
LookupError: Cannot find Lat, do you mean: Lato. Use `.fonts()` to list all the available fonts.

Get available fonts

from mpl_fontkit import FontKit
print(FontKit().fonts())
['Agency FB',
 'Algerian',
 'Arial',
 'Arial Rounded MT Bold',
 'Bahnschrift',
 'Baskerville Old Face',
 'Bauhaus 93',
 ...]

To show what a font looks like

from mpl_fontkit import FontKit
FontKit().show("Lato")
# To display all fonts at once
# FontKit().show_fonts()

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

mpl_fontkit-0.1.0.tar.gz (4.5 kB view hashes)

Uploaded Source

Built Distribution

mpl_fontkit-0.1.0-py3-none-any.whl (5.1 kB view hashes)

Uploaded Python 3

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