Skip to content

Commit 5971491

Browse files
committed
fix restoring visible layer after calling savefig with transparent=False
(this addresses problems with inset-maps reported in #214)
1 parent 4215659 commit 5971491

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

eomaps/eomaps.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3513,6 +3513,9 @@ def text(self, *args, layer=None, **kwargs):
35133513
@wraps(plt.savefig)
35143514
def savefig(self, *args, refetch_wms=False, rasterize_data=True, **kwargs):
35153515
"""Save the figure."""
3516+
# get the currently visible layer (to restore it after saving is done)
3517+
initial_layer = self.BM.bg_layer
3518+
35163519
if plt.get_backend() == "agg":
35173520
# make sure that a draw-event was triggered when using the agg backend
35183521
# (to avoid export-issues with some shapes)
@@ -3533,7 +3536,6 @@ def savefig(self, *args, refetch_wms=False, rasterize_data=True, **kwargs):
35333536
# add the figure background patch as the bottom layer
35343537
transparent = kwargs.get("transparent", False)
35353538
if transparent is False:
3536-
initial_layer = self.BM.bg_layer
35373539
showlayer_name = self.BM._get_showlayer_name(initial_layer)
35383540
layer_with_bg = "|".join(["__BG__", showlayer_name])
35393541
self.show_layer(layer_with_bg)
@@ -3637,25 +3639,22 @@ def savefig(self, *args, refetch_wms=False, rasterize_data=True, **kwargs):
36373639
# trigger a redraw of all savelayers to make sure unmanaged artists
36383640
# and ordinary matplotlib axes are properly drawn
36393641
self.redraw(*savelayers)
3640-
# flush events prior to savefig to avoi dissues with pending draw events
3642+
# flush events prior to savefig to avoid issues with pending draw events
36413643
# that cause wrong positioning of grid-labels and missing artists!
36423644
self.f.canvas.flush_events()
36433645
self.f._mpl_orig_savefig(*args, **kwargs)
36443646

36453647
if redraw is True:
36463648
# reset the shading-axis-size to the used figure dpi
36473649
self._update_shade_axis_size()
3650+
3651+
# restore the previous layer if background was added on save
3652+
if transparent is False:
3653+
self.show_layer(initial_layer)
3654+
36483655
# redraw after the save to ensure that backgrounds are correctly cached
36493656
self.redraw()
36503657

3651-
# restore the previous layer
3652-
elif transparent is False:
3653-
self.BM._refetch_layer("__SPINES__")
3654-
self.BM._refetch_layer("__BG__")
3655-
self.BM._refetch_layer(layer_with_bg)
3656-
self.BM.on_draw(None)
3657-
self.show_layer(initial_layer)
3658-
36593658
def fetch_layers(self, layers=None):
36603659
"""
36613660
Fetch (and cache) the layers of a map.

0 commit comments

Comments
 (0)