在Python的matplotlib.pyplot图表中显示中文
2018-02-08 11:27
591 查看
python的str默认是ascii编码,和unicode编码冲突,因此经常会报以下错误:
UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe5 in position 108: ordinal not in range(128)
解决这个问题很简单,只要在代码加入以下语句:
import sys
reload(sys)
sys.setdefaultencoding('utf8')
代码是不报错了,但如果使用matplotlib.pyplot进行绘图时,图表的标题等位置带中文的话,还是会显示为乱码。
可以在代码中加入以下语句,即可顺利带上中文:
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False
UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe5 in position 108: ordinal not in range(128)
解决这个问题很简单,只要在代码加入以下语句:
import sys
reload(sys)
sys.setdefaultencoding('utf8')
代码是不报错了,但如果使用matplotlib.pyplot进行绘图时,图表的标题等位置带中文的话,还是会显示为乱码。
可以在代码中加入以下语句,即可顺利带上中文:
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False
相关文章推荐
- Linux下python matplotlib.pyplot在图像上显示中文的问题
- Linux下python matplotlib.pyplot在图像上显示中文的问题
- python matplotlib.pyplot 显示中文title等参数
- python matplotlib.pyplot 显示中文title等参数
- python 1: 解决linux系统下python中的matplotlib模块内的pyplot输出图片不能显示中文的问题
- 解决python27 matplotlib 中文不显示问题
- Python matplotlib画图的中文显示问题
- python中matplotlib绘图中文显示问题
- python matplotlib中文显示乱码解决
- Python 3下使用Matplotlib工具画图,中文显示乱码的问题解决(修改配置文件)
- 解决python27 matplotlib 中文不显示问题
- python matplotlib中文显示问题
- python matplotlib 中文显示参数设置
- python matplotlib 显示中文
- 解决Linux系统中python matplotlib画图的中文显示问题
- 解决python27 matplotlib 中文不显示问题
- python tips;matplotlib 显示中文
- python matplotlib 中文显示参数设置
- python matplotlib中文显示问题
- python matplotlib中文显示参数设置解析