初学python,安装anaconda集成环境,使用matplotlib库绘图,标题和标签中文显示不出来,英文可以。
一、前后结果对比
import sys
from matplotlib import pyplot as plt
years = [1960,1960,1970,1980,1990,2000,2010]
gdp = [300.2,543.3,1075.9,2862.5,5979.6,10289.7,14958.3]
plt.plot(years,gdp,color='green',marker='o',linestyle='solid')
plt.title("名义GDP")
plt.ylabel("十亿美元")
plt.show()
报错如下:
Traceback (most recent call last):
File "d:\ProgramData\Anaconda2\lib\site-packages\IPython\core\interactiveshell.py", line 2898, in run_code
self.showtraceback()
File "d:\ProgramData\Anaconda2\lib\site-packages\IPython\core\interactiveshell.py", line 1824, in showtraceback
value, tb, tb_offset=tb_offset)
File "d:\ProgramData\Anaconda2\lib\site-packages\IPython\core\ultratb.py", line 1412, in structured_traceback
self, etype, value, tb, tb_offset, number_of_lines_of_context)
File "d:\ProgramData\Anaconda2\lib\site-packages\IPython\core\ultratb.py", line 1329, in structured_traceback
self, etype, value, elist, tb_offset, number_of_lines_of_context
File "d:\ProgramData\Anaconda2\lib\site-packages\IPython\core\ultratb.py", line 644, in structured_traceback
out_list.extend(self._format_list(elist))
File "d:\ProgramData\Anaconda2\lib\site-packages\IPython\core\ultratb.py", line 682, in _format_list
item += ' %s\n' % line.strip()
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 11: ordinal not in range(128)
更改两行代码,改变编码,就可以了。这是解决方法的出处:
http://www.cnblogs.com/bricker/p/4822276.html
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 13 21:03:24 2017
@author: Riddle
"""
from matplotlib import pyplot as plt
plt.rcParams["font.sans-serif"] = ["Microsoft YaHei"]
plt.rcParams['axes.unicode_minus'] = False
years = [1960,1960,1970,1980,1990,2000,2010]
gdp = [300.2,543.3,1075.9,2862.5,5979.6,10289.7,14958.3]
plt.plot(years,gdp,color='green',marker='o',linestyle='solid')
plt.title(u'名义GDP')
plt.ylabel(u'十亿美元')
plt.show()
二、过程中遇到的其他问题
http://blog.csdn.net/qq_30490125/article/details/52201754
这篇文章的解决方法
import sys
reload(sys)
sys.setdefaultencoding('utf8')
执行reload(sys)报错
Traceback (most recent call last):
File "d:\ProgramData\Anaconda2\lib\site-packages\IPython\core\formatters.py", line 672, in __call__
printer.pretty(obj)
File "d:\ProgramData\Anaconda2\lib\site-packages\IPython\lib\pretty.py", line 368, in pretty
return self.type_pprinters[cls](obj, self, cycle)
File "d:\ProgramData\Anaconda2\lib\site-packages\IPython\lib\pretty.py", line 609, in inner
p.begin_group(step, start)
File "d:\ProgramData\Anaconda2\lib\site-packages\IPython\lib\pretty.py", line 263, in begin_group
self.text(open)
File "d:\ProgramData\Anaconda2\lib\site-packages\IPython\lib\pretty.py", line 211, in text
self.output.write(obj)
File "d:\ProgramData\Anaconda2\lib\site-packages\IPython\lib\pretty.py", line 119, in write
cast_unicode(text, encoding=get_stream_enc(sys.stdout)))
File "d:\ProgramData\Anaconda2\lib\site-packages\IPython\utils\py3compat.py", line 27, in cast_unicode
return decode(s, encoding)
File "d:\ProgramData\Anaconda2\lib\site-packages\IPython\utils\py3compat.py", line 18, in decode
return s.decode(encoding, "replace")
LookupError: unknown encoding: cp0
不清楚reload方法为什么会报这个错。记录下。
三、继续是问题
http://www.cnblogs.com/buzhizhitong/p/5759304.html
这个文章中,手动指定字体的方法,但是试了没有用,不知道是什么原因。记录下。
四、总结
上述有提到,resin2.7默认使用的是ascii编码,其在字符串(字节/字符)的处理上,也与java不用。
具体的细节和原因,我并没有搞清楚,待学习。