如何使 IPython 笔记本 matplotlib 绘图内联

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19410042/
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-08-19 13:40:48  来源:igfitidea点击:

How to make IPython notebook matplotlib plot inline

pythonmatplotlibjupyter-notebookipythondata-visualization

提问by Ian Fiske

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 笔记本。

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 inlineand the ipython command line arguments --pylab=inlinebut 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'后端。

采纳答案by eNord9

I used %matplotlib inlinein the first cell of the notebook and it works. I think you should try:

%matplotlib inline在笔记本的第一个单元格中使用过,并且可以正常工作。我认为你应该尝试:

%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.

回答by Ian Fiske

I found a workaround that is quite satisfactory. I installed Anaconda Pythonand this now works out of the box for me.

我找到了一个非常令人满意的解决方法。我安装了Anaconda Python,现在这对我来说是开箱即用的。

回答by foobarbecue

Use the %pylab inlinemagic command.

使用%pylab inline魔法命令。

回答by thescoop

I have to agree with foobarbecue (I don't have enough recs to be able to simply insert a comment under his post):

我必须同意 foobarbecue(我没有足够的 recs 能够简单地在他的帖子下插入评论):

It's now recommended that python notebook isn't started wit the argument --pylab, and according to Fernando Perez (creator of ipythonnb) %matplotlib inlineshould be the initial notebook command.

现在建议不要使用参数启动 python notebook --pylab,根据 Fernando Perez(ipythonnb 的创建者)的说法,%matplotlib inline应该是初始 notebook 命令。

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

回答by Raymond

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  

回答by CodeFarmer

Ctrl+ Enter

Ctrl+ Enter

%matplotlib inline

Magic Line :D

魔法线 :D

See: Plotting with Matplotlib.

请参阅:使用 Matplotlib 绘图

回答by volodymyr

To make matplotlib inline by default in Jupyter (IPython 3):

要在 Jupyter (IPython 3) 中默认内联 matplotlib:

  1. Edit file ~/.ipython/profile_default/ipython_config.py

  2. Add line c.InteractiveShellApp.matplotlib = 'inline'

  1. 编辑文件 ~/.ipython/profile_default/ipython_config.py

  2. 添加行 c.InteractiveShellApp.matplotlib = 'inline'

Please note that adding this line to ipython_notebook_config.pywould not work. Otherwise it works well with Jupyter and IPython 3.1.0

请注意,将此行添加到ipython_notebook_config.py将不起作用。否则它适用于 Jupyter 和 IPython 3.1.0

回答by L?iten

If your matplotlib version is above 1.4, it is also possible to use

如果你的matplotlib版本在1.4以上,也可以使用

IPython 3.x and above

IPython 3.x 及以上

%matplotlib notebook

import matplotlib.pyplot as plt

older versions

旧版本

%matplotlib nbagg

import matplotlib.pyplot as plt

Both will activate the nbagg backend, which enables interactivity.

两者都将激活nbagg 后端,从而实现交互。

Example plot with the nbagg backend

带有 nbagg 后端的示例图

回答by Blake M

You can simulate this problem with a syntax mistake, however, %matplotlib inlinewon't resolve the issue.

您可以使用语法错误来模拟此问题,但%matplotlib inline不会解决问题。

First an example of the right way to create a plot. Everything works as expected with the imports and magic that eNord9supplied.

首先是创建绘图的正确方法的示例。使用eNord9提供的导入和魔法,一切都按预期工作。

df_randNumbers1 = pd.DataFrame(np.random.randint(0,100,size=(100, 6)), columns=list('ABCDEF'))

df_randNumbers1.ix[:,["A","B"]].plot.kde()

However, by leaving the ()off the end of the plot type you receive a somewhat ambiguous non-error.

但是,通过将()绘图类型的末尾保留下来,您会收到一个有点模棱两可的非错误。

Erronious code:

错误代码:

df_randNumbers1.ix[:,["A","B"]].plot.kde

Example error:

示例错误:

<bound method FramePlotMethods.kde of <pandas.tools.plotting.FramePlotMethods object at 0x000001DDAF029588>>

Other than this one line message, there is no stack trace or other obvious reason to think you made a syntax error. The plot doesn't print.

除了这一行消息,没有堆栈跟踪或其他明显的理由认为您犯了语法错误。情节不打印。

回答by krubo

I had the same problem when I was running the plotting commands in separate cells in Jupyter:

当我在 Jupyter 的不同单元格中运行绘图命令时,我遇到了同样的问题:

In [1]:  %matplotlib inline
         import matplotlib
         import matplotlib.pyplot as plt
         import numpy as np
In [2]:  x = np.array([1, 3, 4])
         y = np.array([1, 5, 3])
In [3]:  fig = plt.figure()
         <Figure size 432x288 with 0 Axes>                      #this might be the problem
In [4]:  ax = fig.add_subplot(1, 1, 1)
In [5]:  ax.scatter(x, y)
Out[5]:  <matplotlib.collections.PathCollection at 0x12341234>  # CAN'T SEE ANY PLOT :(
In [6]:  plt.show()                                             # STILL CAN'T SEE IT :(

The problem was solved by merging the plotting commands into a single cell:

通过将绘图命令合并到一个单元格中解决了该问题:

In [1]:  %matplotlib inline
         import matplotlib
         import matplotlib.pyplot as plt
         import numpy as np
In [2]:  x = np.array([1, 3, 4])
         y = np.array([1, 5, 3])
In [3]:  fig = plt.figure()
         ax = fig.add_subplot(1, 1, 1)
         ax.scatter(x, y)
Out[3]:  <matplotlib.collections.PathCollection at 0x12341234>
         # AND HERE APPEARS THE PLOT AS DESIRED :)