Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
amine-aboufirass opened this issue Mar 30, 2024 · 1 comment

Comments

@amine-aboufirass
Copy link

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:

  • Select b from dropdown
  • Pan the plot
  • Select a from dropdown
  • Click on the "Reset original view" button on the plot

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
Copy link
Collaborator

ianhi commented Apr 2, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants