Restore matplotlib backend after HoloViews matplotlib plot#1538
Restore matplotlib backend after HoloViews matplotlib plot#1538rajeeja wants to merge 4 commits into
Conversation
plot(backend='matplotlib') calls hv.extension('matplotlib'), which switches
the active matplotlib backend and clobbers the IPython inline display hook,
silently breaking subsequent native matplotlib/xarray .plot() calls. Restore
the original matplotlib backend right after the HoloViews extension switch;
HoloViews objects still display via Store.current_backend, so this is safe.
Closes #1537
There was a problem hiding this comment.
Pull request overview
Fixes a Jupyter/IPython plotting regression where uxarray.plot(..., backend="matplotlib") triggers hv.extension("matplotlib"), which can alter Matplotlib’s active backend and break subsequent native matplotlib/xarray plotting in the same session.
Changes:
- Restore the Matplotlib backend immediately after switching HoloViews to the matplotlib backend.
- Update
reset_mpl_backend()documentation to describe intended behavior. - Add a regression test covering backend restoration after a UXarray matplotlib-backed plot.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
uxarray/plot/utils.py |
Adds post-hv.extension("matplotlib") backend restoration and updates backend reset docstring. |
test/test_plot.py |
Adds a regression test asserting Matplotlib backend state and subsequent xarray plotting still works. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
ASV BenchmarkingBenchmark Comparison ResultsBenchmarks that have improved:
Benchmarks that have stayed the same:
Benchmarks that have got worse:
|
|
I was taking a quick look to see if I could review and get this merged to main, but I ended up with a couple quick questions. (1) Can you provide a small example that definitely causes the unexpected behavior mentioned in #1537 before this fix, and does not cause it anymore after this fix? On import uxarray as ux
uxds = ux.tutorial.open_dataset("outCSne30-vortex")
uxds["psi"].plot(backend='matplotlib') # makes a matplotlib-style plot
uxds["psi"].plot() # makes a matplotlib-style plot
# try switching to bokeh just in case it is related to switching back and forth:
uxds["psi"].plot(backend='bokeh') # makes a bokeh-style plot
uxds["psi"].plot() # makes a bokeh-style plot
# switch back again:
uxds["psi"].plot(backend='matplotlib') # makes a matplotlib-style plot
uxds["psi"].plot() # makes a matplotlib-style plot(2) Is it desirable for the backend to be changed "permanently" instead of just a temporary change for the current plot? I naively would expect a kwarg being passed to plot() to only affect behavior for that one plot, not to affect global state. E.g., I would expect the following: import holoviews as hv
hv.extension('bokeh')
uxds["psi"].plot() # makes a bokeh-style plot
uxds["psi"].plot(backend='matplotlib') # makes a matplotlib-style plot
uxds["psi"].plot() # I would expect a bokeh-style plot, but actually it is matplotlib-style.Please let me know if this second point belongs in a separate issue instead. |
|
Thanks. I’ll keep this separate from #1541. Test added. |
|
I agree this should stay separate from #1541. I thought both of my points were unrelated to 1541, though. For (1), I see you added regression tests, with good comments detailing how they aren't actually producing a real Jupyter notebook to see the issue occur (that would be challenging to set up with pytest). Were you able to reproduce the original issue on a real Jupyter notebook before this fix? Ideally, I would like to run a small piece of code which can reproduce the issue when in |
Calling
plot(backend="matplotlib")runshv.extension("matplotlib"), which switches the active matplotlib backend and clobbers the IPython inline display hook, silently breaking any subsequent native matplotlib/xarray.plot()calls. This restores the original matplotlib backend right after the HoloViews extension switch, which is safe because HoloViews objects display throughStore.current_backendrather than the active matplotlib backend. Verified in real Jupyter kernels that the reported sequence now works, with a new regression test and all plotting tests/relevant docs notebooks passing; closes #1537.