.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/plotting/interactive_xmap.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_examples_plotting_interactive_xmap.py: ============================ Interactive crystal map plot ============================ This example shows how to use :doc:`matplotlib event connections ` to add an interactive click function to a :class:`~orix.crystal_map.CrystalMap` plot. Here, we navigate an inverse pole figure (IPF) map and retreive the phase name and corresponding Euler angles from the location of the click. .. note:: This example uses the interactive capabilities of Matplotlib, and this will not appear in the static documentation. Please run this code on your machine to see the interactivity. You can copy and paste individual parts, or download the entire example using the link at the bottom of the page. .. GENERATED FROM PYTHON SOURCE LINES 39-118 .. image-sg:: /examples/plotting/images/sphx_glr_interactive_xmap_001.png :alt: Click position :srcset: /examples/plotting/images/sphx_glr_interactive_xmap_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Phase Orientations Name Space group Point group Proper point group Color 1 5657 (48.4%) austenite None 432 432 tab:blue 2 6043 (51.6%) ferrite None 432 432 tab:orange Properties: iq, dp Scan unit: um | .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from orix.crystal_map import CrystalMap from orix.data import sdss_ferrite_austenite from orix.plot import IPFColorKeyTSL xmap = sdss_ferrite_austenite(allow_download=True) print(xmap) pg_laue = xmap.phases[1].point_group.laue O_au = xmap["austenite"].orientations O_fe = xmap["ferrite"].orientations # Get IPF colors ipf_key = IPFColorKeyTSL(pg_laue) rgb_au = ipf_key.orientation2color(O_au) rgb_fe = ipf_key.orientation2color(O_fe) # Combine IPF color arrays rgb_all = np.zeros((xmap.size, 3)) phase_id_au = xmap.phases.id_from_name("austenite") phase_id_fe = xmap.phases.id_from_name("ferrite") rgb_all[xmap.phase_id == phase_id_au] = rgb_au rgb_all[xmap.phase_id == phase_id_fe] = rgb_fe def select_point(xmap: CrystalMap, rgb_all: np.ndarray) -> tuple[int, int]: """Return location of interactive user click on image. Interactive function for showing the phase name and Euler angles from the click-position. """ fig = xmap.plot( rgb_all, overlay="dp", return_figure=True, figure_kwargs={"figsize": (12, 8)}, ) ax = fig.axes[0] ax.set_title("Click position") # Extract array in the plot with IPF colors + dot product overlay rgb_dp_2d = ax.images[0].get_array() x = y = 0 def on_click(event): x, y = (event.xdata, event.ydata) if x is None: print("Please click inside the IPF map") return print(x, y) # Extract location in crystal map and extract phase name and # Euler angles xmap_yx = xmap[int(np.round(y)), int(np.round(x))] phase_name = xmap_yx.phases_in_data[:].name eu = xmap_yx.rotations.to_euler(degrees=True)[0].round(2) # Format Euler angles eu_str = "(" + ", ".join(np.array_str(eu)[1:-1].split()) + ")" plt.clf() plt.imshow(rgb_dp_2d) plt.plot(x, y, "+", c="k", markersize=15, markeredgewidth=3) plt.title( rf"Phase: {phase_name}, Euler angles: $(\phi_1, \Phi, \phi_2)$ = {eu_str}" ) plt.draw() fig.canvas.mpl_connect("button_press_event", on_click) return x, y x, y = select_point(xmap, rgb_all) plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.163 seconds) .. _sphx_glr_download_examples_plotting_interactive_xmap.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: interactive_xmap.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: interactive_xmap.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: interactive_xmap.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_