Skip to content

Commit df7f0ab

Browse files
committed
Use rc_context more.
rather than manually restoring rcParams at exit.
1 parent 03b255d commit df7f0ab

File tree

4 files changed

+43
-40
lines changed

4 files changed

+43
-40
lines changed

doc/api/next_api_changes/deprecations.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,7 @@ it via the *cmap* parameter:
6363
``DateFormatter.illegal_s``
6464
~~~~~~~~~~~~~~~~~~~~~~~~~~~
6565
This attribute is unused and deprecated.
66+
67+
``widgets.TextBox.params_to_disable``
68+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
69+
This attribute is deprecated.

lib/matplotlib/animation.py

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,33 +1102,31 @@ def func(current_frame: int, total_frames: int) -> Any
11021102
# TODO: Right now, after closing the figure, saving a movie won't work
11031103
# since GUI widgets are gone. Either need to remove extra code to
11041104
# allow for this non-existent use case or find a way to make it work.
1105-
with mpl.rc_context():
1106-
if mpl.rcParams['savefig.bbox'] == 'tight':
1107-
_log.info("Disabling savefig.bbox = 'tight', as it may cause "
1108-
"frame size to vary, which is inappropriate for "
1109-
"animation.")
1110-
mpl.rcParams['savefig.bbox'] = None
1111-
with writer.saving(self._fig, filename, dpi):
1112-
for anim in all_anim:
1113-
# Clear the initial frame
1114-
anim._init_draw()
1115-
frame_number = 0
1116-
# TODO: Currently only FuncAnimation has a save_count
1117-
# attribute. Can we generalize this to all Animations?
1118-
save_count_list = [getattr(a, 'save_count', None)
1119-
for a in all_anim]
1120-
if None in save_count_list:
1121-
total_frames = None
1122-
else:
1123-
total_frames = sum(save_count_list)
1124-
for data in zip(*[a.new_saved_frame_seq() for a in all_anim]):
1125-
for anim, d in zip(all_anim, data):
1126-
# TODO: See if turning off blit is really necessary
1127-
anim._draw_next_frame(d, blit=False)
1128-
if progress_callback is not None:
1129-
progress_callback(frame_number, total_frames)
1130-
frame_number += 1
1131-
writer.grab_frame(**savefig_kwargs)
1105+
if mpl.rcParams['savefig.bbox'] == 'tight':
1106+
_log.info("Disabling savefig.bbox = 'tight', as it may cause "
1107+
"frame size to vary, which is inappropriate for "
1108+
"animation.")
1109+
with mpl.rc_context({'savefig.bbox': None}), \
1110+
writer.saving(self._fig, filename, dpi):
1111+
for anim in all_anim:
1112+
anim._init_draw() # Clear the initial frame
1113+
frame_number = 0
1114+
# TODO: Currently only FuncAnimation has a save_count
1115+
# attribute. Can we generalize this to all Animations?
1116+
save_count_list = [getattr(a, 'save_count', None)
1117+
for a in all_anim]
1118+
if None in save_count_list:
1119+
total_frames = None
1120+
else:
1121+
total_frames = sum(save_count_list)
1122+
for data in zip(*[a.new_saved_frame_seq() for a in all_anim]):
1123+
for anim, d in zip(all_anim, data):
1124+
# TODO: See if turning off blit is really necessary
1125+
anim._draw_next_frame(d, blit=False)
1126+
if progress_callback is not None:
1127+
progress_callback(frame_number, total_frames)
1128+
frame_number += 1
1129+
writer.grab_frame(**savefig_kwargs)
11321130

11331131
# Reconnect signal for first draw if necessary
11341132
if reconnect_first_draw:

lib/matplotlib/pyplot.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,11 +1223,9 @@ def subplot_tool(targetfig=None):
12231223
if targetfig is None:
12241224
targetfig = gcf()
12251225

1226-
tbar = rcParams['toolbar'] # Turn off navigation toolbar for the toolfig.
1227-
rcParams['toolbar'] = 'None'
1228-
toolfig = figure(figsize=(6, 3))
1226+
with rc_context({'toolbar': 'None'}): # No nav toolbar for the toolfig.
1227+
toolfig = figure(figsize=(6, 3))
12291228
toolfig.subplots_adjust(top=0.9)
1230-
rcParams['toolbar'] = tbar
12311229

12321230
if hasattr(targetfig.canvas, "manager"): # Restore the current figure.
12331231
_pylab_helpers.Gcf.set_active(targetfig.canvas.manager)

lib/matplotlib/widgets.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

1616
import numpy as np
1717

18-
from . import cbook, rcParams
18+
import matplotlib as mpl
19+
from . import cbook
1920
from .lines import Line2D
2021
from .patches import Circle, Rectangle, Ellipse
2122
from .transforms import blended_transform_factory
@@ -678,6 +679,11 @@ class TextBox(AxesWidget):
678679
The color of the text box when hovering.
679680
"""
680681

682+
@cbook.deprecated("3.3")
683+
@property
684+
def params_to_disable(self):
685+
return [key for key in mpl.rcParams if 'keymap' in key]
686+
681687
def __init__(self, ax, label, initial='',
682688
color='.95', hovercolor='1', label_pad=.01):
683689
"""
@@ -700,8 +706,6 @@ def __init__(self, ax, label, initial='',
700706

701707
self.DIST_FROM_LEFT = .05
702708

703-
self.params_to_disable = [key for key in rcParams if 'keymap' in key]
704-
705709
self.text = initial
706710
self.label = ax.text(-label_pad, 0.5, label,
707711
verticalalignment='center',
@@ -845,10 +849,10 @@ def begin_typing(self, x):
845849
if self.ax.figure.canvas.manager.key_press_handler_id is not None:
846850
# disable command keys so that the user can type without
847851
# command keys causing figure to be saved, etc
848-
self.reset_params = {}
849-
for key in self.params_to_disable:
850-
self.reset_params[key] = rcParams[key]
851-
rcParams[key] = []
852+
self._restore_keymap = ExitStack()
853+
self._restore_keymap.enter_context(
854+
mpl.rc_context(
855+
{k: [] for k in mpl.rcParams if k.startswith('keymap.')}))
852856
else:
853857
self.ax.figure.canvas.manager.toolmanager.keypresslock(self)
854858

@@ -861,8 +865,7 @@ def stop_typing(self):
861865
if self.ax.figure.canvas.manager.key_press_handler_id is not None:
862866
# since the user is no longer typing,
863867
# reactivate the standard command keys
864-
for key in self.params_to_disable:
865-
rcParams[key] = self.reset_params[key]
868+
self._restore_keymap.close()
866869
else:
867870
toolmanager = self.ax.figure.canvas.manager.toolmanager
868871
toolmanager.keypresslock.release(self)

0 commit comments

Comments
 (0)