我正在尝试更改与 matplotlib 的注释函数一起出现的文本的字体类型(从 deja-vu 到让我们说 Comic sans 或 calibri)。但是,虽然我可以更改图中所有其他元素的字体,但它不会更改带注释的文本的字体。有什么帮助吗?以下是我正在使用的内容以及如何全局设置字体。比较绘图上“1”的样式时,您会注意到字体的差异。
import numpy as np
import os, glob
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.font_manager
from matplotlib import rcParams
import pdb
matplotlib.font_manager._rebuild()
# THIS SETS ALL THE RELEVANT FONT STYLE FOR PLOTS
rcParams['font.family'] = 'sans-serif'
rcParams['font.weight'] = 'regular' #can omit this, it's the default
rcParams['font.sans-serif'] = ['calibri']
# test code
ax = plt.figure().add_subplot()
x,y = np.arange(10),np.arange(10)
ax.plot(x,y)
ax.annotate(r'$100^\circ$',
xy=(1.1, 0.1), xycoords='axes fraction',
xytext=(1.1, 0.8), textcoords='axes fraction',
arrowprops=dict(facecolor='black', shrink=0.01),
horizontalalignment='center', verticalalignment='top',
fontsize=15)
最佳答案
问题是,它是数学文本...如果您更改常规文本的字体特征,则不会影响 Matplotlib 用于数学文本的字体。
当然你可以修改数学文本中使用的字体(Matplotlib中有什么是你不能修改的吗?)
Writing mathematical expressions
[...] Mathtext can use DejaVu Sans (default), DejaVu Serif, the Computer Modern fonts (from (La)TeX), STIX fonts (with are designed to blend well with Times), or a Unicode font that you provide. The mathtext font can be selected with the customization variable
mathtext.fontset
(see Customizing Matplotlib with style sheets and rcParams).
关于python - Matplotlib注释更改字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62826577/