.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plotting/plot_style_3d.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_auto_examples_plotting_plot_style_3d.py: .. _styling_3d_gallery: Styling in 3D ================= This tutorial shows how to adjust the layout of 3D plots in Plotly. For more advanced usage, see the Plotly documentation: https://plotly.com/python/reference/layout/. .. GENERATED FROM PYTHON SOURCE LINES 13-23 .. code-block:: Python import oat_python as oat import networkx as nx import numpy as np import plotly import plotly.express as px import plotly.graph_objects as go from plotly.subplots import make_subplots .. GENERATED FROM PYTHON SOURCE LINES 24-30 Setup --------------------------------------- Let's generate a random simplicial complex to work with. We'll sample random points in a 30-dimensional ambient space, then construct a Vietoris-Rips complex. .. GENERATED FROM PYTHON SOURCE LINES 30-55 .. code-block:: Python # Sample points np.random.seed(0) # Set random seed for reproducibility n_points = 10 ambient_dimension = 30 points_array = np.random.rand(n_points, ambient_dimension) # Generate a Vietoris-Rips complex on these points epsilon = 2.2 dissimilarity_matrix = oat.dissimilarity.sparse_matrix_for_points( points_array, max_dissimilarity = 2.2, ) vietoris_rips_complex = oat.core.vietoris_rips.VietorisRipsComplex( dissimilarity_matrix, ) # List the simplices in the complex simplices = vietoris_rips_complex.simplices_for_dimensions([0,1,2]) simplices[30:35] .. raw:: html
simplex filtration
30 (2, 3, 6) 2.063640
31 (1, 3, 6) 2.077187
32 (4, 7, 8) 2.131222
33 (1, 2, 3) 2.143295
34 (1, 2, 6) 2.143295


.. GENERATED FROM PYTHON SOURCE LINES 56-57 Generate 3D coordinates for the vertices of the complex, based on its combinatorial structure .. GENERATED FROM PYTHON SOURCE LINES 57-66 .. code-block:: Python points = oat.plot.vertex_embedding_for_simplices( simplices.simplex, dimension = 3, iterations = 1000, seed = 11, method = "spring", ) .. GENERATED FROM PYTHON SOURCE LINES 67-68 Generate an initial plot, using :func:`oat_python.plot.fig_3d_for_simplices` .. GENERATED FROM PYTHON SOURCE LINES 68-75 .. code-block:: Python fig = oat.plot.fig_3d_for_simplices( simplices = simplices.simplex, points = points, ) fig .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 76-78 Canvas size --------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 80-81 Set a fixed width and height (in pixels) .. GENERATED FROM PYTHON SOURCE LINES 81-86 .. code-block:: Python fig.update_layout( width = 300, height = 300, ) .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 87-88 Reset the width to automatic sizing .. GENERATED FROM PYTHON SOURCE LINES 88-92 .. code-block:: Python fig.update_layout( width = None, ) .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 93-94 Increase the height .. GENERATED FROM PYTHON SOURCE LINES 94-98 .. code-block:: Python fig.update_layout( height = 600, ) .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 99-103 Aspect ratio --------------------------------------- Enforce a 1:1:1 aspect ratio (compare with the plot above): .. GENERATED FROM PYTHON SOURCE LINES 103-112 .. code-block:: Python fig.update_layout( scene = dict( aspectratio=go.layout.scene.Aspectratio(x=1, y=1, z=1), ) ) fig .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 113-117 Axis limits --------------------------------------- Set explicit axis limits. .. GENERATED FROM PYTHON SOURCE LINES 117-127 .. code-block:: Python fig.update_layout( scene = dict( xaxis = dict(range=[-1, 1],), # x axis limits yaxis = dict(range=[-1.5, 1],), # y axis limits zaxis = dict(range=[-1, 1],), # z axis limits ), ) fig .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 128-132 Grid lines --------------------------------------- Grid lines can be removed with ``oat_python.plot.blank_background``. .. GENERATED FROM PYTHON SOURCE LINES 132-135 .. code-block:: Python oat.plot.blank_background(fig) fig .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 136-140 Zoom --------------------------------------- The camera position can be initialized with greater/lesser zoom by adjusting the `eye` parameter. .. GENERATED FROM PYTHON SOURCE LINES 140-146 .. code-block:: Python fig.update_layout(scene_camera=dict( eye=dict(x=0.6, y=0.6, z=0.6) )) fig .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 147-151 Background color --------------------------------------- Use a solid white background. .. GENERATED FROM PYTHON SOURCE LINES 151-157 .. code-block:: Python oat.plot.set_background_color(fig, 'white') fig .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 158-171 Color --------------------------------------- Colors for vertices, edges, and triangles can be adjusted either by modifying the traces in ``fig.data``, or by passing keyword arguments to the relevant constructor functions. The traces for vertices, edges, and triangles are stored in ``fig.data[0]``, ``fig.data[1]``, and ``fig.data[2]``, respectively. These plots are generated, internally, using the functions - :func:`plotly.graph_objects.Scatter3d` (for vertices) - :func:`oat_python.plot.trace_3d_for_edges` (for edges) - :func:`oat_python.plot.trace_3d_for_triangles` (for triangles) Check out the documentation for these functions to see what parameters can be adjusted. .. GENERATED FROM PYTHON SOURCE LINES 174-187 .. _see_also_style_3d_color: See also ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - Color lists - `The list of built-in Plotly color sequences (high contrast) and color scales (smooth gradients) `_. - `The named CSS colors are listed here `_. - The :ref:`triangles_2d_gallery` gallery shows how to color triangles in 2D. - The :ref:`edges_2d_gallery` and :ref:`edges_3d_gallery` galleries show how to color edges in 2D and 3D. - The :ref:`triangles_3d_gallery` gallery shows how to color triangles in 3D based on scalar values assigned to vertices or faces. .. GENERATED FROM PYTHON SOURCE LINES 190-192 Choosing good triangle colors ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 194-196 You can use any color format that Plotly understands to update colors, including named colors ("white","red","brown"), hex codes (#RRGGBB), and RGB/RGBA values (rgb(255, 0, 0)). .. GENERATED FROM PYTHON SOURCE LINES 196-208 .. code-block:: Python # Change simplex color fig.data[2].update(color="red", opacity=0.7) # triangles fig.data[1].update(line=dict(color="black", width=4)) # edges fig.data[0].update(marker=dict(size=7, color="black")) # vertices # Re-set the background color oat.plot.set_background_color(fig, 'white') fig .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 209-210 Dark colors often fade away against a black background .. GENERATED FROM PYTHON SOURCE LINES 210-213 .. code-block:: Python oat.plot.set_background_color(fig, 'black') fig .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 214-215 White edges and vertices contrast better .. GENERATED FROM PYTHON SOURCE LINES 215-223 .. code-block:: Python fig.data[0].update(marker=dict(size=7, color="white")) # vertices fig.data[1].update(line=dict(color="white", width=6)) # edges oat.plot.set_background_color(fig, 'black') fig .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 224-227 Shades of grey ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 229-230 Shades of grey can give a clean look .. GENERATED FROM PYTHON SOURCE LINES 230-242 .. code-block:: Python # Color all simplices white fig.data[2].update(color="grey", opacity=1.0) # triangles fig.data[1].update(line=dict(color="white", width=8)) # edges fig.data[0].update(marker=dict(size=10, color='white')) # vertices # Re-set the background color oat.plot.set_background_color(fig, 'black') fig .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 243-244 With transparency, the effect is luminous .. GENERATED FROM PYTHON SOURCE LINES 244-255 .. code-block:: Python fig.data[2].update( color="white", facecolor=None, # we previously assigned an explicit value to facecolor, which takes precedence over color; so to use the color parameter, we need to reset facecolor to None opacity=0.4, ) oat.plot.set_background_color(fig, 'black') fig .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 256-262 Mosaic --------------------------------------- Plotly provides predefined color sequences that can be used to assign different colors to different simplices. These "discrete color sequences" are designed to create visual contrast between objects. See the :ref:`color_mapping_gallery` gallery for a complete list of color pallettes. .. GENERATED FROM PYTHON SOURCE LINES 265-266 Load a Plotly sequence of high-contrast pastel colors .. GENERATED FROM PYTHON SOURCE LINES 266-269 .. code-block:: Python pastel_sequence = px.colors.qualitative.Plotly pastel_sequence .. rst-class:: sphx-glr-script-out .. code-block:: none ['#636EFA', '#EF553B', '#00CC96', '#AB63FA', '#FFA15A', '#19D3F3', '#FF6692', '#B6E880', '#FF97FF', '#FECB52'] .. GENERATED FROM PYTHON SOURCE LINES 270-271 Shrink/expand this sequence to match the number of triangles triangles (wrapping around cyclicly, if necessary) .. GENERATED FROM PYTHON SOURCE LINES 271-274 .. code-block:: Python num_triangles = len( [s for s in simplices.simplex if len(s) == 3] ) # count triangles in the plot triangle_colors = [pastel_sequence[i % len(pastel_sequence)] for i in range(num_triangles)] # match the length of the sequence to the number of triangles .. GENERATED FROM PYTHON SOURCE LINES 275-276 Assign these colors to the triangles in the plot .. GENERATED FROM PYTHON SOURCE LINES 276-279 .. code-block:: Python fig.data[2].update( facecolor=triangle_colors ) fig .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 280-282 Smooth color gradients --------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 284-289 Color gradients can be used to convey additional information about simplices. Here we color triangles based on the distance of their vertices from the origin. - See the documentation for :func:`oat_python.plot.trace_3d_for_triangles` for usage instructions. - See the `Plotly documentation on colorscales `_ for a list of built-in colorscales. .. GENERATED FROM PYTHON SOURCE LINES 289-304 .. code-block:: Python triangles = [s for s in simplices.simplex if len(s) == 3] vertices = oat.simplex.vertices_incident_to_simplices(triangles) pastel_sequence = [np.linalg.norm(points[v]) for v in vertices] # distance of each vertex from the origin fig.data[2].update( intensity = pastel_sequence, # assign scalar color values to vertices colorscale = 'Jet', # choose a colorscale intensitymode = 'vertex', # specify that colors are assigned to vertices (not faces) showscale = False, # hide the colorbar ) fig .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 305-308 Color values for vertices can also be passed directly to the :func:`oat_python.plot.trace_3d_for_triangles` function. Here we color triangles based on their z-coordinate. Again, see the documentation for :func:`oat_python.plot.trace_3d_for_triangles` for usage instructions. .. GENERATED FROM PYTHON SOURCE LINES 308-322 .. code-block:: Python trace = oat.plot.trace_3d_for_triangles( triangles = triangles, points = points, opacity = 1.0, intensity = [ point[2] for point in points.values() ], # color triangles based on the z-coordinate of their vertices colorscale = 'Pinkyl', intensitymode = 'vertex', ) triangles_fig = go.Figure(trace) triangles_fig .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 323-326 Opacity --------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 328-329 Opaque simplices can make make 3d structure easier to perceive. .. GENERATED FROM PYTHON SOURCE LINES 329-341 .. code-block:: Python fig.data[2].update(opacity=1.0) # triangles fig .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 342-345 Multiple static views (alternative to video) ----------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 347-350 If you're unable to make a video of a rotating object, you can often convey the same depth information by showing multiple static views of the object from different angles. .. GENERATED FROM PYTHON SOURCE LINES 350-382 .. code-block:: Python # Create a new figure with 3 3D subplots subfig = make_subplots( rows=3, cols=1, specs=[[{'type': 'scene'}], [{'type': 'scene'}], [{'type': 'scene'}]], subplot_titles=["View 1", "View 2", "View 3"], vertical_spacing=0.05 # Adjust this value to control spacing ) # Add the same traces to each subplot for trace in fig.data: subfig.add_trace(trace, row=1, col=1) subfig.add_trace(trace, row=2, col=1) subfig.add_trace(trace, row=3, col=1) # Set different camera angles for each subplot subfig.update_layout( scene=dict(camera=dict(eye=dict(x=0.8, y=0.8, z=0.8))), scene2=dict(camera=dict(eye=dict(x=0., y=0., z=1.6))), scene3=dict(camera=dict(eye=dict(x=-0.8, y=-0.8, z=0.8))), height=400, width=1200 ) subfig.update_layout( showlegend = False, height = 1400, width = None, template = "plotly_dark", ) subfig .. raw:: html


.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.772 seconds) .. _sphx_glr_download_auto_examples_plotting_plot_style_3d.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_style_3d.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_style_3d.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_style_3d.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_