##// END OF EJS Templates
Merge pull request #13606 from Carreau/wn822...
Merge pull request #13606 from Carreau/wn822 second whats new update

File last commit:

r26726:72a79cb1
r27609:2f9524c3 merge
Show More
pylab.py
169 lines | 6.4 KiB | text/x-python | PythonLexer
Fernando Perez
Create core.magics.pylab according to new API.
r6968 """Implementation of magic functions for matplotlib/pylab support.
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2012 The IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
# Our own packages
Min RK
update dependency imports...
r21253 from traitlets.config.application import Application
MinRK
add `%pylab --no-import`...
r11324 from IPython.core import magic_arguments
Fernando Perez
Renamed @register_magics to @magics_class to avoid confusion....
r6973 from IPython.core.magic import Magics, magics_class, line_magic
Fernando Perez
Create core.magics.pylab according to new API.
r6968 from IPython.testing.skipdoctest import skip_doctest
Pierre Gerold
Replace all import of IPython.utils.warn module
r22092 from warnings import warn
Paul Ivanov
include sorted list of backends
r11446 from IPython.core.pylabtools import backends
Fernando Perez
Create core.magics.pylab according to new API.
r6968
#-----------------------------------------------------------------------------
# Magic implementation classes
#-----------------------------------------------------------------------------
MinRK
add `%matplotlib` and `shell.enable_matplotlib`...
r11328 magic_gui_arg = magic_arguments.argument(
MinRK
add `%pylab --no-import`...
r11324 'gui', nargs='?',
Paul Ivanov
include sorted list of backends
r11446 help="""Name of the matplotlib backend to use %s.
MinRK
add `%pylab --no-import`...
r11324 If given, the corresponding matplotlib backend is used,
otherwise it will be matplotlib's default
(which you can set in your matplotlib config file).
Paul Ivanov
include sorted list of backends
r11446 """ % str(tuple(sorted(backends.keys())))
MinRK
add `%matplotlib` and `shell.enable_matplotlib`...
r11328 )
Fernando Perez
Create core.magics.pylab according to new API.
r6968
MinRK
add `%matplotlib` and `shell.enable_matplotlib`...
r11328 @magics_class
class PylabMagics(Magics):
"""Magics related to matplotlib's pylab support"""
Oriol (Prodesk)
Fix %matplotlib...
r25137
MinRK
add `%matplotlib` and `shell.enable_matplotlib`...
r11328 @skip_doctest
@line_magic
@magic_arguments.magic_arguments()
Matthias Bussonnier
remove explicit default...
r21428 @magic_arguments.argument('-l', '--list', action='store_true',
Koen van Besien
added %matplotlib --list so backends are easily visible
r21366 help='Show available matplotlib backends')
MinRK
add `%matplotlib` and `shell.enable_matplotlib`...
r11328 @magic_gui_arg
def matplotlib(self, line=''):
"""Set up matplotlib to work interactively.
Oriol (Prodesk)
Fix %matplotlib...
r25137
MinRK
add `%matplotlib` and `shell.enable_matplotlib`...
r11328 This function lets you activate matplotlib interactive support
Brian E. Granger
Refining the matplotlib APIs in IPython.display.
r15126 at any point during an IPython session. It does not import anything
into the interactive namespace.
Oriol (Prodesk)
Fix %matplotlib...
r25137
Brian E. Granger
Refining the matplotlib APIs in IPython.display.
r15126 If you are using the inline matplotlib backend in the IPython Notebook
you can set which figure formats are enabled using the following::
Oriol (Prodesk)
Fix %matplotlib...
r25137
Brian E. Granger
Refining the matplotlib APIs in IPython.display.
r15126 In [1]: from IPython.display import set_matplotlib_formats
Oriol (Prodesk)
Fix %matplotlib...
r25137
Brian E. Granger
Refining the matplotlib APIs in IPython.display.
r15126 In [2]: set_matplotlib_formats('pdf', 'svg')
Fernando Perez
Create core.magics.pylab according to new API.
r6968
Ryan Nelson
DOC: Update %matplotlib for bbox_inches...
r20711 The default for inline figures sets `bbox_inches` to 'tight'. This can
cause discrepancies between the displayed image and the identical
image created using `savefig`. This behavior can be disabled using the
`%config` magic::
Oriol (Prodesk)
Fix %matplotlib...
r25137
Ryan Nelson
DOC: Update %matplotlib for bbox_inches...
r20711 In [3]: %config InlineBackend.print_figure_kwargs = {'bbox_inches':None}
In addition, see the docstring of
`IPython.display.set_matplotlib_formats` and
Brian E. Granger
Refining the matplotlib APIs in IPython.display.
r15126 `IPython.display.set_matplotlib_close` for more information on
Ryan Nelson
DOC: Update %matplotlib for bbox_inches...
r20711 changing additional behaviors of the inline backend.
Fernando Perez
Create core.magics.pylab according to new API.
r6968
Examples
--------
Brian E. Granger
Refining the matplotlib APIs in IPython.display.
r15126 To enable the inline backend for usage with the IPython Notebook::
Oriol (Prodesk)
Fix %matplotlib...
r25137
Brian E. Granger
Refining the matplotlib APIs in IPython.display.
r15126 In [1]: %matplotlib inline
In this case, where the matplotlib default is TkAgg::
Fernando Perez
Create core.magics.pylab according to new API.
r6968
MinRK
add `%matplotlib` and `shell.enable_matplotlib`...
r11328 In [2]: %matplotlib
Using matplotlib backend: TkAgg
Fernando Perez
Create core.magics.pylab according to new API.
r6968
Brian E. Granger
Refining the matplotlib APIs in IPython.display.
r15126 But you can explicitly request a different GUI backend::
Fernando Perez
Create core.magics.pylab according to new API.
r6968
MinRK
add `%matplotlib` and `shell.enable_matplotlib`...
r11328 In [3]: %matplotlib qt
Koen van Besien
added %matplotlib --list so backends are easily visible
r21366
Thomas Kluyver
Fix various warnings for broken references etc
r21578 You can list the available backends using the -l/--list option::
Koen van Besien
added %matplotlib --list so backends are easily visible
r21366
Koen van Besien
Simpler if statement and added line to the docs
r21406 In [4]: %matplotlib --list
Elliott Sales de Andrade
Add input hooks for GTK4.
r26726 Available matplotlib backends: ['osx', 'qt4', 'qt5', 'gtk3', 'gtk4', 'notebook', 'wx', 'qt', 'nbagg',
Thomas Kluyver
Fix various warnings for broken references etc
r21578 'gtk', 'tk', 'inline']
Koen van Besien
Simpler if statement and added line to the docs
r21406 """
args = magic_arguments.parse_argstring(self.matplotlib, line)
if args.list:
Koen van Besien
added %matplotlib --list so backends are easily visible
r21366 backends_list = list(backends.keys())
print("Available matplotlib backends: %s" % backends_list)
else:
Oriol (Prodesk)
Fix %matplotlib...
r25137 gui, backend = self.shell.enable_matplotlib(args.gui.lower() if isinstance(args.gui, str) else args.gui)
Koen van Besien
added %matplotlib --list so backends are easily visible
r21366 self._show_matplotlib_backend(args.gui, backend)
Fernando Perez
Create core.magics.pylab according to new API.
r6968
MinRK
add `%matplotlib` and `shell.enable_matplotlib`...
r11328 @skip_doctest
@line_magic
@magic_arguments.magic_arguments()
@magic_arguments.argument(
'--no-import-all', action='store_true', default=None,
help="""Prevent IPython from performing ``import *`` into the interactive namespace.
Oriol (Prodesk)
Fix %matplotlib...
r25137
MinRK
move imports to %pylab description...
r11544 You can govern the default behavior of this flag with the
MinRK
add `%matplotlib` and `shell.enable_matplotlib`...
r11328 InteractiveShellApp.pylab_import_all configurable.
"""
)
@magic_gui_arg
def pylab(self, line=''):
"""Load numpy and matplotlib to work interactively.
This function lets you activate pylab (matplotlib, numpy and
interactive support) at any point during an IPython session.
Oriol (Prodesk)
Fix %matplotlib...
r25137
MinRK
move imports to %pylab description...
r11544 %pylab makes the following imports::
Oriol (Prodesk)
Fix %matplotlib...
r25137
MinRK
move imports to %pylab description...
r11544 import numpy
import matplotlib
from matplotlib import pylab, mlab, pyplot
np = numpy
plt = pyplot
Oriol (Prodesk)
Fix %matplotlib...
r25137
MinRK
move imports to %pylab description...
r11544 from IPython.display import display
from IPython.core.pylabtools import figsize, getfigs
Oriol (Prodesk)
Fix %matplotlib...
r25137
MinRK
move imports to %pylab description...
r11544 from pylab import *
from numpy import *
MinRK
add `%matplotlib` and `shell.enable_matplotlib`...
r11328
MinRK
move imports to %pylab description...
r11544 If you pass `--no-import-all`, the last two `*` imports will be excluded.
Oriol (Prodesk)
Fix %matplotlib...
r25137
MinRK
move imports to %pylab description...
r11544 See the %matplotlib magic for more details about activating matplotlib
without affecting the interactive namespace.
Fernando Perez
Create core.magics.pylab according to new API.
r6968 """
MinRK
add `%pylab --no-import`...
r11324 args = magic_arguments.parse_argstring(self.pylab, line)
MinRK
add `%matplotlib` and `shell.enable_matplotlib`...
r11328 if args.no_import_all is None:
MinRK
add `%pylab --no-import`...
r11324 # get default from Application
if Application.initialized():
app = Application.instance()
try:
import_all = app.pylab_import_all
except AttributeError:
import_all = True
else:
# nothing specified, no app - default True
import_all = True
Fernando Perez
Create core.magics.pylab according to new API.
r6968 else:
MinRK
add `%pylab --no-import`...
r11324 # invert no-import flag
MinRK
add `%matplotlib` and `shell.enable_matplotlib`...
r11328 import_all = not args.no_import_all
gui, backend, clobbered = self.shell.enable_pylab(args.gui, import_all=import_all)
self._show_matplotlib_backend(args.gui, backend)
Matthias Bussonnier
reformat because of new versionof black
r25977 print(
Albert Zhang
Small typo in pylab deprecation message
r26027 "%pylab is deprecated, use %matplotlib inline and import the required libraries."
Matthias Bussonnier
reformat because of new versionof black
r25977 )
print("Populating the interactive namespace from numpy and matplotlib")
MinRK
add `%matplotlib` and `shell.enable_matplotlib`...
r11328 if clobbered:
warn("pylab import has clobbered these variables: %s" % clobbered +
Paul Ivanov
less %pylab, more cowbell!
r15553 "\n`%matplotlib` prevents importing * from pylab and numpy"
MinRK
add `%matplotlib` and `shell.enable_matplotlib`...
r11328 )
Oriol (Prodesk)
Fix %matplotlib...
r25137
MinRK
add `%matplotlib` and `shell.enable_matplotlib`...
r11328 def _show_matplotlib_backend(self, gui, backend):
"""show matplotlib message backend message"""
if not gui or gui == 'auto':
Thomas Kluyver
Clean up converted code....
r13386 print("Using matplotlib backend: %s" % backend)