【问题标题】:ipython not producing output graph using matplotlibipython 不使用 matplotlib 生成输出图
【发布时间】:2013-11-19 21:08:35
【问题描述】:

所以我最近开始尝试使用 ipython,我发现我无法让它生成输出图。我在 ipython 中运行以下代码:

from sklearn import linear_model
regr = linear_model.LinearRegression()

regr.fit(x, y)
pl.plot(x, y, 'o')
pl.plot(x_test, regr.predict(x_test))

我正在接收输出:

[<matplotlib.lines.Line2D at 0x21d453b0>]

没有附加图像。

我使用 pythonxy 包安装了 ipython。 关于在 ipython 中正确输出绘图的方法的任何建议

见附图:

【问题讨论】:

  • 你在使用带有 -pylab 选项的 ipython 吗?
  • @joaquin 是的,我想是的,请参阅 elyase 提供的回答评论
  • 您使用的是哪个版本的 IPython?从技术上讲,我意识到您尝试使用--pylab=inline。您可以尝试在单元格中运行 %matplotlib 然后重新执行该代码吗?
  • 我正在运行ipython-1.1.0-py2.7.egg-info 我尝试使用使用%matplotlib 并收到matplotlib backend: Qt4Agg

标签: python python-2.7 matplotlib ipython pythonxy


【解决方案1】:

尝试在单元格中运行:

%pylab inline    # or
%matplotlib inline

之后,绘图应内联显示。或者使用命令行中的 inline 选项启动笔记本:

ipython notebook --pylab=inline

【讨论】:

  • 我的 IPython 已经运行如下:C:\Python27\Scripts\ipython.exe notebook --pylab=inline
  • @AEA,嗯,你的代码在我的笔记本上运行正常,你可以尝试重置你的matplotlib设置rm .matplotlib/matplotlibrc吗?如果您在 windows 上查找 matplotlibrc 的位置,请查看 here
【解决方案2】:
from IPython.display import display
from IPython.display import Image

# your code here

Image(data=<your_image_data_here>, embed=True)

【讨论】:

  • 收到错误:ERROR:astropy:TypeError: &lt;matplotlib.lines.Line2D object at 0x225DADB0&gt; is not JSON serializable
  • 我不确定plot() 输出的格式是什么,但它需要是png 或者将参数format='jpg' 传递给Image()
  • 作为参考,这是我正在关注的教程。我在遇到问题前 3 分钟完成了它。 :(youtube.com/watch?v=uX4ZirOiWkw
猜你喜欢
  • 1970-01-01
  • 2015-05-19
  • 2016-03-18
  • 1970-01-01
  • 2015-05-06
  • 2017-12-21
  • 2014-05-16
  • 1970-01-01
  • 2014-11-07
相关资源
最近更新 更多