pandas plot并未在ipython笔记本中显示为内联

时间:2015-02-07 17:08:46

标签: python matplotlib pandas plot ipython-notebook

我试图在ipython notebook内联中绘制图形,但.plot()方法只显示对象信息,如

<matplotlib.axes._subplots.AxesSubplot at 0x10d8740d0>

但没有图表。我也可以用plt.show()显示图形,但我想要内联。所以我尝试了%matplotlib inlineipython notebook --matplotlib=inline,但没有用。

如果我使用%matplotlib inline,则.plot()会显示

/Users/<username>/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/IPython/core/formatters.py:239: 
FormatterWarning: Exception in image/png formatter: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128) FormatterWarning,

并使用ipython notebook --matplotlib=inline显示相同的内容。

3 个答案:

答案 0 :(得分:5)

更改

ipython notebook --matplotlib=inline 

ipython notebook --matplotlib inline 

请注意没有=标志。

答案 1 :(得分:3)

我会根据上面的评论给你一个例子:

你有这样的事情:

import matplotlib.pyplot as plt

%matplotlib inline

legend = "\xe2"

plt.plot(range(5), range(5))
plt.legend([legend])

导致:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128)

正如我所说,这是因为matplotlib想要使用unicode类型的字符串。因此,在绘图过程中,matplotlib会尝试解码您的字符串,以使用decode将其转换为unicode。但是,decode具有ascii作为默认编码,并且由于您的角色不属于ascii,因此会显示错误。解决方案是使用适当的编码自己解码字符串:

import matplotlib.pyplot as plt

%matplotlib inline

legend = "\xe2".decode(encoding='latin-1')

plt.plot(range(5), range(5))
plt.legend([legend])

enter image description here

顺便说一句,关于使用ipython notebook --matplotlib inline,这样做被认为是不好的做法,因为你隐藏了你为了获得结果笔记本而做的事情。在您的笔记本中包含%matplotlib inline要好得多。

答案 2 :(得分:0)

感谢您的所有帮助。 我已经尝试了以上所有,但没有奏效。

在这里我发现matplotlib 1.4.x中的fontmanager.py中存在错误,修复了matplotlib的this开发版本并且有效。

我很抱歉我以前找不到它。谢谢你们所有人。

相关问题