Skip to content

How to set multiple default fonts with matplotlib? #3010

Closed
@mwaskom

Description

@mwaskom

I asked this as a SO question that didn't seem to get any responses.

I don't think this is a bug, but it feels like a bug because I don't understand it :). Feel free to close this and answer over there if you think it is just a misunderstanding.

I want to set the default matplotlib fonts through the rc parameters in a way that is portable to computers that might not have the same set of fonts available.

Here is what I thought would work:

import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.rcParams["font.sans-serif"] = ["Arial", "Liberation Sans", "Bitstream Vera Sans"]
mpl.rcParams["font.family"] = "sans-serif"

ax = plt.subplot()
ax.plot([0, 1, 2], [0, 2, 1])
ax.set_xlabel("the x label")

Because I have Arial installed on my system, I thought that the xlabel would be in Arial. But this approach should mean the default for someone who doesn't have Arial but does have Liberation Sans (i.e. a default Ubuntu system) will be Liberation Sans. And then it should fall back to Bitstream Vera Sans as the default when neither of the two alternate choices are available.

However, this is not what happens. If I do print ax.xaxis.label.get_fontname() it reports Bitstream Vera Sans. Again, I have Arial installed. In other words, I can then do

ax.set_ylabel("the y label", fontdict={"name": "Arial"})
print ax.yaxis.label.get_fontname()

and get Arial.

It feels like I am missing something fundamental about the relationship between the font.family rc parameter and the font.sans-serif parameter when family is "sans serif".

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions