我尝试了下面的代码,但我只收到了所有 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/