Skip to content Skip to sidebar Skip to footer

Userwarning: Findfont: Font Family ['cursive'] Not Found. Falling Back to Dejavu Sans

Over the terminal few years I have made a transition in my workflow from doing near of my data exploration and plotting on my local motorcar (a 2016 MacBook laptop) to doing most of my plotting on a remote HPC (bank check out this other blog mail for more on that). This workflow is very user-friendly for many reasons (keeping all files in one location, more extensibility and power for my notebooks that my personal laptop tin can't handle, minimizing file storage on my local auto, etc. etc.).

However, I could not go any of my font specifications to work in matplotlib (or whatsoever dependent programs) in my notebooks that I was running via ssh tunnel. Period.

I constantly got some variation of findfont: Font family ['sans-serif'] not found. Falling back to DejaVu Sans..:

Error message

This happened both when I directly specified the font in my notebook:

          import matplotlib.pyplot as plt plt.rcParams["font.family"] = "sans-serif"                  

And besides when I modified my matplotlibrc file (more than on that subsequently).

I figured I was simply doing something incorrect and eventually gave up on trying to gear up it and just succumbed to my fate of manually changing fonts post-plotting in Adobe Illustrator. I did this for… several years.

So yesterday, I had an epiphany. My remote HPC probably doesn't take Microsoft fonts installed.

Facepalm

Every bit Arianna said (slacked, really) when I asked for her comments on this blog post: "This is because unlike an OS that nosotros're used to working with that's all set upwards out-of-the-box, HPC admins don't actually have a need to utilise anything only some kind of Courier New."

Too true.

And so, I present to you…

How to specify fonts in matplotlib when you are working on a Linux system:

First off, I highly recommend using conda environments to manage research computational environments– it tin can enhance reproducibility and avoid package conflicts (more than here). For the purposes of this tutorial, I will presume that you have created a conda surround that has matplotlib installed. For the sake of sit-in my environment is called general_plotting.

Conveniently, at that place is a conda package mscorefonts that contains all the "core fonts" that are used for the Spider web. These fonts include: Andale Mono, Arial, Arial Black, Comic Sans MS, Courier New, Georgia, Impact, Times New Roman, Trebuchet MS, Verdana, and Webdings. While not totally comprehensive, it provides enough font variety for my purposes.

After having activated whatever environment you are using, you can install the mscorefonts package.

          conda activate general_plotting conda install -c conda-forge mscorefonts                  

This volition install and make those fonts available to you lot within your environment. We are most washed– simply a few more than steps merely to make it work.

I establish that I had to delete my cached matplotlib binder. If you accept conda installed I believe that information technology should exist located in your home directory; however, if you can't notice it there leave me a comment below.

          rm ~/.cache/matplotlib -rf                  

Now, whatever adjustments you lot want to brand should be fairly straightforward!

At present, you should be able to specify a font in your notebook preamble every bit you wish:

          import matplotlib matplotlib.rcParams['font.family'] = "sans-serif" matplotlib.rcParams['font.sans-serif'] = "Comic Sans MS"                  

The in a higher place would brand all plots in the notebook default to Comic Sans.

You lot should also be able to specify fonts directly on some figure portion (e.g. y-label):

          fig, ax = plt.plot(x,y) #some random variables ax.set_ylabel("Commencement variable", fontname="Arial", fontsize=12)                  

Setting a default font

I generally like all my plots to expect relatively similar. My preferred font is Arial – it is tidy and generally easy to read. If y'all want to specify a default font for all your plots without having to specify the rcParams in each notebook you can modify the matplotlibrc file for your environs. Yous can call back of the matplotlibrc equally similar to your .bashrc – information technology controls some of the basic functionality and preferences for your matplotlib plotting. This file can be a bit buried (especially when using conda environments). 1 easy way to notice your file is to open a notebook within any surroundings y'all are using and type the following:

          import matplotlib print(matplotlib.matplotlib_fname())                  

This command will impress the absolute path to your matplotlibrc.

Within this file, I edited the file such that I uncommented the following lines and made the following specifications:

          font.family:  sans-serif font.sans-serif: Arial                  

Every bit a random bated while you take your matplotlibrc file open and handy, yous should consider changing your pdf.fonttype to TrueType rather than the default Blazon 3. If you become to edit or tinker with a .pdf file sometime down the line, you might be upset to discover that all the fonts have been converted to vectors (or outlined shapes of the fonts that can't be easily contradistinct). Y'all can set your default behavior to be exporting editable TrueType fonts by finding pdf.fonttype in matplotlibrc and uncommenting it and irresolute it as follows:

          pdf.fonttype:       42                  

You can now change your fonts as you delight and not be stuck with DejaVu Sans (a font that I really, really, REALLY dislike).

Happy plotting!

[Thanks to Arianna for commenting on this blog mail for me :)]

bellloverrien1966.blogspot.com

Source: https://alexanderlabwhoi.github.io/post/2021-03-missingfont/

Post a Comment for "Userwarning: Findfont: Font Family ['cursive'] Not Found. Falling Back to Dejavu Sans"