Solve the problem of Chinese garbled characters in python matplot drawing and the problem of unresponsive plot.show() under Mac system
Reference: https://www.zhihu.com/question/25404709
Most of the tutorials on the web are about Windows. On the mac, the configuration can be as follows:
1. Baidu search font simhei, put it in a
/Users/xxxxx/anaconda/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf
directory similar to that and
click here to download
2. Delete
/Users/lizhen/.matplotlib/fontList.cache
/Users/lizhen/.matplotlib/*.cache最好都删除
3. Modify the configuration file
/Users/lizhen/anaconda/lib/python2.7/site-packages/matplotlib/mpl-data/matplotlibrc
to find font.family and font.sans-serif, remove the # comment, and open it.
font.family : sans-serif
font.sans-serif : SimHei,
4. Code section
import matplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
5. Finally attach the test code, which has a solution to the unresponsiveness of plot.show()
#encoding=utf-8
import matplotlib
matplotlib.use('TkAgg')#这两句用来解决plot.show()后没有反应的问题
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
names = ['5', '10', '15', '20', '25']
x = range(len(names))
y = [0.855, 0.84, 0.835, 0.815, 0.81]
y1=[0.86,0.85,0.853,0.849,0.83]
#plt.plot(x, y, 'ro-')
#plt.plot(x, y1, 'bo-')
#pl.xlim(-1, 11) # 限定横轴的范围
#pl.ylim(-1, 110) # 限定纵轴的范围
plt.plot(x, y, marker='o', mec='r', mfc='w',label=u'y=x^2曲线图')
plt.plot(x, y1, marker='*', ms=10,label=u'y=x^3曲线图')
plt.legend() # 让图例生效
plt.xticks(x, names, rotation=45)
plt.margins(0)
plt.subplots_adjust(bottom=0.15)
plt.xlabel(u"time(s)邻居") #X轴标签
plt.ylabel("RMSE") #Y轴标签
plt.title("A simple plot") #标题
plt.show()