python matplotlib中文显示问题

来源:互联网 发布:手柄助手映射软件 编辑:程序博客网 时间:2024/05/16 16:06

Python中的matplotlib仅支持Unicode编码,默认是不显示中文的,如果让其默认显示中文,可进行如下配置:

1、在Python的安装目录中找到配置文件: %Python_Home%\Lib\site-packages\matplotlib\mpl-data\matplotlibrc    (如,我的是在C:\Python34\Lib\site-packages\matplotlib\mpl-data),用任意文本编辑器打开。

2、找到139行的font.family         : sans-serif将其前面的#注释号去掉

3、找到151行的font.sans-serif     :AR PL UMing CN, SimHei, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif将【AR PL UMing CN, SimHei】添加在最前面,其中AR PL UMing CN代表:宋体。SimHei代表:黑体。并将前面的#注释号去掉,重启编辑器后,便可显示中文了。

4、同时需要更改264行的axes.unicode_minus  : False;使其值为False;否则无法显示负号

5、代码如下:

import matplotlib.pyplot as plt

plt.xlabel('x轴')

plt.ylabel('y轴')

plt.bar(left = (0,1),height =(1,0.5),width = 0.35)

plt.show()

【注:低版本中x.label(u'x轴');中文前需要加u;请注意】


[python] view plain copy
  1. import matplotlib.pyplot as plt  
  2.   
  3. decisionNode = dict(boxstyle="sawtooth" , fc="0.8")  
  4.   
  5. leafNode = dict(boxstyle="round4",fc="0.8")  
  6.   
  7. arrow_args=dict(arrowstyle="<-")  
  8.   
  9.   
  10. def plotNode(nodeTxt,centerPt,parentPt,nodeType):  
  11.     createPlot.ax1.annotate(nodeTxt,xy=parentPt,xycoords='axes fraction',\  
  12.     xytext=centerPt,textcoords='axes fraction',\  
  13.     va="center",ha="center",bbox=nodeType,arrowprops=arrow_args)  
  14.   
  15.   
  16. def createPlot():  
  17.     fig=plt.figure(1,facecolor='white')  
  18.     fig.clf()  
  19.     createPlot.ax1=plt.subplot(1,1,1,frameon=False)  
  20.     plotNode(U'决策节点',(0.5,0.1),(0.1,0.5),decisionNode)  
  21.     plotNode(u'叶节点',(0.8,0.1),(0.3,0.8),leafNode)  
  22.     plt.show()  
  23.    
  24. createPlot()     

原创粉丝点击