简体   繁体   English

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

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

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'后端。

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.

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 后端,从而实现交互。

带有 nbagg 后端的示例图

Ctrl + Enter Ctrl +输入

%matplotlib inline

Magic Line :D魔法线 :D

See: Plotting with Matplotlib .请参阅: 使用 Matplotlib 绘图

使用%pylab inline魔法命令。

To make matplotlib inline by default in Jupyter (IPython 3):要在 Jupyter (IPython 3) 中默认内联 matplotlib:

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

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

Please note that adding this line to ipython_notebook_config.py would not work.请注意,将此行添加到ipython_notebook_config.py将不起作用。 Otherwise it works well with Jupyter and IPython 3.1.0否则它适用于 Jupyter 和 IPython 3.1.0

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 inline should be the initial notebook command.现在建议不要使用参数--pylab启动 python notebook ,并且根据 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

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

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  

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 :)

You can simulate this problem with a syntax mistake, however, %matplotlib inline won'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 eNord9 supplied.使用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.情节不打印。

如果您在 Visual Studio Code (VSCode) 中使用 Jupyter 笔记本,那么inline后端似乎不起作用,因此您需要指定widget / ipympl (您可能需要安装支持,例如pip install ipympl ):

%matplotlib widget

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM