|
20 | 20 |
|
21 | 21 |
|
22 | 22 | # Setup code to configure matplotlib for SVG output |
23 | | -# This is prepended to notebooks before execution |
| 23 | +# This is prepended to notebooks before execution. |
| 24 | +# |
| 25 | +# Do NOT call matplotlib.use('Agg') here: that activates the bare Agg backend, |
| 26 | +# which is non-interactive and bypasses IPython's inline display hooks. plt.show() |
| 27 | +# becomes a no-op and nbconvert never sees any image, so cell outputs end up with |
| 28 | +# stdout/stderr but figures: []. We need the matplotlib_inline backend (Agg under |
| 29 | +# the hood, plus _repr_*_ hooks that emit display_data) — activate it via the |
| 30 | +# %matplotlib inline magic before setting figure_formats to SVG. |
24 | 31 | MATPLOTLIB_SVG_SETUP = ''' |
25 | | -import matplotlib |
26 | | -matplotlib.use('Agg') |
27 | | -import matplotlib.pyplot as plt |
| 32 | +from IPython import get_ipython |
| 33 | +ipython = get_ipython() |
| 34 | +if ipython is not None: |
| 35 | + ipython.run_line_magic('matplotlib', 'inline') |
| 36 | + ipython.run_line_magic('config', "InlineBackend.figure_formats = ['svg']") |
28 | 37 |
|
29 | | -# Configure matplotlib for SVG output |
30 | | -plt.rcParams['savefig.format'] = 'svg' |
| 38 | +import matplotlib.pyplot as plt |
31 | 39 | plt.rcParams['figure.facecolor'] = 'none' |
32 | 40 | plt.rcParams['savefig.facecolor'] = 'none' |
33 | 41 | plt.rcParams['savefig.transparent'] = True |
34 | | -
|
35 | | -# Configure IPython inline backend for SVG |
36 | | -try: |
37 | | - from IPython import get_ipython |
38 | | - ipython = get_ipython() |
39 | | - if ipython is not None: |
40 | | - ipython.run_line_magic('config', "InlineBackend.figure_formats = ['svg']") |
41 | | -except: |
42 | | - pass |
43 | 42 | ''' |
44 | 43 |
|
45 | 44 |
|
|
0 commit comments