.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/Widgets/plot_widget1d_point.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_auto_examples_Widgets_plot_widget1d_point.py: 1D Point Widget =============== A free-moving ``(x, y)`` control point on a 1-D panel, with an optional crosshair. Unlike the vertical / horizontal line widgets it moves in both directions, so it is handy as a draggable data cursor or a control handle for an interactive fit. Add it with :meth:`~anyplotlib.Plot1D.add_point_widget`. .. GENERATED FROM PYTHON SOURCE LINES 10-24 .. code-block:: Python import numpy as np import anyplotlib as apl x = np.linspace(0, 4 * np.pi, 400) y = np.sin(x) * np.exp(-x / 12) fig, ax = apl.subplots(1, 1, figsize=(560, 340)) plot = ax.plot(y, axes=[x], color="#4fc3f7") point = plot.add_point_widget(x=3.0, y=0.5, color="#ff1744", show_crosshair=True) fig .. raw:: html
.. GENERATED FROM PYTHON SOURCE LINES 25-30 Snap the point onto the curve ----------------------------- A ``pointer_move`` handler fires while the point is dragged. Here we read ``point.x``, look up the nearest sample, and push the point back onto the curve with :meth:`~anyplotlib.Widget.set` — a one-line "snap to data" cursor. .. GENERATED FROM PYTHON SOURCE LINES 30-41 .. code-block:: Python @point.add_event_handler("pointer_move") def _snap(event): i = int(np.clip(np.searchsorted(x, point.x), 0, len(x) - 1)) point.set(y=float(y[i])) fig.set_help("Drag the point — it snaps onto the curve as you move it.") fig # Interactive .. raw:: html
.. raw:: html .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.438 seconds) .. _sphx_glr_download_auto_examples_Widgets_plot_widget1d_point.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_widget1d_point.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_widget1d_point.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_widget1d_point.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_