Add rc support.

This commit is contained in:
David Bau
2022-03-28 20:47:46 -04:00
parent c12f12499e
commit 4d41cf1ffc
+15 -13
View File
@@ -24,9 +24,10 @@ class PlotWidget(Image):
For example, in the above, assigning `plot.freq = 3` will redraw the
plot with freq set to 3.
"""
def __init__(self, redraw_rule, **kwargs):
def __init__(self, redraw_rule, rc=None, **kwargs):
super().__init__()
init_args = dict(kwargs)
self.rc = {} if rc is None else rc
all_names = []
for i, (name, p) in enumerate(inspect.signature(redraw_rule).parameters.items()):
@@ -41,20 +42,21 @@ class PlotWidget(Image):
setattr(self, name, Property(default))
all_names.append(name)
old_backend = matplotlib.pyplot.get_backend()
matplotlib.pyplot.switch_backend('agg')
if 'mosaic' in init_args:
self.fig, _ = matplotlib.pyplot.subplot_mosaic(**init_args)
else:
self.fig, _ = matplotlib.pyplot.subplots(**init_args)
with matplotlib.pyplot.rc_context(rc=self.rc):
old_backend = matplotlib.pyplot.get_backend()
matplotlib.pyplot.switch_backend('agg')
if 'mosaic' in init_args:
self.fig, _ = matplotlib.pyplot.subplot_mosaic(**init_args)
else:
self.fig, _ = matplotlib.pyplot.subplots(**init_args)
matplotlib.pyplot.switch_backend(old_backend)
def invoke_redraw():
args = [self.fig]
for name in all_names:
args.append(getattr(self, name))
redraw_rule(*args)
self.render(self.fig)
with matplotlib.pyplot.rc_context(rc=self.rc):
args = [self.fig]
for name in all_names:
args.append(getattr(self, name))
redraw_rule(*args)
self.render(self.fig)
self.on(' '.join(all_names), invoke_redraw)
invoke_redraw()