.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plotting/plot_edges_2d.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_edges_2d.py: .. _edges_2d_gallery: Edges in 2D ======================================== .. GENERATED FROM PYTHON SOURCE LINES 9-15 This tutorial show how to plot edges in 2D, using - :func:`oat_python.plot.trace_2d_for_edge`, which generates a trace for a single edge, and - :func:`oat_python.plot.trace_2d_for_edges`, which generates a single trace for multiple edges. .. GENERATED FROM PYTHON SOURCE LINES 18-20 Setup ------------------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 22-27 .. code-block:: Python import oat_python as oat import plotly import plotly.graph_objects as go import numpy as np .. GENERATED FROM PYTHON SOURCE LINES 28-30 We'll use a cycle graph on N vertices as an example. Here we generate N evenly spaced points on a circle, which will be the vertices of the graph. .. GENERATED FROM PYTHON SOURCE LINES 30-35 .. code-block:: Python N = 10 points = oat.point_cloud.circle( n_points=N, ) points .. rst-class:: sphx-glr-script-out .. code-block:: none array([[ 1.00000000e+00, 0.00000000e+00], [ 8.09016994e-01, 5.87785252e-01], [ 3.09016994e-01, 9.51056516e-01], [-3.09016994e-01, 9.51056516e-01], [-8.09016994e-01, 5.87785252e-01], [-1.00000000e+00, 1.22464680e-16], [-8.09016994e-01, -5.87785252e-01], [-3.09016994e-01, -9.51056516e-01], [ 3.09016994e-01, -9.51056516e-01], [ 8.09016994e-01, -5.87785252e-01]]) .. GENERATED FROM PYTHON SOURCE LINES 36-44 Color scales ------------------------------------------------------ Plotly has no native functionality to plot multiple edges on a colorscale in a single trace. If you'd like to plot multiple edges on a colorscale, you can create a separate trace for each edge, and assign a color to each edge individually. Here's an example. .. GENERATED FROM PYTHON SOURCE LINES 47-51 Get a Plotly colorscale object, which maps values in [0,1] to RGB colors. - For a full list of Plotly colorscales, see: https://plotly.com/python/builtin-colorscales/ - For another example of mapping scalar values to colors, see :ref:`color_mapping_gallery`. .. GENERATED FROM PYTHON SOURCE LINES 51-54 .. code-block:: Python colorscale = plotly.colors.get_colorscale('Rainbow') .. GENERATED FROM PYTHON SOURCE LINES 55-56 Convert N evenly spaced values in [0,1] to colors using the colorscale. We'll use these colors for the edges of a cycle graph. .. GENERATED FROM PYTHON SOURCE LINES 56-62 .. code-block:: Python colors = plotly.colors.sample_colorscale( colorscale, np.linspace(0, 1, N) ) colors .. rst-class:: sphx-glr-script-out .. code-block:: none ['rgb(150, 0, 90)', 'rgb(17, 0, 188)', 'rgb(0, 19, 243)', 'rgb(0, 110, 255)', 'rgb(24, 209, 197)', 'rgb(92, 255, 83)', 'rgb(186, 248, 0)', 'rgb(255, 207, 0)', 'rgb(255, 99, 0)', 'rgb(255, 0, 0)'] .. GENERATED FROM PYTHON SOURCE LINES 63-64 Plot the edges of the graph, using :func:`oat_python.plot.trace_2d_for_edge`. .. GENERATED FROM PYTHON SOURCE LINES 64-97 .. code-block:: Python # Make one trace per edge, each with a different color. For an explanation of # the keyword arguments, see the docstring for :func:`oat_python.plot.trace_2d_for_edge`. traces = [] for p in range(N): trace = oat.plot.trace_2d_for_edge( points = points, edge = [p, (p+1) % N], line = dict( color = colors[p], width = 3, ), name = f"Trace for edge {p}", ) traces.append(trace) # Create a figure with all traces fig = plotly.graph_objects.Figure(traces) # Set the title, and make the aspect ratio equal fig.update_layout( title="Using a colorscale by placing edges in separate traces", yaxis=dict( scaleanchor="x", # Anchor y-axis scaling to x-axis scaleratio=1.0 # Set the aspect ratio (1.0 for square aspect) ), ) # Show the figure fig .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 98-108 Grouping legend entries ------------------------------------------------------ Each trace appears separately in the legend by default. However, - You can group multiple traces together in the legend using keyword arguments. See the `Legend` section of :ref:`triangles_3d_gallery` for examples. - If you do not need to assign different colors to different edges, then you can plot multiple edges in a single trace using :func:`oat_python.plot.trace_2d_for_edges`. See the next section for an example. .. GENERATED FROM PYTHON SOURCE LINES 111-118 Grouping multiple edges in a single trace (may improve performance) ---------------------------------------------------------------------- If you do not need to assign different colors to different edges, then you can plot multiple edges in a single trace using :func:`oat_python.plot.trace_2d_for_edges`. Here's an example. .. GENERATED FROM PYTHON SOURCE LINES 120-122 Make a single trace for all edges, with the same color for all edges. For an explanation of the keyword arguments, see the docstring for :func:`oat_python.plot.trace_2d_for_edges`. .. GENERATED FROM PYTHON SOURCE LINES 122-148 .. code-block:: Python trace = oat.plot.trace_2d_for_edges( points = points, edges = [ [p, (p+1) % N] for p in range(N) ], line = dict( color = "crimson", width = 3, ), showlegend = True, name = f"Edges of a cycle graph", ) # Create a figure with the trace fig = plotly.graph_objects.Figure(trace) # Set the title, and make the aspect ratio equal fig.update_layout( title="Multiple edges in a single trace", yaxis=dict( scaleanchor="x", # Anchor y-axis scaling to x-axis scaleratio=1.0 # Set the aspect ratio (1.0 for square aspect) ), ) # Show the figure fig .. raw:: html


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