Ubuntu 中使用 matplotlib 画图如何正常显示中文

  • 首先,下载字体:

mcdx@ubuntu:~$ sudo apt-get install ttf-wqy-zenhei      # 文泉驿-正黑
mcdx@ubuntu:~$ sudo apt-get install ttf-wqy-microhei    # 文泉驿-微米黑
mcdx@ubuntu:~$ sudo apt-get install xfonts-wqy          # 文泉驿-点阵宋体
  ... ...
  • 查看系统中都有哪些中文字体:

其中带 CJK 的都是中日韩通用的字体

mcdx@ubuntu:~$ fc-list :lang=zh-cn

/usr/share/fonts/truetype/arphic/uming.ttc: AR PL UMing TW MBE:style=Light
/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc: Noto Sans CJK JP ...
/usr/share/fonts/truetype/arphic/ukai.ttc: AR PL UKai CN:style=Book
/usr/share/fonts/truetype/arphic/ukai.ttc: AR PL UKai HK:style=Book
/usr/share/fonts/truetype/arphic/ukai.ttc: AR PL UKai TW:style=Book
/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc: Noto Sans Mono CJK KR ...
/usr/share/fonts/opentype/noto/NotoSansCJK-Black.ttc: Noto Sans CJK TC ...
/usr/share/fonts/opentype/noto/NotoSansCJK-Black.ttc: Noto Sans CJK KR ...
/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc: Noto Sans Mono CJK JP ...
/usr/share/fonts/opentype/noto/NotoSansCJK-Medium.ttc: Noto Sans CJK JP ...
/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc: Noto Sans CJK JP ...
/usr/share/fonts/opentype/noto/NotoSansCJK-Light.ttc: Noto Sans CJK KR,Noto ...
/usr/share/fonts/X11/misc/wenquanyi_13px.pcf: WenQuanYi Bitmap ...
/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc: 文泉驿正黑,文泉驛正黑 ...
/usr/share/fonts/X11/misc/wenquanyi_12pt.pcf: WenQuanYi Bitmap Song:style=Regular
/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc: 文泉驿点阵正黑,文泉驛點陣正黑 ...
  ... ...
  • 举个栗子:

使用文泉驿的字体,由上面的最后一行得知字体路径为
/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc

In [1]: import matplotlib
   ...: from matplotlib import pyplot as plt
   ...: myfont = matplotlib.font_manager.FontProperties(
   ...:     fname='/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc'
   ...: )
   ...: fig = plt.figure()
   ...: ax = fig.add_subplot(1,1,1)
   ...: ax.set_title(u'试一下文泉驿的字体', fontproperties=myfont)
   ...: fig.show()
   ...: 
正常显示中文~

你可能感兴趣的:(Ubuntu 中使用 matplotlib 画图如何正常显示中文)