文章目录
在默认状态下,matplotlib无法在图表中使用中文。
1、错误信息
font = get_font(fname)
File "D:\software\Python3.8\lib\site-packages\matplotlib\font_manager.py", line 1341, in get_font
return _get_font(filename, hinting_factor)
FileNotFoundError: [Errno 2] No such file or directory: '/Library/Fonts/Songti.ttc'
错误原因
上面代码主要错误原因是因为Windows下的字体文件路径使用错误。
2、解决方法
📌📌matplotlib本身是支持Unicode的,但是默认情况下matplotlib使用自带的字体,而自带的字体中没有中文字体。这样要它支持中文的思路就有了——给maplotlib添加中文字体。
📢 法1: 查询Windows下可用的所以字体
from matplotlib import font_manager
a= sorted([f.name for f in font_manager.fontManager.ttflist])
for i in a:
print(i)
显示的所有字体:
Arial
Arial
Arial
Arial
Arial
Bahnschrift
Calibri
Calibri
Calibri
Calibri
Calibri
Calibri
Cambria
Cambria
Cambria
Cambria
Candara
Candara
Candara
Candara
Candara
Candara
Comic Sans MS
Comic Sans MS
Comic Sans MS
Comic Sans MS
Consolas
Consolas
Consolas
Consolas
Constantia
Constantia
Constantia
Constantia
Corbel
Corbel
Corbel
Corbel
Corbel
Corbel
Courier New
Courier New
Courier New
Courier New
DejaVu Sans
DejaVu Sans
DejaVu Sans
DejaVu Sans
DejaVu Sans Display
DejaVu Sans Mono
DejaVu Sans Mono
DejaVu Sans Mono
DejaVu Sans Mono
DejaVu Serif
DejaVu Serif
DejaVu Serif
DejaVu Serif
DejaVu Serif Display
DengXian
DengXian
DengXian
Ebrima
Ebrima
FangSong
FangSong_GB2312
Franklin Gothic Medium
Franklin Gothic Medium
Gabriola
Gadugi
Gadugi
Georgia
Georgia
Georgia
Georgia
HYZongYiJ
HoloLens MDL2 Assets
Impact
Ink Free
Javanese Text
KaiTi
KaiTi_GB2312
Leelawadee UI
Leelawadee UI
Leelawadee UI
Lucida Console
Lucida Sans Unicode
MS Gothic
MV Boli
Malgun Gothic
Malgun Gothic
Malgun Gothic
Marlett
Microsoft Himalaya
Microsoft JhengHei
Microsoft JhengHei
Microsoft JhengHei
Microsoft New Tai Lue
Microsoft New Tai Lue
Microsoft PhagsPa
Microsoft PhagsPa
Microsoft Sans Serif
Microsoft Tai Le
Microsoft Tai Le
Microsoft YaHei
Microsoft YaHei
Microsoft YaHei
Microsoft Yi Baiti
MingLiU
MingLiU-ExtB
Mongolian Baiti
Myanmar Text
Myanmar Text
Nirmala UI
Nirmala UI
Nirmala UI
Palatino Linotype
Palatino Linotype
Palatino Linotype
Palatino Linotype
STIXGeneral
STIXGeneral
STIXGeneral
STIXGeneral
STIXNonUnicode
STIXNonUnicode
STIXNonUnicode
STIXNonUnicode
STIXSizeFiveSym
STIXSizeFourSym
STIXSizeFourSym
STIXSizeOneSym
STIXSizeOneSym
STIXSizeThreeSym
STIXSizeThreeSym
STIXSizeTwoSym
STIXSizeTwoSym
STXihei
Segoe MDL2 Assets
Segoe Print
Segoe Print
Segoe Script
Segoe Script
Segoe UI
Segoe UI
Segoe UI
Segoe UI
Segoe UI
Segoe UI
Segoe UI
Segoe UI
Segoe UI
Segoe UI
Segoe UI
Segoe UI
Segoe UI Emoji
Segoe UI Historic
Segoe UI Symbol
SimHei
SimSun
SimSun-ExtB
Sitka Small
Sitka Small
Sitka Small
Sitka Small
Sylfaen
Symbol
Tahoma
Tahoma
Times New Roman
Times New Roman
Times New Roman
Times New Roman
Trebuchet MS
Trebuchet MS
Trebuchet MS
Trebuchet MS
Verdana
Verdana
Verdana
Verdana
Webdings
Wingdings
YouYuan
Yu Gothic
Yu Gothic
Yu Gothic
Yu Gothic
cmb10
cmex10
cmmi10
cmr10
cmss10
cmsy10
cmtt10
然后设置中文显示:
plt.rcParams['font.sans-serif']=['SimHei'] #解决中文显示
plt.rcParams['axes.unicode_minus'] = False #解决符号无法显示
📢 法2: 使用matplotlib的字体管理器指定字体文件
matplotlib中有一个字体管理器——matplotlib.Font_manager,通过该管理器的方法——matplotlib.Font_manager.FontProperties(fname)可以指定一个ttf字体文件作为图表使用的字体。这样,只要我们传入Unicode字符串,我们就可以想用什么字体就用什么字体了。
首先查看Windows下的所有字体文件:
设置成想使用的中文字体:
import matplotlib.font_manager as fm
import matplotlib.pyplot as plt
# fname字体文件的路径
font = fm.FontProperties(fname=r'C:\WINDOWS\Fonts\simhei.ttf')
验证
法1:未使用。
法2 :已经验证:成功。
3、windows/linux/mac下的系统字体目录位置
from matplotlib import font_manager
font = font_manger.FontProperties(fname='')
实例化FontProperties,其中参数fname=“字体目录”:
- 📢Mac os下的系统字体目录为:
/System/Library/Fonts/
- 📢Linux下的系统字体目录为:
/usr/share/fonts
- 📢Windows下的系统字体目录为:
C:\WINDOWS\Fonts