python matplotlib 中文显示问题

# -*- coding: UTF-8 -*-
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties

# windows下配置 font 为中文字体,自己去该路径找到自己电脑自带的字体
# font = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=14)

# mac下配置 font 为中文字体,自己去该路径找到自己电脑自带的字体
font = FontProperties(fname='/System/Library/Fonts/STHeiti Medium.ttc')

# Python3可以不加 u,加上 fontproperties 参数即可
plt.xlabel(u'x 轴', fontproperties=font)
plt.ylabel(u'y 轴', fontproperties=font)
plt.title(u'标题' fontproperties=font)

# 注意:plt.legend()有点不同,需要加上 prop 参数
plt.legend((type_1, type_2, type_3), ('类型一', '类型二', '类型三'), loc=4, prop=font)

plt.show()

猜你喜欢

转载自www.cnblogs.com/chenyoude/p/10123033.html
今日推荐