Last active
March 15, 2019 05:37
-
-
Save LiuQixuan/18ab5c9a8e1387c228e70ba0b5d01361 to your computer and use it in GitHub Desktop.
matplotlib 能用的中文字体
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| goodfont=[ | |
| 'Adobe Heiti Std', | |
| 'Arial Unicode MS', | |
| 'DengXian', | |
| 'SimHei', | |
| 'STKaiti', | |
| 'STXihei', | |
| ] | |
| 程序中有效: | |
| import numpy as np | |
| from matplotlib import pyplot as plt | |
| import matplotlib | |
| zh_cn_font = matplotlib.font_manager.FontProperties(fname="F:\ProjectFiles\SimHei.ttf") | |
| #第七行的作用是为了消除更换为unicode字体之后0、负数之类的显示异常。之后所有使用中文字体的地方只字符串都以u""的形式出现,并指定fontproperties属性为我们的指定的myfont就行了 | |
| matplotlib.rcParams['axes.unicode_minus'] = False | |
| #matplotlib.rcParams['font.sans-serif'] = ['SimHei'] | |
| x = np.arange(0,9) | |
| y = 2*x+1 | |
| plt.title(u"Matplotlib 标题",fontproperties = zh_cn_font) | |
| plt.xlabel(u"x 轴:",fontproperties = zh_cn_font) | |
| plt.ylabel(u"y 轴:",fontproperties = zh_cn_font) | |
| plt.plot(x,y) | |
| plt.show() | |
| 本地有效,一劳永逸法: | |
| 1. 找到matplotlib 配置文件:import matplotlib | |
| print(matplotlib.matplotlib_fname()) | |
| # 我自己的输出结果如下: | |
| # D:\Program Files\Python36\Lib\site-packages\matplotlib\mpl-data | |
| 2. 编辑器打开此文件 matplotlibrc删除font.family和font.sans-serif两行前的#,并在font.sans-serif后添加微软雅黑字体Microsoft YaHei | |
| 3. 下载字体:msyh.ttf (微软雅黑)放在matplotlib 字体文件夹下:# D:\Program Files\Python36\Lib\site-packages\matplotlib\mpl-data\fonts\ttf | |
| 4. 删除.matplotlib/cache里面的两个缓存字体文件C:\Users\你的用户名\.matplotlib<img src="https://pic4.zhimg.com/50/v2-92f9f704ea4e9f440b57d3074da29db4_hd.jpg" data-rawwidth="254" data-rawheight="56" class="content_image" width="254"/> | |
| 5. 重启Python |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment