matplotlib / ipympl

Matplotlib Jupyter Integration
https://matplotlib.org/ipympl/
BSD 3-Clause "New" or "Revised" License
1.59k stars 225 forks source link

Matplotlib Fails to Update Axis Limits with ipywidgets in Jupyter Lab #551

Open amine-aboufirass opened 8 months ago

amine-aboufirass commented 8 months ago

I'm using ipywidgets along with matplotlib in Jupyter Lab and stumbled across some strange behavior. Here's an example of the code:

import matplotlib.pyplot as plt
import ipywidgets as widgets
%matplotlib widget
plt.ioff()

d = {
    "a": [1,2,3],
    "b": [2,3,4]
}

fig, ax = plt.subplots()

def update(change):
    data = d[change["new"]]
    ax.clear()  # Clear the previous plot
    ax.plot(data, data)
    ax.set_xlim(min(data), max(data))
    ax.set_ylim(min(data), max(data))
    ax.figure.canvas.draw()

dropdown = widgets.Dropdown(options = list(d.keys()))
dropdown.observe(update, "value")

display(dropdown, fig.canvas)
update({"new": "a"})

The steps I carry out are as follows:

Instead of snapping the plot extents back to what they are set for choice a, I instead get the following:

enter image description here

Where it looks like matplotlib has indeed plotted the data [1,2,3], but somehow failed to update the axis limits based on that data.

I really hope this isn't a bug, and that I'm doing something wrong. Has anyone seen this behavior before?

ianhi commented 7 months ago

Can you try this in a notebook but with a different interactive backend? To achieve this use %matplotlib qt in place of the %matplotlib widget, you will also need to remove the ioff or call plt.show()

I suspect that what you are experiencing is the confusing interactions with how the toolbar updates it's memory of axis limits rather than a bug in ipympl