MCPcopy Create free account
hub / github.com/ipython/ipython / select_figure_formats

Function select_figure_formats

IPython/core/pylabtools.py:209–256  ·  view source on GitHub ↗

Select figure formats for the inline backend. Parameters ========== shell : InteractiveShell The main IPython instance. formats : str or set One or a set of figure formats to enable: 'png', 'retina', 'jpeg', 'svg', 'pdf'. **kwargs : any Extra keyword argu

(shell, formats, **kwargs)

Source from the content-addressed store, hash-verified

207
208
209def select_figure_formats(shell, formats, **kwargs):
210 """Select figure formats for the inline backend.
211
212 Parameters
213 ==========
214 shell : InteractiveShell
215 The main IPython instance.
216 formats : str or set
217 One or a set of figure formats to enable: 'png', 'retina', 'jpeg', 'svg', 'pdf'.
218 **kwargs : any
219 Extra keyword arguments to be passed to fig.canvas.print_figure.
220 """
221 import matplotlib
222 from matplotlib.figure import Figure
223
224 svg_formatter = shell.display_formatter.formatters['image/svg+xml']
225 png_formatter = shell.display_formatter.formatters['image/png']
226 jpg_formatter = shell.display_formatter.formatters['image/jpeg']
227 pdf_formatter = shell.display_formatter.formatters['application/pdf']
228
229 if isinstance(formats, str):
230 formats = {formats}
231 # cast in case of list / tuple
232 formats = set(formats)
233
234 [ f.pop(Figure, None) for f in shell.display_formatter.formatters.values() ]
235 mplbackend = matplotlib.get_backend().lower()
236 if mplbackend == 'nbagg' or mplbackend == 'module://ipympl.backend_nbagg':
237 formatter = shell.display_formatter.ipython_display_formatter
238 formatter.for_type(Figure, _reshow_nbagg_figure)
239
240 supported = {'png', 'png2x', 'retina', 'jpg', 'jpeg', 'svg', 'pdf'}
241 bad = formats.difference(supported)
242 if bad:
243 bs = "%s" % ','.join([repr(f) for f in bad])
244 gs = "%s" % ','.join([repr(f) for f in supported])
245 raise ValueError("supported formats are: %s not %s" % (gs, bs))
246
247 if 'png' in formats:
248 png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
249 if 'retina' in formats or 'png2x' in formats:
250 png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))
251 if 'jpg' in formats or 'jpeg' in formats:
252 jpg_formatter.for_type(Figure, lambda fig: print_figure(fig, 'jpg', **kwargs))
253 if 'svg' in formats:
254 svg_formatter.for_type(Figure, lambda fig: print_figure(fig, 'svg', **kwargs))
255 if 'pdf' in formats:
256 pdf_formatter.for_type(Figure, lambda fig: print_figure(fig, 'pdf', **kwargs))
257
258#-----------------------------------------------------------------------------
259# Code for initializing matplotlib and importing pylab

Callers 2

set_matplotlib_formatsFunction · 0.90
configure_inline_supportFunction · 0.85

Calls 4

print_figureFunction · 0.85
retina_figureFunction · 0.85
popMethod · 0.80
for_typeMethod · 0.80

Tested by

no test coverage detected