【问题标题】:Matplotlib annotate change fontMatplotlib 注释更改字体
【发布时间】:2020-07-10 02:46:48
【问题描述】:

我正在尝试更改与 matplotlib 的注释功能一起出现的文本的字体类型(从似曾相识到让我们说 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)

【问题讨论】:

    标签: python matplotlib plot fonts annotate


    【解决方案1】:

    问题是,它是一个数学文本……如果你改变 regular 文本的字体特征,你不会影响 Matplotlib 用于数学文本的字体。

    当然你可以修改数学文本中使用的字体(Matplotlib中有什么是你不能修改的吗?)

    Writing mathematical expressions

    [...] Mathtext 可以使用 DejaVu Sans(默认)、DejaVu Serif、Computer Modern 字体(来自 (La)TeX)、STIX 字体(旨在与 Times 很好地融合)或 Unicode您提供的字体。可以使用自定义变量mathtext.fontset(参见Customizing Matplotlib with style sheets and rcParams)选择数学文本字体。

    【讨论】:

      猜你喜欢
      • 2020-09-14
      • 2018-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-09
      • 1970-01-01
      • 1970-01-01
      • 2018-08-15
      相关资源
      最近更新 更多