.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/plot_slicer_demo.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_plot_slicer_demo.py: Slicer demo =========== The example demonstrates the `plot_3d_slicer` - contributed by `@prisae `_ Using the inversion result from the example notebook `plot_laguna_del_maule_inversion.ipynb `_ You have to use :code:`%matplotlib notebook` in Jupyter Notebook, and :code:`%matplotlib widget` in Jupyter Lab (latter requires the package ``ipympl``). .. GENERATED FROM PYTHON SOURCE LINES 16-25 .. code-block:: Python # %matplotlib notebook # %matplotlib widget import discretize import numpy as np import matplotlib.pyplot as plt from matplotlib.colors import SymLogNorm import pooch .. GENERATED FROM PYTHON SOURCE LINES 26-31 Download and load data ---------------------- In the following we load the :code:`mesh` and :code:`Lpout` that you would get from running the laguna-del-maule inversion notebook. .. GENERATED FROM PYTHON SOURCE LINES 31-45 .. code-block:: Python model_url = "https://storage.googleapis.com/simpeg/laguna_del_maule_slicer.tar.gz" downloaded_items = pooch.retrieve( model_url, known_hash="107293bfdeb77b314f4cb451a24c2c93a55aae40da28f43cf3c075d71acfb957", processor=pooch.Untar(), ) mesh_path = next(filter(lambda f: f.endswith("mesh.json"), downloaded_items)) model_path = next(filter(lambda f: f.endswith("Lpout.npy"), downloaded_items)) # Load the mesh and model mesh = discretize.load_mesh(mesh_path) Lpout = np.load(model_path) .. GENERATED FROM PYTHON SOURCE LINES 46-51 Case 1: Using the intrinsinc functionality ------------------------------------------ 1.1 Default options ^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 51-55 .. code-block:: Python mesh.plot_3d_slicer(Lpout) plt.show() .. image-sg:: /examples/images/sphx_glr_plot_slicer_demo_001.png :alt: plot slicer demo :srcset: /examples/images/sphx_glr_plot_slicer_demo_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 56-61 1.2 Create a function to improve plots, labeling after creation ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Depending on your data the default option might look a bit odd. The look of the figure can be improved by getting its handle and adjust it. .. GENERATED FROM PYTHON SOURCE LINES 61-104 .. code-block:: Python def beautify(title, fig=None): """Beautify the 3D Slicer result.""" # Get figure handle if not provided if fig is None: fig = plt.gcf() # Get principal figure axes axs = fig.get_children() # Set figure title fig.suptitle(title, y=0.95, va="center") # Adjust the y-labels on the first subplot (XY) plt.setp(axs[1].yaxis.get_majorticklabels(), rotation=90) for label in axs[1].yaxis.get_ticklabels(): label.set_visible(False) for label in axs[1].yaxis.get_ticklabels()[::3]: label.set_visible(True) axs[1].set_ylabel("Northing (m)") # Adjust x- and y-labels on the second subplot (XZ) axs[2].set_xticks([357500, 362500, 367500]) axs[2].set_xlabel("Easting (m)") plt.setp(axs[2].yaxis.get_majorticklabels(), rotation=90) axs[2].set_yticks([2500, 0, -2500, -5000]) axs[2].set_yticklabels(["$2.5$", "0.0", "-2.5", "-5.0"]) axs[2].set_ylabel("Elevation (km)") # Adjust x-labels on the third subplot (ZY) axs[3].set_xticks([2500, 0, -2500, -5000]) axs[3].set_xticklabels(["", "0.0", "-2.5", "-5.0"]) # Adjust colorbar axs[4].set_ylabel("Density (g/cc$^3$)") # Ensure sufficient margins so nothing is clipped plt.subplots_adjust(bottom=0.1, top=0.9, left=0.1, right=0.9) .. GENERATED FROM PYTHON SOURCE LINES 106-110 .. code-block:: Python mesh.plot_3d_slicer(Lpout) beautify("mesh.plot_3d_slicer(Lpout)") plt.show() .. image-sg:: /examples/images/sphx_glr_plot_slicer_demo_002.png :alt: mesh.plot_3d_slicer(Lpout) :srcset: /examples/images/sphx_glr_plot_slicer_demo_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 111-117 1.3 Set `xslice`, `yslice`, and `zslice`; transparent region ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The 2nd-4th input arguments are the initial x-, y-, and z-slice location (they default to the middle of the volume). The transparency-parameter can be used to define transparent regions. .. GENERATED FROM PYTHON SOURCE LINES 117-125 .. code-block:: Python mesh.plot_3d_slicer(Lpout, 370000, 6002500, -2500, transparent=[[-0.02, 0.1]]) beautify( "mesh.plot_3d_slicer(" "\nLpout, 370000, 6002500, -2500, transparent=[[-0.02, 0.1]])" ) plt.show() .. image-sg:: /examples/images/sphx_glr_plot_slicer_demo_003.png :alt: mesh.plot_3d_slicer( Lpout, 370000, 6002500, -2500, transparent=[[-0.02, 0.1]]) :srcset: /examples/images/sphx_glr_plot_slicer_demo_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 126-128 1.4 Set `clim`, use `pcolor_opts` to show grid lines ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 128-138 .. code-block:: Python mesh.plot_3d_slicer( Lpout, clim=[-0.4, 0.2], pcolor_opts={"edgecolor": "k", "linewidth": 0.1} ) beautify( "mesh.plot_3d_slicer(\nLpout, clim=[-0.4, 0.2], " "pcolor_opts={'edgecolor': 'k', 'linewidth': 0.1})" ) plt.show() .. image-sg:: /examples/images/sphx_glr_plot_slicer_demo_004.png :alt: mesh.plot_3d_slicer( Lpout, clim=[-0.4, 0.2], pcolor_opts={'edgecolor': 'k', 'linewidth': 0.1}) :srcset: /examples/images/sphx_glr_plot_slicer_demo_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 139-141 1.5 Use `pcolor_opts` to set `SymLogNorm`, and another `cmap` ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 141-151 .. code-block:: Python mesh.plot_3d_slicer( Lpout, pcolor_opts={"norm": SymLogNorm(linthresh=0.01), "cmap": "RdBu_r"} ) beautify( "mesh.plot_3d_slicer(Lpout," "\npcolor_opts={'norm': SymLogNorm(linthresh=0.01),'cmap': 'RdBu_r'})`" ) plt.show() .. image-sg:: /examples/images/sphx_glr_plot_slicer_demo_005.png :alt: mesh.plot_3d_slicer(Lpout, pcolor_opts={'norm': SymLogNorm(linthresh=0.01),'cmap': 'RdBu_r'})` :srcset: /examples/images/sphx_glr_plot_slicer_demo_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 152-164 1.6 Use :code:`aspect` and :code:`grid` ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ By default, :code:`aspect='auto'` and :code:`grid=[2, 2, 1]`. This means that the figure is on a 3x3 grid, where the `xy`-slice occupies 2x2 cells of the subplot-grid, `xz`-slice 2x1, and the `zy`-silce 1x2. So the :code:`grid=[x, y, z]`-parameter takes the number of cells for `x`, `y`, and `z`-dimension. :code:`grid` can be used to improve the probable weired subplot-arrangement if :code:`aspect` is anything else than :code:`auto`. However, if you zoom then it won't help. Expect the unexpected. .. GENERATED FROM PYTHON SOURCE LINES 164-169 .. code-block:: Python mesh.plot_3d_slicer(Lpout, aspect=["equal", 1.5], grid=[4, 4, 3]) beautify("mesh.plot_3d_slicer(Lpout, aspect=['equal', 1.5], grid=[4, 4, 3])") plt.show() .. image-sg:: /examples/images/sphx_glr_plot_slicer_demo_006.png :alt: mesh.plot_3d_slicer(Lpout, aspect=['equal', 1.5], grid=[4, 4, 3]) :srcset: /examples/images/sphx_glr_plot_slicer_demo_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 170-175 1.7 Transparency-slider ^^^^^^^^^^^^^^^^^^^^^^^ Setting the transparent-parameter to 'slider' will create interactive sliders to change which range of values of the data is visible. .. GENERATED FROM PYTHON SOURCE LINES 175-181 .. code-block:: Python mesh.plot_3d_slicer(Lpout, transparent="slider") beautify("mesh.plot_3d_slicer(Lpout, transparent='slider')") plt.show() .. image-sg:: /examples/images/sphx_glr_plot_slicer_demo_007.png :alt: mesh.plot_3d_slicer(Lpout, transparent='slider') :srcset: /examples/images/sphx_glr_plot_slicer_demo_007.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 182-186 Case 2: Just using the Slicer class ------------------------------------------ This way you get the figure-handle, and can do further stuff with the figure. .. GENERATED FROM PYTHON SOURCE LINES 186-200 .. code-block:: Python # You have to initialize a figure fig = plt.figure() # Then you have to get the tracker from the Slicer tracker = discretize.mixins.Slicer(mesh, Lpout) # Finally you have to connect the tracker to the figure fig.canvas.mpl_connect("scroll_event", tracker.onscroll) # Run it through beautify beautify("'discretize.mixins.Slicer' together with\n'fig.canvas.mpl_connect'", fig) plt.show() .. image-sg:: /examples/images/sphx_glr_plot_slicer_demo_008.png :alt: 'discretize.mixins.Slicer' together with 'fig.canvas.mpl_connect' :srcset: /examples/images/sphx_glr_plot_slicer_demo_008.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.507 seconds) .. _sphx_glr_download_examples_plot_slicer_demo.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_slicer_demo.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_slicer_demo.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_slicer_demo.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_