Skip to content

Restore matplotlib backend after HoloViews matplotlib plot#1538

Open
rajeeja wants to merge 7 commits into
mainfrom
rajeeja/fix_matplotlib_backend
Open

Restore matplotlib backend after HoloViews matplotlib plot#1538
rajeeja wants to merge 7 commits into
mainfrom
rajeeja/fix_matplotlib_backend

Conversation

@rajeeja

@rajeeja rajeeja commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Calling plot(backend="matplotlib") runs hv.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 through Store.current_backend rather 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.

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread uxarray/plot/utils.py
Comment thread uxarray/plot/utils.py
Comment thread test/test_plot.py Outdated
@cmdupuis3 cmdupuis3 added the run-benchmark Run ASV benchmark workflow label Jul 2, 2026
@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown

ASV Benchmarking

Benchmark Comparison Results

Benchmarks that have improved:

Change Before [a7509c3] After [cc682d6] Ratio Benchmark (Parameter)
- 588M 395M 0.67 face_bounds.FaceBounds.peakmem_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/ugrid/geoflow-small/grid.nc'))
- 709M 394M 0.56 face_bounds.FaceBounds.peakmem_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/ugrid/quad-hexagon/grid.nc'))
- 499M 386M 0.77 mpas_ocean.Gradient.peakmem_gradient('480km')

Benchmarks that have stayed the same:

Change Before [a7509c3] After [cc682d6] Ratio Benchmark (Parameter)
7.84±0.04μs 7.67±0.03μs 0.98 bench_connectivity.Connectivity.time_edge_face('120km')
7.95±0.1μs 7.82±0.06μs 0.98 bench_connectivity.Connectivity.time_edge_face('480km')
7.79±0.05μs 7.68±0.07μs 0.99 bench_connectivity.Connectivity.time_edge_node('120km')
7.88±0.07μs 7.93±0.1μs 1.01 bench_connectivity.Connectivity.time_edge_node('480km')
7.84±0.07μs 7.99±0.2μs 1.02 bench_connectivity.Connectivity.time_face_edge('120km')
7.88±0.04μs 7.95±0.09μs 1.01 bench_connectivity.Connectivity.time_face_edge('480km')
7.79±0.1μs 7.87±0.04μs 1.01 bench_connectivity.Connectivity.time_face_face('120km')
7.95±0.06μs 7.90±0.2μs 0.99 bench_connectivity.Connectivity.time_face_face('480km')
15.5±0.2μs 15.7±0.1μs 1.01 bench_connectivity.Connectivity.time_face_node('120km')
16.0±0.05μs 15.9±0.09μs 0.99 bench_connectivity.Connectivity.time_face_node('480km')
7.66±0.04μs 7.79±0.2μs 1.02 bench_connectivity.Connectivity.time_node_edge('120km')
7.86±0.01μs 7.99±0.1μs 1.02 bench_connectivity.Connectivity.time_node_edge('480km')
7.84±0.1μs 7.80±0.09μs 0.99 bench_connectivity.Connectivity.time_node_face('120km')
7.92±0.1μs 7.88±0.1μs 0.99 bench_connectivity.Connectivity.time_node_face('480km')
392M 392M 1 face_bounds.FaceBounds.peakmem_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/mpas/QU/oQU480.231010.nc'))
422M 425M 1.01 face_bounds.FaceBounds.peakmem_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/scrip/outCSne8/outCSne8.nc'))
10.2±0.2ms 9.92±0.03ms 0.98 face_bounds.FaceBounds.time_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/mpas/QU/oQU480.231010.nc'))
2.68±0.07ms 2.66±0.02ms 0.99 face_bounds.FaceBounds.time_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/scrip/outCSne8/outCSne8.nc'))
13.4±0.05ms 13.5±0.09ms 1.01 face_bounds.FaceBounds.time_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/ugrid/geoflow-small/grid.nc'))
1.54±0.02ms 1.60±0.05ms 1.04 face_bounds.FaceBounds.time_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/ugrid/quad-hexagon/grid.nc'))
778±3ms 782±3ms 1.01 import.Imports.timeraw_import_uxarray
665±3ns 653±3ns 0.98 mpas_ocean.CheckNorm.time_check_norm('120km')
647±3ns 660±20ns 1.02 mpas_ocean.CheckNorm.time_check_norm('480km')
549±1ms 557±5ms 1.01 mpas_ocean.ConnectivityConstruction.time_face_face_connectivity('120km')
34.5±0.1ms 34.3±0.1ms 0.99 mpas_ocean.ConnectivityConstruction.time_face_face_connectivity('480km')
525±10μs 525±6μs 1 mpas_ocean.ConnectivityConstruction.time_n_nodes_per_face('120km')
437±7μs 435±8μs 1 mpas_ocean.ConnectivityConstruction.time_n_nodes_per_face('480km')
3.54±0.02ms 3.50±0.03ms 0.99 mpas_ocean.ConstructFaceLatLon.time_cartesian_averaging('120km')
2.68±0.03ms 2.68±0.03ms 1 mpas_ocean.ConstructFaceLatLon.time_cartesian_averaging('480km')
2.58±0.01s 2.60±0.03s 1.01 mpas_ocean.ConstructFaceLatLon.time_welzl('120km')
167±3ms 164±1ms 0.98 mpas_ocean.ConstructFaceLatLon.time_welzl('480km')
15.7±0.4ms 15.3±0.02ms 0.97 mpas_ocean.ConstructTreeStructures.time_ball_tree('120km')
847±10μs 857±7μs 1.01 mpas_ocean.ConstructTreeStructures.time_ball_tree('480km')
8.24±0.01ms 8.23±0.02ms 1 mpas_ocean.ConstructTreeStructures.time_kd_tree('120km')
571±20μs 582±9μs 1.02 mpas_ocean.ConstructTreeStructures.time_kd_tree('480km')
508±5ms 510±0.8ms 1 mpas_ocean.CrossSections.time_const_lat('120km', 1)
257±1ms 257±0.8ms 1 mpas_ocean.CrossSections.time_const_lat('120km', 2)
133±0.2ms 133±0.4ms 1 mpas_ocean.CrossSections.time_const_lat('120km', 4)
377±0.9ms 375±0.8ms 0.99 mpas_ocean.CrossSections.time_const_lat('480km', 1)
190±0.9ms 190±2ms 1 mpas_ocean.CrossSections.time_const_lat('480km', 2)
98.1±0.2ms 98.1±0.2ms 1 mpas_ocean.CrossSections.time_const_lat('480km', 4)
16.8±0.02ms 16.9±0.03ms 1 mpas_ocean.DualMesh.time_dual_mesh_construction('120km')
1.98±0.02ms 1.97±0.02ms 1 mpas_ocean.DualMesh.time_dual_mesh_construction('480km')
644±2ms 649±1ms 1.01 mpas_ocean.GeoDataFrame.time_to_geodataframe('120km', False)
39.3±0.6ms 39.1±0.6ms 0.99 mpas_ocean.GeoDataFrame.time_to_geodataframe('120km', True)
55.8±0.4ms 56.1±0.2ms 1.01 mpas_ocean.GeoDataFrame.time_to_geodataframe('480km', False)
4.19±0.04ms 4.18±0.05ms 1 mpas_ocean.GeoDataFrame.time_to_geodataframe('480km', True)
406M 407M 1 mpas_ocean.Gradient.peakmem_gradient('120km')
129±0.8ms 131±2ms 1.01 mpas_ocean.Gradient.time_gradient('120km')
8.83±0.02ms 9.34±0.5ms 1.06 mpas_ocean.Gradient.time_gradient('480km')
144±3μs 148±2μs 1.03 mpas_ocean.HoleEdgeIndices.time_construct_hole_edge_indices('120km')
66.8±0.4μs 67.3±0.6μs 1.01 mpas_ocean.HoleEdgeIndices.time_construct_hole_edge_indices('480km')
354M 354M 1 mpas_ocean.Integrate.peakmem_integrate('120km')
333M 333M 1 mpas_ocean.Integrate.peakmem_integrate('480km')
139±0.8μs 137±0.7μs 0.99 mpas_ocean.Integrate.time_integrate('120km')
122±2μs 122±2μs 1 mpas_ocean.Integrate.time_integrate('480km')
103±1ms 105±1ms 1.02 mpas_ocean.MatplotlibConversion.time_dataarray_to_polycollection('120km', 'exclude')
103±1ms 104±2ms 1.01 mpas_ocean.MatplotlibConversion.time_dataarray_to_polycollection('120km', 'include')
103±0.8ms 104±0.6ms 1 mpas_ocean.MatplotlibConversion.time_dataarray_to_polycollection('120km', 'split')
8.02±0.06ms 8.28±0.1ms 1.03 mpas_ocean.MatplotlibConversion.time_dataarray_to_polycollection('480km', 'exclude')
8.07±0.1ms 8.12±0.07ms 1.01 mpas_ocean.MatplotlibConversion.time_dataarray_to_polycollection('480km', 'include')
8.03±0.06ms 8.16±0.04ms 1.02 mpas_ocean.MatplotlibConversion.time_dataarray_to_polycollection('480km', 'split')
219±1μs 220±1μs 1 mpas_ocean.PointInPolygon.time_face_search_lonlat('120km')
217±1μs 217±0.8μs 1 mpas_ocean.PointInPolygon.time_face_search_lonlat('480km')
207±2μs 208±0.8μs 1.01 mpas_ocean.PointInPolygon.time_face_search_xyz('120km')
208±0.6μs 207±1μs 1 mpas_ocean.PointInPolygon.time_face_search_xyz('480km')
164±0.07ms 162±1ms 0.99 mpas_ocean.RemapDownsample.time_bilinear_remapping
176±0.5ms 176±0.5ms 1 mpas_ocean.RemapDownsample.time_inverse_distance_weighted_remapping
3.06±0ms 3.05±0.01ms 1 mpas_ocean.RemapDownsample.time_nearest_neighbor_remapping
920±2ms 922±5ms 1 mpas_ocean.RemapUpsample.time_bilinear_remapping
36.5±0.4ms 36.2±0.4ms 0.99 mpas_ocean.RemapUpsample.time_inverse_distance_weighted_remapping
6.39±0.1ms 6.38±0.1ms 1 mpas_ocean.RemapUpsample.time_nearest_neighbor_remapping
23.0±0.3ms 22.5±0.4ms 0.98 mpas_ocean.ZonalAverage.time_zonal_average('120km')
4.59±0.05ms 4.59±0.04ms 1 mpas_ocean.ZonalAverage.time_zonal_average('480km')
331M 329M 0.99 quad_hexagon.QuadHexagon.peakmem_open_dataset
328M 328M 1 quad_hexagon.QuadHexagon.peakmem_open_grid
4.99±0.05ms 4.99±0.1ms 1 quad_hexagon.QuadHexagon.time_open_dataset
4.23±0.05ms 4.22±0.06ms 1 quad_hexagon.QuadHexagon.time_open_grid

@Sevans711

Copy link
Copy Markdown
Collaborator

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 main currently (i.e. without this fix yet), in a Jupyter notebook, I ran the following, putting each plot() call in its own cell. I tried in Python 3.10 and Python 3.13. In both cases, every plot call displays a plot; I can't reproduce the failure to make plots. Each of these calls successfully makes a plot.

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.

@rajeeja

rajeeja commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

Thanks. I’ll keep this separate from #1541. Test added.

@Sevans711

Copy link
Copy Markdown
Collaborator

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 main in a real Jupyter notebook; then change to this branch, rerun the code, and see that the issue does not occur anymore.

@rajeeja rajeeja moved this to 👀 In review in UXarray Development Jul 8, 2026
@Sevans711

Copy link
Copy Markdown
Collaborator

Ah, I misread the original bug. It isn't saying that uxarray.UxDataArray.plot() fails, it's saying that xarray.DataArray.plot() fails. I believe your changes so far have not fixed this bug. From within this branch, I still see the bug:

Screenshot 2026-07-09 at 11 15 13 AM

What I would expect, once the bug has been fixed, would be for the xr.DataArray([1,2,3]).plot() cell to show a plot, like it does if it is run before calling any uxarray plotting routines:

Screenshot 2026-07-09 at 11 11 51 AM

@rajeeja rajeeja requested a review from Sevans711 July 9, 2026 15:52
@rajeeja

rajeeja commented Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

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 main, the UXarray plot changes Matplotlib from inline to agg, and the later native Matplotlib plot warns that FigureCanvasAgg is non-interactive.

On this branch, the backend is restored to inline, and the later native Matplotlib plot displays normally.

@Sevans711

Copy link
Copy Markdown
Collaborator

Ah, I can confirm, if I explicitly include %matplotlib inline then things work on this branch and not on main.

However, when I don't include it, even on this branch, the results are that no plot gets shown:
Screenshot 2026-07-09 at 12 54 42 PM

I basically never explicitly write out %matplotlib inline in my notebooks. Can we make a fix that works even when you don't include that line?

rajeeja added 2 commits July 9, 2026 17:17
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.
@Sevans711

Copy link
Copy Markdown
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run-benchmark Run ASV benchmark workflow

Projects

Status: 👀 In review

Development

Successfully merging this pull request may close these issues.

simple uxarray.plot(backend="matplotlib") seems to break subsequent xarray.plot functionality

5 participants