.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_gallery/events/line_data_thickness_event.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_events_line_data_thickness_event.py: Events line data thickness ========================== Simple example of adding event handlers for line data and thickness. .. GENERATED FROM PYTHON SOURCE LINES 7-79 .. image-sg:: /_gallery/events/images/sphx_glr_line_data_thickness_event_001.webp :alt: line data thickness event :srcset: /_gallery/events/images/sphx_glr_line_data_thickness_event_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 fastplotlib as fpl import numpy as np figure = fpl.Figure(size=(700, 560)) xs = np.linspace(0, 4 * np.pi, 100) # sine wave ys = np.sin(xs) sine = np.column_stack([xs, ys]) # cosine wave ys = np.cos(xs) cosine = np.column_stack([xs, ys]) # create line graphics sine_graphic = figure[0, 0].add_line(data=sine) cosine_graphic = figure[0, 0].add_line(data=cosine, offset=(0, 4, 0)) # make a list of the line graphics for convenience lines = [sine_graphic, cosine_graphic] def change_thickness(ev: fpl.GraphicFeatureEvent): # sets thickness of all the lines new_value = ev.info["value"] for g in lines: g.thickness = new_value def change_data(ev: fpl.GraphicFeatureEvent): # sets data of all the lines using the given event and value from the event # the user's slice/index # This can be a single int index, a slice, # or even a numpy array of int or bool for fancy indexing! indices = ev.info["key"] # the new values to set at the given indices new_values = ev.info["value"] # set the data for all the lines for g in lines: g.data[indices] = new_values # add the event handlers to the line graphics for g in lines: g.add_event_handler(change_thickness, "thickness") g.add_event_handler(change_data, "data") figure.show() figure[0, 0].axes.intersection = (0, 0, 0) # set the y-value of the middle 40 points of the sine graphic to 1 # after the sine_graphic sets its data, the event handlers will be called # and therefore the cosine graphic will also set its data using the event data sine_graphic.data[30:70, 1] = np.ones(40) # set the thickness of the cosine graphic, this will trigger an event # that causes the sine graphic's thickness to also be set from this value cosine_graphic.thickness = 10 # 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 0.365 seconds) .. _sphx_glr_download__gallery_events_line_data_thickness_event.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: line_data_thickness_event.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: line_data_thickness_event.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: line_data_thickness_event.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_