.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/Widgets/plot_widget2d_arrow.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_widget2d_arrow.py: 2D Arrow Widget =============== A draggable arrow overlay on a 2-D image panel. The tail sits at ``(x, y)`` and the head at ``(x + u, y + v)`` in data coordinates. Drag the body to move the whole arrow; drag the head handle to re-aim it (updating ``u`` / ``v``). Add it with :meth:`~anyplotlib.Plot2D.add_arrow_widget`. .. GENERATED FROM PYTHON SOURCE LINES 10-26 .. code-block:: Python import numpy as np import anyplotlib as apl rng = np.random.default_rng(3) data = rng.standard_normal((128, 128)).cumsum(0).cumsum(1) data = (data - data.min()) / (data.max() - data.min()) xy = np.linspace(0, 10, 128) fig, ax = apl.subplots(1, 1, figsize=(460, 460)) v = ax.imshow(data, axes=[xy, xy], units="nm") arrow = v.add_arrow_widget(x=2.0, y=2.0, u=5.0, v=4.0, color="#ff1744", linewidth=2) fig .. raw:: html
.. GENERATED FROM PYTHON SOURCE LINES 27-32 React to drags -------------- Register a ``pointer_move`` handler to read the live geometry while the arrow is dragged or re-aimed (there is no ``on_changed`` method — the event system is the API). Read ``arrow.x/y/u/v`` inside the handler. .. GENERATED FROM PYTHON SOURCE LINES 32-42 .. code-block:: Python @arrow.add_event_handler("pointer_move") def _report(event): print(f"tail=({arrow.x:.1f}, {arrow.y:.1f}) vector=({arrow.u:.1f}, {arrow.v:.1f})") fig.set_help("Drag the arrow body to move it; drag the head to re-aim it.") fig # Interactive .. raw:: html
.. raw:: html .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.461 seconds) .. _sphx_glr_download_auto_examples_Widgets_plot_widget2d_arrow.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_widget2d_arrow.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_widget2d_arrow.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_widget2d_arrow.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_