標籤:決策 creat ota span ref ax1 erp mat detail
在python中,預設情況下是無法顯示中文的,如下代碼:
[python] view plain copy
- 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()
得到映像如下:
產生中文亂碼的原因就是字型的預設設定中並沒有中文字型,所以我們只要手動添加中文字型的名稱就可以了
手動增加如下代碼
[python] view plain copy
- from pylab import *
- mpl.rcParams[‘font.sans-serif‘] = [‘SimHei‘]
原始碼修改如下:
[python] view plain copy
- 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()
最終得到映像
成功!
原文連結:http://blog.csdn.net/u013038499/article/details/52449768
【轉】 Python 中,matplotlib繪圖無法顯示中文的問題