Python 中,matplotlib绘图无法显示中文的问题

尊重原创,转载自http://blog.csdn.net/u013038499/article/details/52449768
在python中,默认情况下是无法显示中文的,如下代码:
  1. import matplotlib.pyplot as plt  
  2.   
  3. # 定义文本框和箭头格式  
  4. decisionNode = dict(boxstyle = "sawtooth", fc = "0.8")  
  5. leafNode = dict(boxstyle = "round4", fc = "0.8")  
  6. arrow_args = dict(arrowstyle = "<-")  
  7.   
  8. # 绘制带箭头的注解  
  9. def plotNode(nodeTxt, centerPt, parentPt, nodeType) :  
  10.     createPlot.ax1.annotate(nodeTxt, xy = parentPt, xycoords = 'axes fraction', xytext = centerPt, textcoords = 'axes fraction', va = 'center', ha = 'center', bbox = nodeType, arrowprops = arrow_args)  
  11.   
  12. def createPlot() :  
  13.     fig = plt.figure(1, facecolor='white')  
  14.     fig.clf()  
  15.     createPlot.ax1 = plt.subplot(111, frameon = False)  
  16.     plotNode(U'决策节点', (0.50.1), (0.10.5), decisionNode)  
  17.     plotNode(U'叶节点', (0.80.1), (0.30.8), leafNode)  
  18.     plt.show()  
  19.   
  20. createPlot()  
import matplotlib.pyplot as plt

# 定义文本框和箭头格式
decisionNode = dict(boxstyle = "sawtooth", fc = "0.8")
leafNode = dict(boxstyle = "round4", fc = "0.8")
arrow_args = dict(arrowstyle = "<-")

# 绘制带箭头的注解
def plotNode(nodeTxt, centerPt, parentPt, nodeType) :
	createPlot.ax1.annotate(nodeTxt, xy = parentPt, xycoords = 'axes fraction', xytext = centerPt, textcoords = 'axes fraction', va = 'center', ha = 'center', bbox = nodeType, arrowprops = arrow_args)

def createPlot() :
	fig = plt.figure(1, facecolor='white')
	fig.clf()
	createPlot.ax1 = plt.subplot(111, frameon = False)
	plotNode(U'决策节点', (0.5, 0.1), (0.1, 0.5), decisionNode)
	plotNode(U'叶节点', (0.8, 0.1), (0.3, 0.8), leafNode)
	plt.show()

createPlot()

得到图像如下:


产生中文乱码的原因就是字体的默认设置中并没有中文字体,所以我们只要手动添加中文字体的名称就可以了

手动增加如下代码

  1. from pylab import *  
  2. mpl.rcParams['font.sans-serif'] = ['SimHei']  
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']

源代码修改如下:

  1. import matplotlib.pyplot as plt  
  2.   
  3. from pylab import *  
  4. mpl.rcParams['font.sans-serif'] = ['SimHei']  
  5.   
  6. # 定义文本框和箭头格式  
  7. decisionNode = dict(boxstyle = "sawtooth", fc = "0.8")  
  8. leafNode = dict(boxstyle = "round4", fc = "0.8")  
  9. arrow_args = dict(arrowstyle = "<-")  
  10.   
  11. # 绘制带箭头的注解  
  12. def plotNode(nodeTxt, centerPt, parentPt, nodeType) :  
  13.     createPlot.ax1.annotate(nodeTxt, xy = parentPt, xycoords = 'axes fraction', xytext = centerPt, textcoords = 'axes fraction', va = 'center', ha = 'center', bbox = nodeType, arrowprops = arrow_args)  
  14.   
  15. def createPlot() :  
  16.     fig = plt.figure(1, facecolor='white')  
  17.     fig.clf()  
  18.     createPlot.ax1 = plt.subplot(111, frameon = False)  
  19.     plotNode(U'决策节点', (0.50.1), (0.10.5), decisionNode)  
  20.     plotNode(U'叶节点', (0.80.1), (0.30.8), leafNode)  
  21.     plt.show()  
  22. createPlot()  
import matplotlib.pyplot as plt

from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']

# 定义文本框和箭头格式
decisionNode = dict(boxstyle = "sawtooth", fc = "0.8")
leafNode = dict(boxstyle = "round4", fc = "0.8")
arrow_args = dict(arrowstyle = "<-")

# 绘制带箭头的注解
def plotNode(nodeTxt, centerPt, parentPt, nodeType) :
	createPlot.ax1.annotate(nodeTxt, xy = parentPt, xycoords = 'axes fraction', xytext = centerPt, textcoords = 'axes fraction', va = 'center', ha = 'center', bbox = nodeType, arrowprops = arrow_args)

def createPlot() :
	fig = plt.figure(1, facecolor='white')
	fig.clf()
	createPlot.ax1 = plt.subplot(111, frameon = False)
	plotNode(U'决策节点', (0.5, 0.1), (0.1, 0.5), decisionNode)
	plotNode(U'叶节点', (0.8, 0.1), (0.3, 0.8), leafNode)
	plt.show()
createPlot()
最终得到图像


成功!

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值