Skip to content

Commit

Permalink
fix restoring visible layer after calling savefig with `transparent=F…
Browse files Browse the repository at this point in the history
…alse`

(this addresses problems with inset-maps reported in #214)
  • Loading branch information
raphaelquast committed Dec 19, 2023
1 parent 4215659 commit 5971491
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions eomaps/eomaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3513,6 +3513,9 @@ def text(self, *args, layer=None, **kwargs):
@wraps(plt.savefig)
def savefig(self, *args, refetch_wms=False, rasterize_data=True, **kwargs):
"""Save the figure."""
# get the currently visible layer (to restore it after saving is done)
initial_layer = self.BM.bg_layer

Check warning on line 3517 in eomaps/eomaps.py

View check run for this annotation

Codecov / codecov/patch

eomaps/eomaps.py#L3517

Added line #L3517 was not covered by tests

if plt.get_backend() == "agg":
# make sure that a draw-event was triggered when using the agg backend
# (to avoid export-issues with some shapes)
Expand All @@ -3533,7 +3536,6 @@ def savefig(self, *args, refetch_wms=False, rasterize_data=True, **kwargs):
# add the figure background patch as the bottom layer
transparent = kwargs.get("transparent", False)
if transparent is False:
initial_layer = self.BM.bg_layer
showlayer_name = self.BM._get_showlayer_name(initial_layer)
layer_with_bg = "|".join(["__BG__", showlayer_name])
self.show_layer(layer_with_bg)
Expand Down Expand Up @@ -3637,25 +3639,22 @@ def savefig(self, *args, refetch_wms=False, rasterize_data=True, **kwargs):
# trigger a redraw of all savelayers to make sure unmanaged artists
# and ordinary matplotlib axes are properly drawn
self.redraw(*savelayers)
# flush events prior to savefig to avoi dissues with pending draw events
# flush events prior to savefig to avoid issues with pending draw events
# that cause wrong positioning of grid-labels and missing artists!
self.f.canvas.flush_events()
self.f._mpl_orig_savefig(*args, **kwargs)

if redraw is True:
# reset the shading-axis-size to the used figure dpi
self._update_shade_axis_size()

# restore the previous layer if background was added on save
if transparent is False:
self.show_layer(initial_layer)

Check warning on line 3653 in eomaps/eomaps.py

View check run for this annotation

Codecov / codecov/patch

eomaps/eomaps.py#L3652-L3653

Added lines #L3652 - L3653 were not covered by tests

# redraw after the save to ensure that backgrounds are correctly cached
self.redraw()

# restore the previous layer
elif transparent is False:
self.BM._refetch_layer("__SPINES__")
self.BM._refetch_layer("__BG__")
self.BM._refetch_layer(layer_with_bg)
self.BM.on_draw(None)
self.show_layer(initial_layer)

def fetch_layers(self, layers=None):
"""
Fetch (and cache) the layers of a map.
Expand Down

0 comments on commit 5971491

Please sign in to comment.