Skip to content

unexpected behavior of font.family seleciton #18808

Closed
@IvanAvdeevGth

Description

@IvanAvdeevGth

It seems that matplotlib.rcParams['font.family'] does not work as expected. Here is the source

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import matplotlib as mpl
import matplotlib.pyplot as plt
# setup matplotlib to use xelatex as text engine
mpl.use("pgf")
mpl.rcParams['text.usetex'] = True
mpl.rcParams['pgf.rcfonts'] = False
mpl.rcParams['pgf.texsystem'] = 'xelatex'
mpl.rcParams['pgf.preamble'] = \
    r'\usepackage{mathspec}'+\
    r'\setmainfont{Helvetica}'+\
    r'\setmathsfont(Digits,Latin,Greek)[]{Helvetica}'
def draw_some_text(ff):
    plt.figure(figsize=(4.5, 2.5))
    plt.xlim(0,4.5)
    plt.ylim(0,2.5)
    plt.text(0,0.1,r'normal: abcdefghijklmnopqrstuvwxyz0123456789')
    plt.text(0,0.6,r'$math:~abcdefghijklmnopqrstuvwxyz0123456789$')
    plt.savefig('font_family_%s.pdf'%ff)
    plt.close()
# normal font does not changed
mpl.rcParams['font.family'] = 'sans-serif'
draw_some_text('sans-serif')
# normal font is changed to helvetica
mpl.rcParams['font.family'] = 'serif'
draw_some_text('serif')
# also works
mpl.rcParams['font.family'] = '---'
draw_some_text('---')

What i tried to do is to redirect all the text processing to xelatex to be able to select both math and normal fonts manually. It works, but there are two strange things:

  1. If i use the font family 'sans-serif', then the normal font does not changed to helvetica (in this case), while if i select 'serif' family, everything works just fine.
  2. Everything works fine even if i use 'font.family' = '---' (or any other string), while documentation suggests that only five specific values can be used, see https://matplotlib.org/3.3.2/tutorials/introductory/customizing.html#customizing-with-matplotlibrc-files.

For this example i use python: 3.6.9 [GCC 8.4.0] and matplotlib: 3.3.2.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions