欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

如何使IPython Notebook Matplotlib内联绘图

程序员文章站 2022-03-21 11:04:49
...

本文翻译自:How to make IPython notebook matplotlib plot inline

I am trying to use IPython notebook on MacOS X with Python 2.7.2 and IPython 1.1.0. 我正在MacOS X上使用Python 2.7.2和IPython 1.1.0的情况下使用IPython Notebook。

I cannot get matplotlib graphics to show up inline. 我无法获得matplotlib图形来内联显示。

import matplotlib
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline  

I have also tried %pylab inline and the ipython command line arguments --pylab=inline but this makes no difference. 我也尝试了%pylab inline和ipython命令行参数--pylab=inline但这没什么区别。

x = np.linspace(0, 3*np.pi, 500)
plt.plot(x, np.sin(x**2))
plt.title('A simple chirp')
plt.show()

Instead of inline graphics, I get this: 我得到的不是内联图形,而是:

<matplotlib.figure.Figure at 0x110b9c450>

And matplotlib.get_backend() shows that I have the 'module://IPython.kernel.zmq.pylab.backend_inline' backend. matplotlib.get_backend()表明我有'module://IPython.kernel.zmq.pylab.backend_inline'后端。


#1楼

参考:https://stackoom.com/question/1JRRC/如何使IPython-Notebook-Matplotlib内联绘图


#2楼

I found a workaround that is quite satisfactory. 我找到了一种非常令人满意的解决方法。 I installed Anaconda Python and this now works out of the box for me. 我安装了Anaconda Python ,现在对我来说开箱即用。


#3楼

I'm not sure why joaquin posted his answer as a comment, but it is the correct answer: 我不确定为什么华金发表了他的回答作为评论,但这是正确的答案:

start ipython with ipython notebook --pylab inline ipython notebook --pylab inline启动ipython

Edit: Ok, this is now deprecated as per comment below. 编辑:好的,现在根据以下评论不推荐使用。 Use the %pylab inline magic command. 使用%pylab inline魔术命令。


#4楼

I used %matplotlib inline in the first cell of the notebook and it works. 我在笔记本的第一个单元格中使用了%matplotlib inline ,并且可以正常工作。 I think you should try: 我认为您应该尝试:

%matplotlib inline

import matplotlib
import numpy as np
import matplotlib.pyplot as plt

You can also always start all your IPython kernels in inline mode by default by setting the following config options in your config files: 默认情况下,通过在配置文件中设置以下配置选项,您也可以始终始终以内联模式启动所有IPython内核:

c.IPKernelApp.matplotlib=<CaselessStrEnum>
  Default: None
  Choices: ['auto', 'gtk', 'gtk3', 'inline', 'nbagg', 'notebook', 'osx', 'qt', 'qt4', 'qt5', 'tk', 'wx']
  Configure matplotlib for interactive use with the default matplotlib backend.

#5楼

I have to agree with foobarbecue (I don't have enough recs to be able to simply insert a comment under his post): 我必须同意foobarbecue(我的建议不足,无法简单地在他的帖子下插入评论):

It's now recommended that python notebook isn't started wit the argument --pylab , and according to Fernando Perez (creator of ipythonnb) %matplotlib inline should be the initial notebook command. 现在建议不要使用--pylab参数启动python笔记本,根据Fernando Perez(ipythonnb的创建者)的说法, %matplotlib inline应该是笔记本的初始命令。

See here: http://nbviewer.ipython.org/github/ipython/ipython/blob/1.x/examples/notebooks/Part%203%20-%20Plotting%20with%20Matplotlib.ipynb 请参阅此处: http : //nbviewer.ipython.org/github/ipython/ipython/blob/1.x/examples/notebooks/Part%203%20-%20Plotting%20with%20Matplotlib.ipynb


#6楼

I did the anaconda install but matplotlib is not plotting 我做了anaconda安装,但是matplotlib没有绘制

It starts plotting when i did this 当我这样做时它开始绘图

import matplotlib
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline