matplotlib / matplotlib/ipympl

Matplotlib Fails to Update Axis Limits with ipywidgets in Jupyter Lab

Open
#551 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Jupyter Notebook
Stars
1.7k
Forks
234
PR merge metrics
No merged PRs in 30d

Description

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

```python
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][1]][1]

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?

[1]: https://i.stack.imgur.com/nCOHG.png

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the Jupyter Lab reproducer in the issue with the ipywidgets dropdown and Matplotlib widget backend, then follow the listed selection, pan, and reset sequence. Compare the restored axis limits with the limits set for each dataset; done means the reset view matches the currently selected data after switching from b to a.

Written by the indexing model from the issue text.

Assessment

Tech stack
jupyter, jupyter-notebook, python
Domain
data-visualization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
40/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.