python matplotlib 显示中文问题

查了很多方法,最终算是有一个可以解决的了。。。
我的环境ubuntu16.04,python3.5
1、首先下从网上下载宋体字体(直接google“宋体下载”)或者从window系统中拷贝simsun.ttc文件;
2、把上面的文件放在“/python3.5/site-packages/matplotlib/mpl-data/fonts/ttf/simsun.ttc” (不同系统可能matplotlib路径不同)
然后直接贴代码如下:

 matplotlib import pyplot as plt
import numpy as np

from matplotlib.font_manager import FontProperties

font = FontProperties(fname=r"py3.5/lib/python3.5/site-packages/matplotlib/mpl-data/fonts/ttf/simsun.ttc", size=14)


words = [ '苹果',  '香蕉',  '番茄酱',  '番茄汁','橘子']
vectors = np.array([[0.1,   0.3], 
                    [-0.5, -0.1], 
                    [0.2,   0.2],
                    [0.15, 0.2],
                    [-0.3, -0.2],
                    [-0.25,-0.15]])

plt.plot(vectors[:,0], vectors[:,1], 'o')
plt.xlim(-0.6, 0.3)
plt.ylim(-0.3, 0.5)
for word, x, y in zip(words, vectors[:,0], vectors[:,1]):
    plt.annotate(word, (x, y), size=16, fontproperties=font) #此处将设置好的字体属性设为参数
    print(word)
plt.show()

Reference:
matplotlib绘图中文显示问题

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容