Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

ipympl (the name comes from IPython - MatPlotLib) is the modern way to display matplotlib outputs under IPython / Jupyter

executive summary

not under jupyter book

at this point, this sort of figures won’t render well under jupyter book, meaning if you read this as a static HTML document, you won’t see the figures
in that case, you are encouraged to download this notebook and run it locally on your computer

requirements

ipympl requires a separate installation with - wait for it

pip install ipympl

operating mode

you use it in a notebook in a pretty straightforward way

it is what’s called a matplotlib backend, to you turn it on using the %matplotlib magic

reload your kernel

it is important that the %matplotlib magic appears before you draw any figure

if you find yourself in a situation where you have done a figure with the the default (inline) backend, and you want to switch to ipympl afterwards, then you need to restart your kernel !

some examples

the examples below are essentially copied as-is right from the doc here https://matplotlib.org/ipympl/examples/full-example.html

the figure is natively interactive

first, let’s see the native interactive tools - in the code it is named the toolbar

Loading...

at that point, your figure is interactive:

now quickly, here are a few things you could do programatically with this toolbar

# Always hide the toolbar
fig.canvas.toolbar_visible = False

# Put it back to its default|
fig.canvas.toolbar_visible = 'fade-in-fade-out'

# Change the toolbar position
fig.canvas.toolbar_position = 'top'

# Hide the Figure name at the top of the figure
fig.canvas.header_visible = False

# Hide the footer
fig.canvas.footer_visible = False

# Disable the resizing feature
fig.canvas.resizable = False

# back on
fig.canvas.toolbar_visible = True
Loading...

changing a line plot with a slider

here’s now a more interesting example, where we create a slider to see how an external parameter impacts the figure

observe the following:

Loading...

3d plotting

the interesting things to observe in this example is

Loading...