如何在 IPython notebook 中打开交互式 matplotlib 窗口?

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

How can I open the interactive matplotlib window in IPython notebook?

pythonmatplotlibjupyter-notebook

提问by metakermit

I am using IPython with --pylab=inlineand would sometimes like to quickly switch to the interactive, zoomable matplotlib GUI for viewing plots (the one that pops up when you plot something in a terminal Python console). How could I do that? Preferably without leaving or restarting my notebook.

我正在使用 IPython,--pylab=inline有时想快速切换到交互式、可缩放的 matplotlib GUI 以查看绘图(在终端 Python 控制台中绘图时弹出的图形)。我怎么能那样做?最好不要离开或重新启动我的笔记本。

The problem with inline plots in IPy notebook is that they are of a limited resolution and I can't zoom into them to see some smaller parts. With the maptlotlib GUI that starts from a terminal, I can select a rectangle of the graph that I want to zoom into and the axes adjust accordingly. I tried experimenting with

IPy notebook 中的内联图的问题在于它们的分辨率有限,我无法放大它们以查看一些较小的部分。使用从终端启动的 maptlotlib GUI,我可以选择要放大的图形矩形,并相应地调整轴。我尝试尝试

from matplotlib import interactive
interactive(True)

and

interactive(False)

but that didn't do anything. I couldn't find any hint online either.

但这没有做任何事情。我在网上也找不到任何提示。

采纳答案by Adrian Martin

According to the documentation, you should be able to switch back and forth like this:

根据文档,您应该能够像这样来回切换:

In [2]: %matplotlib inline 
In [3]: plot(...)

In [4]: %matplotlib qt  # wx, gtk, osx, tk, empty uses default
In [5]: plot(...) 

and that will pop up a regular plot window (a restart on the notebook may be necessary).

这将弹出一个常规绘图窗口(可能需要重新启动笔记本)。

I hope this helps.

我希望这有帮助。

回答by volodymyr

If all you want to do is to switch from inline plots to interactive and back (so that you can pan/zoom), it is better to use %matplotlib magic.

如果您只想从内联图切换到交互式并返回(以便您可以平移/缩放),最好使用 %matplotlib 魔法。

#interactive plotting in separate window
%matplotlib qt 

and back to html

然后回到 html

#normal charts inside notebooks
%matplotlib inline 

%pylab magic imports a bunch of other things and may even result in a conflict. It does "from pylab import *".

%pylab magic 导入了一堆其他东西,甚至可能导致冲突。它执行“从 pylab 导入 *”。

You also can use new notebook backend (added in matplotlib 1.4):

您还可以使用新的笔记本后端(在 matplotlib 1.4 中添加):

#interactive charts inside notebooks, matplotlib 1.4+
%matplotlib notebook 

If you want to have more interactivity in your charts, you can look at mpld3and bokeh. mpld3 is great, if you don't have ton's of data points (e.g. <5k+) and you want to use normal matplotlib syntax, but more interactivity, compared to %matplotlib notebook . Bokeh can handle lots of data, but you need to learn it's syntax as it is a separate library.

如果您想在图表中具有更多交互性,可以查看mpld3bokeh。mpld3 很棒,如果您没有大量数据点(例如 <5k+)并且您想使用普通的 matplotlib 语法,但与 %matplotlib notebook 相比具有更多的交互性。Bokeh 可以处理大量数据,但您需要学习它的语法,因为它是一个单独的库。

Also you can check out pivottablejs (pip install pivottablejs)

你也可以查看pivottablejs(pip install pivottablejs)

from pivottablejs import pivot_ui
pivot_ui(df)

However cool interactive data exploration is, it can totally mess with reproducibility. It has happened to me, so I try to use it only at the very early stage and switch to pure inline matplotlib/seaborn, once I got the feel for the data.

无论交互式数据探索有多酷,它都可能完全破坏可重复性。它发生在我身上,所以我尝试只在很早的阶段使用它,一旦我对数据有了感觉,就切换到纯内联 matplotlib/seaborn。

回答by arnoutaertgeerts

A better solution for your problem might be the Chartslibrary. It enables you to use the excellent Highchartsjavascript library to make beautiful and interactive plots. Highcharts uses the HTML svgtag so all your charts are actually vector images.

您的问题的更好解决方案可能是Charts库。它使您能够使用优秀的Highchartsjavascript 库来制作漂亮的交互式绘图。Highcharts 使用 HTMLsvg标签,因此您所有的图表实际上都是矢量图像。

Some features:

一些特点:

  • Vector plots which you can download in .png, .jpg and .svg formats so you will never run into resolution problems
  • Interactive charts (zoom, slide, hover over points, ...)
  • Usable in an IPython notebook
  • Explore hundreds of data structures at the same time using the asynchronous plotting capabilities.
  • 您可以下载 .png、.jpg 和 .svg 格式的矢量图,因此您永远不会遇到分辨率问题
  • 交互式图表(缩放、滑动、悬停在点上,...)
  • 可在 IPython 笔记本中使用
  • 使用异步绘图功能同时探索数百个数据结构。

Disclaimer: I'm the developer of the library

免责声明:我是库的开发者

回答by tacaswell

Starting with matplotlib 1.4.0 there is now an an interactive backend for use in the notebook

从 matplotlib 1.4.0 开始,现在有一个可在笔记本中使用的交互式后端

%matplotlib notebook

There are a few version of IPython which do not have that alias registered, the fall back is:

有几个版本的 IPython 没有注册该别名,回退是:

%matplotlib nbagg

If that does not work update you IPython.

如果这不起作用,请更新您的 IPython。

To play with this, goto tmpnb.org

要玩这个,去tmpnb.org

and paste

并粘贴

%matplotlib notebook

import pandas as pd
import numpy as np
import matplotlib

from matplotlib import pyplot as plt
import seaborn as sns

ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
ts = ts.cumsum()

df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index,
                  columns=['A', 'B', 'C', 'D'])
df = df.cumsum()
df.plot(); plt.legend(loc='best')    

into a code cell (or just modify the existing python demo notebook)

进入代码单元格(或只是修改现有的 python 演示笔记本)

回答by Marcin Lentner

Restart kernel and clear output (if not starting with new notebook), then run

重新启动内核并清除输出(如果不是从新笔记本开始),然后运行

%matplotlib tk

For more info go to Plotting with matplotlib

有关更多信息,请转到使用 matplotlib 绘图

回答by Bill Moore

I'm using ipython in "jupyter QTConsole" from Anaconda at www.continuum.io/downloads on 5/28/20117.

我在 20117 年 5 月 28 日在 www.continuum.io/downloads 来自 Anaconda 的“jupyter QTConsole”中使用 ipython。

Here's an example to flip back and forth between a separate window and an inline plot mode using ipython magic.

这是一个使用 ipython magic 在单独窗口和内联绘图模式之间来回切换的示例。

>>> import matplotlib.pyplot as plt

# data to plot
>>> x1 = [x for x in range(20)]

# Show in separate window
>>> %matplotlib
>>> plt.plot(x1)
>>> plt.close() 

# Show in console window
>>> %matplotlib inline
>>> plt.plot(x1)
>>> plt.close() 

# Show in separate window
>>> %matplotlib
>>> plt.plot(x1)
>>> plt.close() 

# Show in console window
>>> %matplotlib inline
>>> plt.plot(x1)
>>> plt.close() 

# Note: the %matplotlib magic above causes:
#      plt.plot(...) 
# to implicitly include a:
#      plt.show()
# after the command.
#
# (Not sure how to turn off this behavior
# so that it matches behavior without using %matplotlib magic...)
# but its ok for interactive work...