绘制图形时使用了中文标题,会出现乱码:
原因是matplotlib.pyplot在显示时无法找到合适的字体。
先把需要的字体(在系统盘C盘的windows下的fonts目录内)添加到FontProperties中。
具体解决方法如下:
import matplotlib.pyplot as plt
#加入以下代码,导入字体库文件
from matplotlib.font_manager import FontProperties
font_set = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=12)
fig=plt.figure()
self.axes.scatter(np.random.rand(20), np.random.rand(20), color='red', marker='+')
self.axes.scatter(np.random.rand(30), np.random.rand(30), color='green', marker='o')
plt.ylabel(u'平均每餐消费额',fontproperties=font_set)
成功后,样图: