Findfont: Font Family [u'normal'] Not Found. Falling Back to Dejavu Sans

Over the concluding few years I accept made a transition in my workflow from doing almost of my data exploration and plotting on my local machine (a 2016 MacBook laptop) to doing most of my plotting on a remote HPC (cheque out this other blog post for more on that). This workflow is very convenient for many reasons (keeping all files in one location, more than extensibility and power for my notebooks that my personal laptop can't handle, minimizing file storage on my local machine, 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 institute. Falling back to DejaVu Sans..:

Error message

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

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

And as well when I modified my matplotlibrc file (more than on that later).

I figured I was merely doing something wrong and eventually gave upward on trying to fix it and merely succumbed to my fate of manually changing fonts post-plotting in Adobe Illustrator. I did this for… several years.

And then yesterday, I had an epiphany. My remote HPC probably doesn't have Microsoft fonts installed.

Facepalm

As Arianna said (slacked, actually) when I asked for her comments on this weblog post: "This is considering unlike an OS that we're used to working with that'due south all ready up out-of-the-box, HPC admins don't really have a demand to utilize anything but some kind of Courier New."

Likewise true.

So, I nowadays 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 can enhance reproducibility and avoid bundle conflicts (more than here). For the purposes of this tutorial, I volition assume that yous have created a conda environment that has matplotlib installed. For the sake of demonstration my environment is called general_plotting.

Conveniently, there is a conda bundle mscorefonts that contains all the "core fonts" that are used for the 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 non totally comprehensive, it provides plenty font multifariousness for my purposes.

Later having activated whatever environs yous are using, you tin install the mscorefonts parcel.

          conda activate general_plotting conda install -c conda-forge mscorefonts                  

This will install and make those fonts bachelor to you within your environment. We are almost done– just a few more steps merely to make it work.

I found that I had to delete my buried matplotlib folder. If you accept conda installed I believe that it should be located in your home directory; however, if you can't observe information technology there go out me a comment below.

          rm ~/.enshroud/matplotlib -rf                  

Now, any adjustments you want to brand should exist fairly straightforward!

Now, you should exist able to specify a font in your notebook preamble every bit yous wish:

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

The higher up would make all plots in the notebook default to Comic Sans.

You lot should also be able to specify fonts straight on some effigy portion (e.k. y-label):

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

Setting a default font

I generally similar all my plots to look relatively similar. My preferred font is Arial – it is tidy and more often than not like shooting fish in a barrel to read. If you 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. You tin think of the matplotlibrc as similar to your .bashrc – it controls some of the basic functionality and preferences for your matplotlib plotting. This file tin can be a chip buried (especially when using conda environments). One easy fashion to find your file is to open a notebook within whatever environment you 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                  

As a random aside while you have your matplotlibrc file open and handy, you should consider changing your pdf.fonttype to TrueType rather than the default Type 3. If you go to edit or tinker with a .pdf file quondam 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 hands altered). You can set your default behavior to be exporting editable TrueType fonts past finding pdf.fonttype in matplotlibrc and uncommenting it and changing information technology as follows:

          pdf.fonttype:       42                  

Y'all tin can at present alter 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 weblog post for me :)]

mcmilliancaravered44.blogspot.com

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

0 Response to "Findfont: Font Family [u'normal'] Not Found. Falling Back to Dejavu Sans"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel