5
5
# Distributed under the terms of the Modified BSD License.
6
6
7
7
from io import BytesIO
8
+ import warnings
8
9
9
10
from IPython .core .display import _pngxy
10
11
from IPython .utils .decorators import flag_calls
25
26
'svg' : 'svg' ,
26
27
'pdf' : 'pdf' ,
27
28
'ps' : 'ps' ,
28
- 'inline' : 'module://ipykernel.pylab .backend_inline' ,
29
+ 'inline' : 'module://matplotlib_inline .backend_inline' ,
29
30
'ipympl' : 'module://ipympl.backend_nbagg' ,
30
31
'widget' : 'module://ipympl.backend_nbagg' ,
31
32
}
49
50
del backend2gui ['svg' ]
50
51
del backend2gui ['pdf' ]
51
52
del backend2gui ['ps' ]
52
- del backend2gui ['module://ipykernel.pylab .backend_inline' ]
53
+ del backend2gui ['module://matplotlib_inline .backend_inline' ]
53
54
54
55
#-----------------------------------------------------------------------------
55
56
# Matplotlib utilities
@@ -96,10 +97,10 @@ def figsize(sizex, sizey):
96
97
97
98
def print_figure (fig , fmt = 'png' , bbox_inches = 'tight' , ** kwargs ):
98
99
"""Print a figure to an image, and return the resulting file data
99
-
100
+
100
101
Returned data will be bytes unless ``fmt='svg'``,
101
102
in which case it will be unicode.
102
-
103
+
103
104
Any keyword args are passed to fig.canvas.print_figure,
104
105
such as ``quality`` or ``bbox_inches``.
105
106
"""
@@ -112,7 +113,7 @@ def print_figure(fig, fmt='png', bbox_inches='tight', **kwargs):
112
113
if fmt == 'retina' :
113
114
dpi = dpi * 2
114
115
fmt = 'png'
115
-
116
+
116
117
# build keyword args
117
118
kw = {
118
119
"format" :fmt ,
@@ -162,7 +163,7 @@ def mpl_runner(safe_execfile):
162
163
A function suitable for use as the ``runner`` argument of the %run magic
163
164
function.
164
165
"""
165
-
166
+
166
167
def mpl_execfile (fname ,* where ,** kw ):
167
168
"""matplotlib-aware wrapper around safe_execfile.
168
169
@@ -243,7 +244,7 @@ def select_figure_formats(shell, formats, **kwargs):
243
244
bs = "%s" % ',' .join ([repr (f ) for f in bad ])
244
245
gs = "%s" % ',' .join ([repr (f ) for f in supported ])
245
246
raise ValueError ("supported formats are: %s not %s" % (gs , bs ))
246
-
247
+
247
248
if 'png' in formats :
248
249
png_formatter .for_type (Figure , lambda fig : print_figure (fig , 'png' , ** kwargs ))
249
250
if 'retina' in formats or 'png2x' in formats :
@@ -274,7 +275,7 @@ def find_gui_and_backend(gui=None, gui_select=None):
274
275
Returns
275
276
-------
276
277
A tuple of (gui, backend) where backend is one of ('TkAgg','GTKAgg',
277
- 'WXAgg','Qt4Agg','module://ipykernel.pylab .backend_inline','agg').
278
+ 'WXAgg','Qt4Agg','module://matplotlib_inline .backend_inline','agg').
278
279
"""
279
280
280
281
import matplotlib
@@ -308,7 +309,7 @@ def activate_matplotlib(backend):
308
309
309
310
import matplotlib
310
311
matplotlib .interactive (True )
311
-
312
+
312
313
# Matplotlib had a bug where even switch_backend could not force
313
314
# the rcParam to update. This needs to be set *before* the module
314
315
# magic of switch_backend().
@@ -329,11 +330,11 @@ def activate_matplotlib(backend):
329
330
330
331
def import_pylab (user_ns , import_all = True ):
331
332
"""Populate the namespace with pylab-related values.
332
-
333
+
333
334
Imports matplotlib, pylab, numpy, and everything from pylab and numpy.
334
-
335
+
335
336
Also imports a few names from IPython (figsize, display, getfigs)
336
-
337
+
337
338
"""
338
339
339
340
# Import numpy as np/pyplot as plt are conventions we're trying to
@@ -346,12 +347,12 @@ def import_pylab(user_ns, import_all=True):
346
347
"plt = pyplot\n "
347
348
)
348
349
exec (s , user_ns )
349
-
350
+
350
351
if import_all :
351
352
s = ("from matplotlib.pylab import *\n "
352
353
"from numpy import *\n " )
353
354
exec (s , user_ns )
354
-
355
+
355
356
# IPython symbols to add
356
357
user_ns ['figsize' ] = figsize
357
358
from IPython .display import display
@@ -361,59 +362,20 @@ def import_pylab(user_ns, import_all=True):
361
362
362
363
363
364
def configure_inline_support (shell , backend ):
364
- """Configure an IPython shell object for matplotlib use.
365
+ """
366
+ DEPRECATED
367
+
368
+ Configure an IPython shell object for matplotlib use.
365
369
366
370
Parameters
367
371
----------
368
372
shell : InteractiveShell instance
369
373
370
374
backend : matplotlib backend
371
375
"""
372
- # If using our svg payload backend, register the post-execution
373
- # function that will pick up the results for display. This can only be
374
- # done with access to the real shell object.
375
-
376
- # Note: if we can't load the inline backend, then there's no point
377
- # continuing (such as in terminal-only shells in environments without
378
- # zeromq available).
379
- try :
380
- from ipykernel .pylab .backend_inline import InlineBackend
381
- except ImportError :
382
- return
383
- import matplotlib
376
+ warnings .warn ("`configure_inline_support` is deprecated, directly use "
377
+ "`matplotlib_inline.backend_inline.configure_inline_support()`" ,
378
+ DeprecationWarning , stacklevel = 2 )
384
379
385
- cfg = InlineBackend .instance (parent = shell )
386
- cfg .shell = shell
387
- if cfg not in shell .configurables :
388
- shell .configurables .append (cfg )
389
-
390
- if backend == backends ['inline' ]:
391
- from ipykernel .pylab .backend_inline import flush_figures
392
- shell .events .register ('post_execute' , flush_figures )
393
-
394
- # Save rcParams that will be overwrittern
395
- shell ._saved_rcParams = {}
396
- for k in cfg .rc :
397
- shell ._saved_rcParams [k ] = matplotlib .rcParams [k ]
398
- # load inline_rc
399
- matplotlib .rcParams .update (cfg .rc )
400
- new_backend_name = "inline"
401
- else :
402
- from ipykernel .pylab .backend_inline import flush_figures
403
- try :
404
- shell .events .unregister ('post_execute' , flush_figures )
405
- except ValueError :
406
- pass
407
- if hasattr (shell , '_saved_rcParams' ):
408
- matplotlib .rcParams .update (shell ._saved_rcParams )
409
- del shell ._saved_rcParams
410
- new_backend_name = "other"
411
-
412
- # only enable the formats once -> don't change the enabled formats (which the user may
413
- # has changed) when getting another "%matplotlib inline" call.
414
- # See https://github.com/ipython/ipykernel/issues/29
415
- cur_backend = getattr (configure_inline_support , "current_backend" , "unset" )
416
- if new_backend_name != cur_backend :
417
- # Setup the default figure format
418
- select_figure_formats (shell , cfg .figure_formats , ** cfg .print_figure_kwargs )
419
- configure_inline_support .current_backend = new_backend_name
380
+ from matplotlib_inline .backend_inline import configure_inline_support_orig
381
+ configure_inline_support_orig (shell , backend )
0 commit comments