python - Matplotlib注释更改字体

标签 python matplotlib plot fonts annotate

我正在尝试更改与 matplotlib 的注释函数一起出现的文本的字体类型(从 deja-vu 到让我们说 Comic sans 或 calibri)。但是,虽然我可以更改图中所有其他元素的字体,但它不会更改带注释的文本的字体。有什么帮助吗?以下是我正在使用的内容以及如何全局设置字体。比较绘图上“1”的样式时,您会注意到字体的差异。 enter image description here

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/

相关文章:

Python 请求 Post 请求数据与 Django

python - Python 中的泰尔 U 1/泰尔 U 2 预测系数公式

python - 来自已经分箱的数据的直方图,我有分箱和频率值

python - 在刻度之间放置标签

r - R 中的堆积条形图

plot - Matplotlib y 轴限制在设置 x 轴限制后没有更新?

python - 用 moto 模拟 lambda 响应

python - 如何在 Odoo 12 中直接访问看板 View 模板中的 python 方法?

python - 创建五边形或更高的三角形网格

plot - 如何将用户定义的图例附加到 gnuplot 中的现有图例