Skip to content Skip to sidebar Skip to footer

Matplotlib Ticks Sans-serif For All Plots

I'm trying to achieve sans-serif axes labels and ticks in matplotlib with pgf. I want to control that by rcParams so I have it working for all subsequent plots. Currently, I'm usin

Solution 1:

According to the pgf texsystem example, you need to use the "pfg" backend (mpl.use("pgf")) and choose the font you want to use:

style = {
        "pgf.texsystem": "pdflatex",   
        "text.usetex": True,                
        "pgf.preamble": [
         r"\usepackage[utf8x]{inputenc}",
         r"\usepackage[T1]{fontenc}",
         r"\usepackage{cmbright}",
         ]
        }

enter image description here

Alternatively you may use a formatter, which does not format the ticklabels as latex math (i.e. does not put them into dollar signs).

One may adapt the default ScalarFormatter not to use latex math by setting

ax.xaxis.get_major_formatter()._usetex = False
ax.yaxis.get_major_formatter()._usetex = False

Solution 2:

The problem is LateX related. You just need to load the extra cmbright package that enable sans-serif math fonts.

On Debian-like systems:

sudo apt install texlive-fonts-extra

On Fedora:

sudo dnf install texlive-cmbright

Then try your code with this style:

style = {   
        "text.usetex": True,                
        "font.family": "sans-serif""text.latex.preamble" : r"\usepackage{cmbright}"
        }

Post a Comment for "Matplotlib Ticks Sans-serif For All Plots"