In JupyterLab how to make `%matplotlib widget` *Download plot* saves a SVG instead of a PNG?
Feb 22, 10:38pm

In JupyterLab, when using the %matplotlib widget backend, the "Download plot" button typically saves the plot as a PNG by default. To save the plot as an SVG instead, you need to configure the matplotlib backend or use a workaround, since the widget backend does not natively support SVG downloads directly from the toolbar.

Workaround: Save as SVG Programmatically

You can manually save the plot as an SVG using Python code. Here’s how:

  1. Plot your figure as usual with %matplotlib widget.
  2. Save the figure as SVG using fig.savefig():
python
%matplotlib widget
import matplotlib.pyplot as plt

# Create your plot
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [4, 5, 6])

# Save as SVG
fig.savefig("plot.svg", format="svg", dpi=1200, bbox_inches="tight")
  1. Download the SVG file from JupyterLab’s file browser.

Alternative: Use %matplotlib inline for SVG Downloads

If you want the "Download plot" button to save as SVG, switch to the %matplotlib inline backend:

python
%matplotlib inline
import matplotlib.pyplot as plt

# Create your plot
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [4, 5, 6])

# The "Download plot" button will now save as SVG
  • With %matplotlib inline, the "Download plot" button in JupyterLab will save the plot as an SVG by default.

Why This Happens

  • The %matplotlib widget backend uses a canvas-based renderer, which does not support SVG downloads directly from the toolbar.
  • The %matplotlib inline backend renders plots as SVG in the notebook, so the download button works as expected.
Feb 22, 10:38pm

This is a shared conversation and may include unverified content that doesn't reflect Mistral AI's views. Login to resume chatting.