Matplotlib 发出警告消息因为findfont- python
最近在使用Matplotlib生成图表时,遇到了一个问题:Matplotlib总是给出以下警告消息:
UserWarning: findfont: Font family [‘sans-serif’] not found. Falling back to DejaVu Sans.
prop.set_family(family)
这种情况发生的原因是Matplotlib默认使用sans-serif字体,当该字体不存在于系统中时,使用备用字体DejaVu Sans。
阅读更多:Matplotlib 教程
了解字体配置
在Matplotlib中,字体配置很重要。您可以在配置文件中指定字体或使用rcParams来进行配置。
例如:
import matplotlib as mpl
mpl.rcParams['font.family'] = 'serif'
mpl.rcParams['font.serif'] = 'Times, Palatino, New Century Schoolbook, Bookman, Computer Modern Roman'
mpl.rcParams['font.sans-serif'] = 'Arial, Helvetica, sans-serif'
这里,我们将mpl.rcParams[‘font.family’]设置为serif。然后,我们在mpl.rcParams[‘font.serif’]中指定可用于serif字体的字体。类似地,我们在mpl.rcParams[‘font.sans-serif’]中指定sans-serif字体。所有这些字体都是从配置文件的“fontList.cache”中提取的。
解决字体配置问题
解决字体配置问题也很简单。只需按照以下步骤进行操作:
- 安装所需字体,例如,我们希望使用中文字体SimHei,则可以搜索并下载“SimHei.ttf”字体;
- 将字体文件放在某个目录下,并修改matplotlib配置文件matplotlibrc,可以在控制台输入以下命令查找配置文件路径:
import matplotlib
print(matplotlib.matplotlib_fname())
将以下部分的注释取消:
#font.family : sans-serif
#font.style : normal
#font.variant : normal
#font.weight : medium
#font.stretch : normal
#font.serif : DejaVu Serif, Bitstream Vera Serif, Computer Modern Roman, New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif
#font.sans-serif : DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Arial, Helvetica, Avant Garde, sans-serif
#font.cursive : Apple Chancery, Textile, Zapf Chancery, Sand, cursive
#font.fantasy : Comic Sans MS, Chicago, Charcoal, Impact, Western, fantasy
#font.monospace : Bitstream Vera Sans Mono, Computer Modern Typewriter, Andale Mono, Nimbus Mono L, Courier New, Courier, Fixed, Terminal, monospace
修改为:
font.family : sans-serif
font.style : normal
font.variant : normal
font.weight : medium
font.stretch : normal
font.serif : SimHei, DejaVu Serif, Bitstream Vera Serif, Computer Modern Roman, New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif
font.sans-serif : SimHei, DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Arial, Helvetica, Avant Garde, sans-serif
font.cursive : Apple Chancery, Textile, Zapf Chancery, Sand, cursive
font.fantasy : Comic Sans MS, Chicago, Charcoal, Impact, Western, fantasy
font.monospace : Bitstream Vera Sans Mono, Computer Modern Typewriter, Andale Mono, Nimbus Mono L, Courier New, Courier, Fixed, Terminal, monospace
示例1
下面是一个使用SimHei字体的简单示例:
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
# 使用SimHei
font = FontProperties(fname=r"/Library/Fonts/SimHei.ttf", size=14)
# 简单绘图
x = [1, 2, 3, 4, 5]
y = [2.3, 3.4, 1.2, 6.6, 7.plt.plot(x, y, label="图表", color="red")
plt.xlabel("x轴", fontproperties=font)
plt.ylabel("y轴", fontproperties=font)
plt.title("Matplotlib测试", fontproperties=font)
plt.legend()
plt.show()
示例2
下面是另一个使用中文显示的示例,这里使用的是微软雅黑字体:
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
# 导入微软雅黑字体
path = 'C:/Windows/Fonts/msyh.ttc'
fontprop = fm.FontProperties(fname=path)
# 数据
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y1 = [1, 2, 3, 4, 2, 1, 0, 4, 2, 1]
y2 = [2, 3, 2, 1, 5, 7, 2, 4, 3, 3]
# 图像属性设置
plt.plot(x, y1, 'ro-', label='图1')
plt.plot(x, y2, 'g^--', label='图2')
plt.xlabel('X-轴', fontproperties=fontprop)
plt.ylabel('Y-轴', fontproperties=fontprop)
plt.title('图1和图2', fontproperties=fontprop)
plt.legend()
plt.show()
总结
Matplotlib是Python中最流行的图表库之一,也是数据可视化的首选库之一。然而,由于字体配置问题而导致的警告信息可能会引起不必要的困扰,因此对字体配置应该予以重视。使用以上方法,可以很好地解决字体配置问题。