Python 中,matplotlib绘图无法显示中文的问题
2017-11-09 09:46
1026 查看
尊重原创,转载自http://blog.csdn.net/u013038499/article/details/52449768
在python中,默认情况下是无法显示中文的,如下代码:
[python]
view plain
copy
print?
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
print?
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']
源代码修改如下:
[python]
view plain
copy
print?
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()
成功!
在python中,默认情况下是无法显示中文的,如下代码:
[python]
view plain
copy
print?
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()
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
print?
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']
from pylab import * mpl.rcParams['font.sans-serif'] = ['SimHei']
源代码修改如下:
[python]
view plain
copy
print?
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()
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()
成功!
相关文章推荐
- Python使用matplotlib绘图无法显示中文问题的解决方法
- Python中,matplotlib绘图无法显示中文的问题
- Python 中,matplotlib绘图无法显示中文的问题
- Python 中,matplotlib绘图无法显示中文的问题
- python中matplotlib绘图中文显示问题
- Python绘图问题:Matplotlib中如何正确显示中文
- python中matplotlib绘图中文显示问题
- python中matplotlib绘图中文显示问题
- 解决mac上matplotlib(plot)无法显示中文的问题 (Python)
- python中matplotlib绘图中文显示问题
- 解决python中matplotlib绘图中文显示问题
- python中matplotlib绘图中文显示问题
- python番外(1)——matplotlib中文显示问题
- python matplotlib绘图时图例显示问题
- python matplotlib中显示中文问题
- 解决python27 matplotlib 中文不显示问题
- 解决windows下 Python中 matplotlib 做图中文不显示的问题
- 快捷改变matplotlib绘图中不能正常显示中文的问题
- python matplotlib绘图时图例显示问题
- 解决Windows系统下python利用matplotlib绘图时中文乱码的问题