Restore matplotlib backend after HoloViews matplotlib plot#1538
Restore matplotlib backend after HoloViews matplotlib plot#1538rajeeja wants to merge 7 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:
|
|
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 |
|
Yes — I reproduced this in notebook-style execution. Minimal check: %matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
import uxarray as ux
uxds = ux.tutorial.open_dataset("outCSne30-vortex")
print("initial:", mpl.get_backend())
uxds["psi"].plot(backend="matplotlib")
print("after uxarray:", mpl.get_backend())
plt.figure()
plt.plot([0, 1], [0, 1])
plt.show()On On this branch, the backend is restored to |
mpl.use() restores the matplotlib backend name but does not re-register
IPython's inline display integration that hv.extension('matplotlib')
clobbers. In a Jupyter kernel without an explicit %matplotlib inline,
native matplotlib/xarray .plot() calls after a uxarray matplotlib plot
still failed to render. Re-run configure_inline_support when the restored
backend is inline so the display hook is reinstated. See #1537.
|
Thank you for working through all these unexpected complications…. I was reviewing and ready to approve, but then somehow discovered another failure case. First of all, your fix looks good for what we observed already. I confirmed on my end that the following runs and creates a plot, as expected: # intentionally skipping: %matplotlib inline
import matplotlib.pyplot as plt
import uxarray as ux
uxds = ux.tutorial.open_dataset("outCSne30-vortex")
uxds["psi"].plot(backend="matplotlib")
plt.figure()
plt.plot([0, 1], [0, 1])
plt.show()Now, for the failure case: it is about what happens when you don't call plt.show() explicitly. When I remove it, I don't see any plot: import matplotlib.pyplot as plt
import uxarray as ux
uxds = ux.tutorial.open_dataset("outCSne30-vortex")
uxds["psi"].plot(backend="matplotlib")
plt.figure() # this line isn't actually necessary but it doesn't hurt.
plt.plot([0, 1], [0, 1])This leads to not displaying any figure. The issue also persists across all subsequent matplotlib plots in the notebook as well, for example doing plt.plot([1,2,3,2,1]) in another cell will lead to no plot being displayed. Calling plt.show() at a later time will show all plots that have been created since the previous time plt.show() was called. That's not ideal; normally I expect Jupyter to display plots if they are the last line in the cell. E.g., in a fresh kernel, if I run the following: import matplotlib.pyplot as plt
plt.plot([0,1],[0,1])Then a plot gets displayed. I usually don't call plt.show() explicitly, because it's more convenient to just end the cell with the plot object. I'm not sure if this will help with debugging or make things more confusing, but I also noticed that if you call plt.plot() before making your first uxarray plot, then plt.show() remains unnecessary. For example: import matplotlib.pyplot as plt
import uxarray as ux
plt.plot([1,2,3,2,1])
uxds = ux.tutorial.open_dataset("outCSne30-vortex")
uxds["psi"].plot(backend="matplotlib")
plt.figure() # this line isn't actually necessary but it doesn't hurt.
plt.plot([0, 1], [0, 1])This will show two figures, first the [1,2,3,2,1] plot, then the second plot. All subsequent calls to plt.plot() do not seem to require plt.show(), which is good. |



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.