.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "11_demos\python_packages\matplotlib\demo_oscilloscope.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_11_demos_python_packages_matplotlib_demo_oscilloscope.py: Oscilloscope =============== .. GENERATED FROM PYTHON SOURCE LINES 5-65 .. container:: sphx-glr-animation .. raw:: html
.. code-block:: Python import matplotlib.animation as animation import matplotlib.pyplot as plt import numpy as np from matplotlib.lines import Line2D class Scope: def __init__(self, ax, maxt=2, dt=0.02): self.ax = ax self.dt = dt self.maxt = maxt self.tdata = [0] self.ydata = [0] self.line = Line2D(self.tdata, self.ydata) self.ax.add_line(self.line) self.ax.set_ylim(-0.1, 1.1) self.ax.set_xlim(0, self.maxt) def update(self, y): lastt = self.tdata[-1] if lastt >= self.tdata[0] + self.maxt: # reset the arrays self.tdata = [self.tdata[-1]] self.ydata = [self.ydata[-1]] self.ax.set_xlim(self.tdata[0], self.tdata[0] + self.maxt) self.ax.figure.canvas.draw() # This slightly more complex calculation avoids floating-point issues # from just repeatedly adding `self.dt` to the previous value. t = self.tdata[0] + len(self.tdata) * self.dt self.tdata.append(t) self.ydata.append(y) self.line.set_data(self.tdata, self.ydata) return (self.line,) def emitter(p=0.1): """Return a random value in [0, 1) with probability p, else 0.""" while True: v = np.random.rand() if v > p: yield 0.0 else: yield np.random.rand() # Fixing random state for reproducibility np.random.seed(19680801 // 10) fig, ax = plt.subplots() scope = Scope(ax) # pass a generator in "emitter" to produce data for the update func ani = animation.FuncAnimation( fig, scope.update, emitter, interval=50, blit=True, save_count=100 ) plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 5.995 seconds) .. _sphx_glr_download_11_demos_python_packages_matplotlib_demo_oscilloscope.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_oscilloscope.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_oscilloscope.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_oscilloscope.zip `