python - 如何在 python shell 中检查 matplotlib 的默认字体?

标签 python matplotlib fonts

我尝试了下面的代码,但我只收到了所有 ttf 字体文件的列表。

import matplotlib.font_manager as fm 
for font in fm.findSystemFonts():
    print font

我在想也许我需要使用 matplotlib 中的 findFont() 函数,但我不确定如何使用它。我的目标是能够询问 python matplotlib 使用的默认字体是什么,理想情况下,如果不是 Times New Roman,我想将默认字体更改为 Times New Roman。另外,如果 matplotlib 库不存在 times new roman 那么我如何将默认值更改为下一个最佳衬线字体。

最佳答案

您可以使用rcParams动态修改字体。检查以下代码:

import matplotlib as mpl

mpl.rcParams['font.family'] = ['serif'] # default is sans-serif
mpl.rcParams['font.serif'] = [
           'Times New Roman',
           'Times',
           'Bitstream Vera Serif',
           'DejaVu Serif',
           'New Century Schoolbook',
           'Century Schoolbook L',
           'Utopia',
           'ITC Bookman',
           'Bookman',
           'Nimbus Roman No9 L',
           'Palatino',
           'Charter',
           'serif']

这样,如果您的系统中可用 Times New Roman,matplotlib 将在 session 期间使用它,如果没有,它将尝试使用定义的下一个字体类型。看看here for more info .

如果您想将参数重置为默认值,可以使用:

mpl.rcdefaults()

其他选项是 use a stylesheet使用您想要的设置,您可以将该样式应用于所有绘图,或者使用上下文管理器仅应用于其中一些绘图。

关于python - 如何在 python shell 中检查 matplotlib 的默认字体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35593345/

相关文章:

python - Matplotlib 颜色图范围

python - 使用 python 数据框中的两列(值、计数)绘制直方图

python - 用 Python 绘制方波

html - CSS - 'Open Sans' 字体

ios - 如何修复 iPhone 6 上的模糊文字 (+)

python - 如何使用 Tkinter 移动 Canvas 图像?

javascript - 动态创建 html div jinja2 和 ajax

unicode - 字符集、语言环境、字体和代码页?

python - python 中的 Matlab 长格式

python - 在 python 程序中通过 ctypes 使用具有线程本地存储的共享库时发生内存泄漏