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/

相关文章:

java - 在 Java 中呈现 Sinhala unicode 字体

python - 如何使用 pandas 在 Excel 工作表中的同一个图形上绘制多个数据框?

python - 如何为极坐标图中的圆形线着色(matplotlib)

python - matplotlib/ Pandas : put line label along the plotted lines in time series plot

android - 在 android webview 中显示 Malayalam Unicode 字体

html - 为什么 Chrome 使用相同的 CSS 来使用不同的字体?

python - 使用堆栈评估中缀表达式时出现算术错误评估和 KeyDict 错误;

python - pandas 数据框中的过滤

python - 在 DRF 中使用 HyperlinkedModelSerializer 有什么好处?

python - python中使用的存储大量数组