pandas 熊猫图未在 ipython 笔记本中显示为内联

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28384951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:55:42  来源:igfitidea点击:

pandas plot doesn't show in ipython notebook as inline

pythonmatplotlibpandasplotipython-notebook

提问by HMPark

I'm trying to plot graph in ipython notebook inline, but .plot()methos just shows the object information, as

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

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

but no graph. also i could make it show graph with plt.show(), but i want do it inline. so i tryed %matplotlib inlineand ipython notebook --matplotlib=inline, but it was no helpful.

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

If i use %matplotlib inline, then .plot()shows

如果我使用%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,

and using ipython notebook --matplotlib=inlineshows same.

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

采纳答案by aus_lacy

Change

改变

ipython notebook --matplotlib=inline 

to

ipython notebook --matplotlib inline 

Notice no =sign.

注意没有=标志。

回答by Robert Smith

I will give you an example based on my comment above:

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

You have something like this:

你有这样的事情:

import matplotlib.pyplot as plt

%matplotlib inline

legend = "\xe2"

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

which results in:

这导致:

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

As I said, this is because matplotlib wants to use strings of type unicode. So, in the process of plotting, matplotlib tries to decode your string to convert it to unicode using decode. However, decodehas asciias default encoding, and since your character doesn't belong to ascii, an error is shown. The solution is to decode the string yourself with the appropriate encoding:

正如我所说,这是因为 matplotlib 想要使用类型为 的字符串unicode。因此,在绘图过程中,matplotlib 尝试解码您的字符串以使用decode. 但是,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

在此处输入图片说明

By the way, regarding the use ipython notebook --matplotlib inline, it is considered bad practice to do that because you are hiding what you did in order to obtain the resulting notebook. It is much better to include %matplotlib inlinein your notebook.

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

回答by HMPark

Thanks for all your helps. i've tried all of above, but did't work.

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

here i found that was bug in fontmanager.pyin matplotlib 1.4.x, fixed with thisdevelopment version of matplotlib and it worked.

在这里我发现这是fontmanager.pymatplotlib 1.4.x 中的错误,使用 matplotlib 的这个开发版本修复并且它起作用了。

i'm so sorry that i couldn't find it earlier. thanks for all of you.

很抱歉我没能早点找到它。谢谢大家。