Data ScienceData Visualization

How to Use Custom Fonts with Matplotlib – In 5 Minutes or Less

Matplotlib visualizations can be an eyesore by default. Lucikly for you, a lot can be tweaked with a couple of lines of code. Today we’ll take a look at changing fonts and explain why it is generally a good idea.

The article is structured as follows:

Custom fonts – Why?

Why not? The default ones won’t get you far. If your company has a distinct branding, why not use it whenever possible? 

Let’s take a look at how default stylings look like. There’s no data visualization without data, so let’s declare a simple dataset first. It contains dummy quarterly sales for an imaginary company:

Here’s how the dataset looks like:

Image 1 - Dummy visualization dataset (image by author)

Image 1 – Dummy visualization dataset (image by author)

Let’s visualize this data with both line and scatter plots. We’ll keep most of the default stylings and just remove the top and right spines:

Here’s the visualization:

Image 2 - Visualization with default fonts (image by author)

Image 2 – Visualization with default fonts (image by author)

As you can see, the fonts look decent, but I don’t know a single brand that uses DejaVu Sans as their font of choice. Let’s see how to change it next.

Using built-in fonts

Matplotlib makes it easy to use fonts installed on your machine. You can use the following code snippet to list the first ten available fonts:

Here are the results:

Image 3 - Preinstalled fonts (image by author)

Image 3 – Preinstalled fonts (image by author)

Remove the `[:10]` if you want the entire list. You can specify the value for the `font` parameter to use a font of interest in either title or the axis labels. 

The following code snippet shows you how to use Courier New font in the title:

Here’s how the visualization looks like: 

Image 4 - Visualization with a preinstalled system font (image by author)

Image 4 – Visualization with a preinstalled system font (image by author)

But did you know there’s no need to install the font before using it? All you need is a TTF file. Let’s explore how that works next.

Using custom fonts from a TTF file

To follow along, please download the Merriweather font from here (or any other). Unzip the file and copy the path to the folder.

From here, we can use the `font_manager` from Matplotlib to add fonts from a file. You’ll want to add the fonts one by one inside the loop.

Once added, we’ll set the entire Matplotlib font family to Merriweather, so we don’t have to specify the font everywhere manually.

Once that is done, you can make the visualization as you usually would:

The results are shown in the following figure:

Image 5 - Visualization with a custom TTF font (image by author)

Image 5 – Visualization with a custom TTF font (image by author)

And that’s how easy it is to add custom fonts to Matplotlib! Let’s wrap things up next.

Conclusion

Today’s article was short but to the point. You’ve learned how to incorporate your branding into data visualizations, an essential skill if you want to send a consistent message across all mediums.

Stay tuned to the blog if you’ve liked this piece – a bunch more similar articles are coming soon.

Learn More

Stay connected

Dario Radečić
Data scientist, blogger, and enthusiast. Passionate about deep learning, computer vision, and data-driven decision making.

You may also like

Comments are closed.

More in Data Science