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)
| 207 | |
| 208 | |
| 209 | def 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 |
no test coverage detected