Skip to content

Commit 2074eb6

Browse files
committed
Remove matplotlib examples
These are broken because using Matplotlib in output widgets is brittle. The right approach might be to rewrite these with ipympl.
1 parent 0391603 commit 2074eb6

File tree

3 files changed

+0
-211
lines changed

3 files changed

+0
-211
lines changed

docs/source/tutorials.rst

-126
Original file line numberDiff line numberDiff line change
@@ -95,129 +95,3 @@ We note the following:
9595
To render different widgets conditionally, we anchor a container (an ``HBox``)
9696
and swap the container's children.
9797

98-
99-
Using IPywidget server with Matplotlib
100-
--------------------------------------
101-
102-
Let's build a simple application that updates a matplotlib plot every second. We
103-
use an ``Output`` widget to get capture the matplotlib plot. We then embed our
104-
output widget in a top-level container. Every second, we generate a new output
105-
widget containing a new plot and swap it into the container. The code for this
106-
example is in `examples/matplotlib_random
107-
<https://github.com/pbugnion/ipywidgets_server/tree/master/examples/matplotlib_random>`_::
108-
109-
# example.py
110-
111-
import time
112-
113-
import matplotlib.pyplot as plt
114-
115-
import numpy as np
116-
117-
import ipywidgets as widgets
118-
from IPython.display import display
119-
120-
SIZE = 50
121-
XBASIS = np.linspace(0.0, 1.0, SIZE)
122-
123-
container = widgets.VBox()
124-
125-
def update():
126-
""" Generate a new random plot and embed it into the container """
127-
output = widgets.Output()
128-
with output:
129-
fig, ax = plt.subplots(figsize=(12, 8))
130-
ax.plot(XBASIS, np.random.rand(SIZE))
131-
ax.set_ylim(0.0, 1.0)
132-
plt.show()
133-
container.children = [output]
134-
135-
display(container)
136-
137-
while True:
138-
# Update the plot in a busy loop
139-
time.sleep(1)
140-
update()
141-
142-
143-
Save this script to a file called `example.py`. You can then run::
144-
145-
$ ipywidgets-server example:container
146-
147-
Head over to ``http://127.0.0.1:8866`` in your browser. You should see the widget.
148-
149-
.. image:: images/matplotlib-simple.png
150-
151-
For a more complex example, let's build a widget to explore how the `sin`
152-
changes depending on the parameters that are passed. We will plot ``a *
153-
sin(k*x)``, with sliders to change the value of ``a`` and ``k``. The code for
154-
this example is also available at `examples/matplotlib_sine_waves
155-
<https://github.com/pbugnion/ipywidgets_server/tree/master/examples/matplotlib_sine_waves>`_::
156-
157-
# example.py
158-
159-
import matplotlib.pyplot as plt
160-
161-
import numpy as np
162-
163-
import ipywidgets as widgets
164-
165-
XBASIS = np.linspace(-2*np.pi, 2*np.pi)
166-
167-
168-
class SineRenderer(object):
169-
170-
def __init__(self):
171-
self._amplitude_slider = widgets.FloatSlider(
172-
1.0, min=-2.0, max=2.0, description='amplitude'
173-
)
174-
self._frequency_slider = widgets.FloatSlider(
175-
1.0, min=0.1, max=3.0, description='frequency'
176-
)
177-
self._bind_callbacks()
178-
self._controls_container = widgets.VBox([
179-
self._amplitude_slider,
180-
self._frequency_slider
181-
])
182-
self._plot_container = widgets.HBox([])
183-
self._application_container = widgets.HBox([
184-
self._controls_container, self._plot_container
185-
])
186-
187-
def _bind_callbacks(self):
188-
self._amplitude_slider.observe(
189-
self._on_param_change, names='value')
190-
self._frequency_slider.observe(
191-
self._on_param_change, names='value')
192-
193-
def _on_param_change(self, change):
194-
self.render()
195-
196-
def render(self, change=None):
197-
amplitude = self._amplitude_slider.value
198-
frequency = self._frequency_slider.value
199-
output = widgets.Output()
200-
with output:
201-
fig, ax = plt.subplots(figsize=(12, 8))
202-
ax.plot(XBASIS, amplitude * np.sin(frequency*XBASIS))
203-
ax.set_ylim(-2.5, 2.5)
204-
plt.show()
205-
self._plot_container.children = [output]
206-
return self._application_container
207-
208-
209-
container = SineRenderer().render()
210-
211-
Save this script to a file called `example.py`. You can then run::
212-
213-
$ ipywidgets-server example:container
214-
215-
.. image:: images/matplotlib-sine.png
216-
217-
It is worth noting the following:
218-
219-
- we wrap the application into a controller class responsible both for generating the view and for reacting to user actions. Using a class provides better encapsulation and re-use.
220-
- in the class constructor, we handle rendering the static components of the view. We create two container widgets, one to hold the sliders and one to hold the plot. We stack these two containers in an ``HBox``, the top level widget holding our application.
221-
- We handle reacting to changes in the sliders by `observing` the ``value`` traitlet of the slider. The ``.observe`` method takes a callback as first argument. The callback that we pass in just re-renders the plot. The second argument to ``.observe`` is a list of attributes of the slider to observe. We only want to react to changes in the slider value (rather than, say, its maximum or minimum).
222-
- The ``render`` method of our application renders the dynamic components and returns the top level widget.
223-

examples/matplotlib_random/example.py

-33
This file was deleted.

examples/matplotlib_sine_waves/example.py

-52
This file was deleted.

0 commit comments

Comments
 (0)