30

解决Mac中anaconda作图中文异常显示的问题

 3 years ago
source link: http://www.cnblogs.com/plaza-wyj/p/13171400.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

说明

本篇主要针对在MAC系统中Anaconda环境下,matplotlib显示不了中文的问题,提出解决Python绘图时中文显示的方法。

运行环境

macOS Mojave 10.14.6  Python 3.7.4  Anaconda Notebook 6.0.1

问题由来

使用matplotlib时作图显示不出中文,令人很别扭,

N326ziN.png!web

解决方法

1.在访达文件夹中搜索“Fonts”,进入字体资源库,找到Mac内置标准中文字体Arial Unicode,值得注意的是文件名并不是全名。

MfQ7reJ.png!web

2.接下来需要在Anaconda中打印出所有字体全名,并找到中文字体全名为Arial Unicode MS。

代码如下:

import matplotlib
font_list=sorted([f.name for f in matplotlib.font_manager.fontManager.ttflist])
for i in font_list:
    print (i)

结果如下:

aueeE3A.png!web

3.程序加载自符库

代码如下:

plt.rcParams['font.family'] = ['Arial Unicode MS'] #正常显示中文 
plt.rcParams['font.sans-serif'] = ['KaiTi']#作图的中文
plt.rcParams['font.serif'] = ['KaiTi']#作图的中文
plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题

最终结果

7nAZNzM.png!web


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK