.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_gallery/guis/imgui_legend_iris.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_guis_imgui_legend_iris.py: ImGUI Legend with Markers ========================= The iris dataset, with the species of each sample shown by color and the k-means cluster it was assigned to shown by marker shape. A feature that is not the same for every datapoint needs a label for each of its values, so the legend has an entry per species and an entry per cluster. .. GENERATED FROM PYTHON SOURCE LINES 9-67 .. image-sg:: /_gallery/guis/images/sphx_glr_imgui_legend_iris_001.webp :alt: imgui legend iris :srcset: /_gallery/guis/images/sphx_glr_imgui_legend_iris_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:19: UserWarning: casting float64 array to float32 warn(f"casting {array.dtype} array to float32") | .. code-block:: Python # test_example = true import numpy as np import fastplotlib as fpl from fastplotlib.ui import Legend from sklearn.cluster import KMeans from sklearn import datasets figure = fpl.Figure(size=(700, 560)) iris = datasets.load_iris() # one marker shape per predicted cluster kmeans = KMeans(n_clusters=3, n_init=10, random_state=0).fit(iris["data"]) markers = np.asarray(["circle", "square", "diamond"])[kmeans.labels_] scatter = figure[0, 0].add_scatter( data=iris["data"][:, :2], # sepal length and width sizes=12, cmap="tab10", cmap_transform=iris["target"], # species markers=markers, alpha=0.5, ) # qualitative colormap, span its full range so that species k always gets color k scatter.cmap_range = (0, scatter.cmap.num_colors) # the species entries are colored by the colormap, the cluster entries show the marker shapes legend = Legend( [ scatter.create_legend_item( label="iris", cmap_transform_labels={ i: str(name) for i, name in enumerate(iris["target_names"]) }, markers_labels={ "circle": "cluster 0", "square": "cluster 1", "diamond": "cluster 2", }, ) ] ) # a floating legend is drawn over the plot and can be dragged around, it reserves no canvas space figure.add_imgui_window( legend, location="floating", rect=(0.8, 0.1, 0, 0), title="legend" ) figure.show(maintain_aspect=False) # NOTE: fpl.loop.run() should not be used for interactive sessions # See the "JupyterLab and IPython" section in the user guide if __name__ == "__main__": print(__doc__) fpl.loop.run() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.130 seconds) .. _sphx_glr_download__gallery_guis_imgui_legend_iris.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: imgui_legend_iris.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: imgui_legend_iris.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_