参考链接:
https://blog.csdn.net/Richlander/article/details/107240611
https://stackoverflow.com/questions/51810908/custom-fonts-in-google-colaboratory-matplotlib-charts
https://colab.research.google.com/drive/15kcRpv9wkLO2vxWux_gzobVcPuIoJ14r#scrollTo=NOCOwFP9Pnmq
findfont: Font family ['New Times Roman'] not found. Falling back to DejaVu Sans.
如果按照网上搜到的一些方法进行设置,总是会出现以上的问题。
先导入用到的包:
import matplotlib as mpl
import matplotlib.font_manager as fm
import matplotlib.pyplot as plt
在Ubuntu中,可以去网上下载Times.ttf字体文件。我使用在线下载过该文件,但是我没有找到,后来通过查询相关博客,在
'/usr/local/share/fonts/' 这个目录下找到Times.ttf字体文件。
至此,还需作如下操作:
1. 创建新的字体列表
fm.fontManager.ttflist += fm.createFontList(['/usr/local/share/fonts/Times.ttf'])
2. 配置rc参数应用字体
mpl.rc('font', family='serif')
这里的family参数,如果设置为serif报错,则报错找不到什么就填写什么。
例如:报错如下
findfont: Font family ['serif'] not found. Falling back to DejaVu Sans.
即填写serif。
接下来就可以正常使用matplotlib了。