diff --git a/baukit/plotwidget.py b/baukit/plotwidget.py index 5fb40bb..b6a4b20 100644 --- a/baukit/plotwidget.py +++ b/baukit/plotwidget.py @@ -1,5 +1,4 @@ from .labwidget import Image, Property -import matplotlib, matplotlib.pyplot import inspect class PlotWidget(Image): @@ -25,10 +24,12 @@ class PlotWidget(Image): plot with freq set to 3. """ def __init__(self, redraw_rule, rc=None, **kwargs): + import matplotlib, matplotlib.pyplot super().__init__() init_args = dict(kwargs) - render_args = dict(format='svg', figsize=(5, 3.5)) - self.rc = {} if rc is None else rc + render_args = dict(format='svg') + if rc is None: + rc = {} all_names = [] for i, (name, p) in enumerate(inspect.signature(redraw_rule).parameters.items()): @@ -48,7 +49,11 @@ class PlotWidget(Image): if name in init_args: render_args[name] = init_args.pop(name) - with matplotlib.pyplot.rc_context(rc=self.rc): + for default_arg, default_value in [('figsize', (5, 3.5))]: + if default_arg not in init_args: + init_args[default_arg] = default_value + + with matplotlib.pyplot.rc_context(rc=rc): old_backend = matplotlib.pyplot.get_backend() matplotlib.pyplot.switch_backend('agg') if 'mosaic' in init_args: @@ -58,7 +63,7 @@ class PlotWidget(Image): matplotlib.pyplot.switch_backend(old_backend) def invoke_redraw(): - with matplotlib.pyplot.rc_context(rc=self.rc): + with matplotlib.pyplot.rc_context(rc=rc): args = [self.fig] for name in all_names: args.append(getattr(self, name)) diff --git a/baukit/show.py b/baukit/show.py index 694e92a..dbe3323 100644 --- a/baukit/show.py +++ b/baukit/show.py @@ -20,7 +20,6 @@ import base64 import collections from contextlib import contextmanager import io -import IPython import types import re import sys diff --git a/notebooks/using_show_and_widgets.ipynb b/notebooks/using_show_and_widgets.ipynb index cd3d3cc..dbe574d 100644 --- a/notebooks/using_show_and_widgets.ipynb +++ b/notebooks/using_show_and_widgets.ipynb @@ -339,7 +339,7 @@ "id": "498e7872", "metadata": {}, "source": [ - "You can also respond to any property change using the `on()` method. Here we trace whenever ra2 changes." + "You can also respond to any property change using the `on()` method. Here we show a message whenever ra3 changes." ] }, { @@ -349,11 +349,16 @@ "metadata": {}, "outputs": [], "source": [ - "def say_hello():\n", - " print(f'ra3 is {ra3.value}')\n", + "from baukit import Div\n", + "\n", + "div = Div('change the slider')\n", "ra3 = Range()\n", - "ra3.on('value', say_hello)\n", - "show(ra3)" + "\n", + "def print_value():\n", + " div.innerHTML = f'the value is {ra3.value}'\n", + "ra3.on('value', print_value)\n", + "\n", + "show([[div, show.style(flex=7), ra3]])" ] }, { @@ -383,14 +388,14 @@ "def myplot(plt, frequency=1.0, amplitude=1.0):\n", " [ax] = plt.axes\n", " ax.clear()\n", - " ax.set_title(f'Sine wave of frequency {frequency}, amplitude {amplitude}')\n", + " ax.set_title(f'Sine wave of frequency {frequency:.2f}, amplitude {amplitude:.2f}')\n", " x = torch.linspace(0, 20, 300)\n", " y = (frequency * x).sin()\n", " ax.plot(x, amplitude * y)\n", " ax.set_ylim(-5, 5)\n", "\n", - "show([[PlotWidget(myplot, figsize=(5,3.5)),\n", - " PlotWidget(myplot, frequency=2, amplitude=3, figsize=(5,3.5))]])" + "show([[PlotWidget(myplot),\n", + " PlotWidget(myplot, frequency=2, amplitude=3)]])" ] }, {