.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_gallery/text/moving_label.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__gallery_text_moving_label.py: Moving TextGraphic label ======================== A TextGraphic that labels a point on a line and another TextGraphic that moves along the line on every draw. .. GENERATED FROM PYTHON SOURCE LINES 7-84 .. image-sg:: /_gallery/text/images/sphx_glr_moving_label_001.webp :alt: moving label :srcset: /_gallery/text/images/sphx_glr_moving_label_001.webp :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/work/fastplotlib/fastplotlib/fastplotlib/graphics/features/_base.py:18: UserWarning: casting float64 array to float32 warn(f"casting {array.dtype} array to float32") | .. code-block:: Python # test_example = false import numpy as np import fastplotlib as fpl # create a sinc wave xs = np.linspace(-2 * np.pi, 2 * np.pi, 200) ys = np.sinc(xs) data = np.column_stack([xs, ys]) # create a figure figure = fpl.Figure(size=(700, 450)) # sinc wave line = figure[0, 0].add_line(data, thickness=2) # position for the text label on the peak pos = (0, max(ys), 0) # create label for the peak text_peak = figure[0, 0].add_text( f"peak ", font_size=20, anchor="bottom-right", offset=pos ) # add a point on the peak point_peak = figure[0, 0].add_scatter(np.asarray([pos]), sizes=10, colors="r") # create a text that will move along the line text_moving = figure[0, 0].add_text( f"({xs[0]:.2f}, {ys[0]:.2f}) ", font_size=16, outline_color="k", outline_thickness=1, anchor="top-center", offset=(*data[0], 0) ) # a point that will move on the line point_moving = figure[0, 0].add_scatter(np.asarray([data[0]]), sizes=10, colors="magenta") index = 0 def update(): # moves the text and point before every draw global index # get the new position new_pos = (*data[index], 0) # move the text and point to the new position text_moving.offset = new_pos point_moving.data[0] = new_pos # set the text to the new position text_moving.text = f"({new_pos[0]:.2f}, {new_pos[1]:.2f})" # increment index index += 1 if index == data.shape[0]: index = 0 # add update as an animation functions figure.add_animations(update) figure[0, 0].axes.visible = False figure.show(maintain_aspect=False) # NOTE: `if __name__ == "__main__"` is NOT how to use fastplotlib interactively # please see our docs for using fastplotlib interactively in ipython and jupyter if __name__ == "__main__": print(__doc__) fpl.loop.run() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 12.665 seconds) .. _sphx_glr_download__gallery_text_moving_label.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: moving_label.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: moving_label.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: moving_label.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_