Skip to content

Commit f4389d0

Browse files
authored
Merge pull request #13117 from quarto-dev/backport/set-matplotlib
[backport] fix set_matplotlib_formats being removed from Ipython
2 parents 21c3538 + 19248dd commit f4389d0

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

news/changelog-1.7.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## In this release
44

55
- ([#13051](https://github.com/quarto-dev/quarto-cli/issues/13051)): Fixed support for captioned Markdown table inside Div syntax for crossref. This is special handling, but this could be output by function like `knitr::kable()` with old option support.
6+
- ([#12753](https://github.com/quarto-dev/quarto-cli/issues/12753)): Support change in IPython 9+ and import `set_matplotlib_formats` from `matplotlib_inline.backend_inline` in the internal `setup.py` script used to initialize rendering with Jupyter engine.
67

78
## In previous releases
89

src/resources/jupyter/lang/python/setup.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,14 @@
2222
plt.rcParams['figure.figsize'] = (fig_width, fig_height)
2323
plt.rcParams['figure.dpi'] = fig_dpi
2424
plt.rcParams['savefig.dpi'] = "figure"
25-
from IPython.display import set_matplotlib_formats
25+
26+
# IPython 7.14 deprecated set_matplotlib_formats from IPython
27+
try:
28+
from matplotlib_inline.backend_inline import set_matplotlib_formats
29+
except ImportError:
30+
# Fall back to deprecated location for older IPython versions
31+
from IPython.display import set_matplotlib_formats
32+
2633
set_matplotlib_formats(fig_format)
2734
except Exception:
2835
pass
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
title: Get SVG from matplotlib
3+
format:
4+
html:
5+
fig-format: svg
6+
_quarto:
7+
tests:
8+
html:
9+
ensureHtmlElements:
10+
- ['figure.figure img[src$=".svg"]']
11+
- ['figure.figure img[src$=".png"]']
12+
---
13+
14+
```{python}
15+
import matplotlib.pyplot as plt
16+
import numpy as np
17+
18+
fig, ax = plt.subplots()
19+
20+
x = np.linspace(0, 4 * np.pi, 100)
21+
y1 = np.sin(x)
22+
y2 = np.cos(x)
23+
24+
ax.set_title("Sine and Cosine")
25+
ax.plot(x, y1)
26+
ax.plot(x, y2)
27+
ax.legend(["Sine", "Cosine"])
28+
```

0 commit comments

Comments
 (0)