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

后端 未结 7 1870
臣服心动
臣服心动 2020-11-30 17:30

I am using IPython with --pylab=inline and would sometimes like to quickly switch to the interactive, zoomable matplotlib GUI for viewing plots (the one that po

相关标签:
7条回答
  • 2020-11-30 18:04

    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.

    0 讨论(0)
  • 2020-11-30 18:05

    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.

    #interactive plotting in separate window
    %matplotlib qt 
    

    and back to 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 *".

    You also can use new notebook backend (added in 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 mpld3 and 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.

    Also you can check out 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.

    0 讨论(0)
  • 2020-11-30 18:09

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

    Here's an example to flip back and forth between a separate window and an inline plot mode using 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...
    
    0 讨论(0)
  • 2020-11-30 18:24

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

    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.

    Disclaimer: I'm the developer of the library

    0 讨论(0)
  • 2020-11-30 18:26

    You can use

    %matplotlib qt

    If you got the error ImportError: Failed to import any qt binding then install PyQt5 as: pip install PyQt5 and it works for me.

    0 讨论(0)
  • 2020-11-30 18:26

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

    %matplotlib tk
    

    For more info go to Plotting with matplotlib

    0 讨论(0)
提交回复
热议问题