.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "sphinx_gallery_output/plot_18_legends.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_sphinx_gallery_output_plot_18_legends.py: Node and Edge Legends ===================== Legends for node or edge properties can be created through the use of matplotlib proxy artists. For a comprehensive guide, see the `matplotlib legend guide`_. .. _matplotlib legend guide: https://matplotlib.org/stable/tutorials/intermediate/legend_guide.html#proxy-legend-handles .. GENERATED FROM PYTHON SOURCE LINES 12-102 .. image-sg:: /sphinx_gallery_output/images/sphx_glr_plot_18_legends_001.png :alt: plot 18 legends :srcset: /sphinx_gallery_output/images/sphx_glr_plot_18_legends_001.png :class: sphx-glr-single-img .. code-block:: default import matplotlib.pyplot as plt from netgraph import Graph triangle = [(0, 1), (1, 2), (2, 0)] node_positions = { 0 : (0.2, 0.2), 1 : (0.5, 0.8), 2 : (0.8, 0.2), } node_color = { 0 : 'tab:blue', 1 : 'tab:orange', 2 : 'tab:green', } node_shape = { 0 : 's', 1 : '8', 2 : 'o', } node_size = { 0 : 5, 1 : 10, 2 : 15, } edge_width = { (0, 1) : 1, (1, 2) : 2, (2, 0) : 3, } edge_color = { (0, 1) : 'tab:red', (1, 2) : 'tab:purple', (2, 0) : 'tab:brown' } fig, ax = plt.subplots() g = Graph( triangle, node_layout=node_positions, node_labels=True, edge_labels=True, node_size=node_size, node_color=node_color, node_edge_color=node_color, node_shape=node_shape, edge_width=edge_width, edge_color=edge_color, ax=ax ) # Create proxy artists for legend handles. node_proxy_artists = [] for node in [0, 1, 2]: proxy = plt.Line2D( [], [], linestyle='None', color=node_color[node], marker=node_shape[node], markersize=node_size[node], label=node ) node_proxy_artists.append(proxy) node_legend = ax.legend(handles=node_proxy_artists, loc='upper left', title='Nodes') ax.add_artist(node_legend) edge_proxy_artists = [] for edge in triangle: proxy = plt.Line2D( [], [], linestyle='-', color=edge_color[edge], linewidth=edge_width[edge], label=edge ) edge_proxy_artists.append(proxy) edge_legend = ax.legend(handles=edge_proxy_artists, loc='upper right', title='Edges') ax.add_artist(edge_legend) plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.519 seconds) .. _sphx_glr_download_sphinx_gallery_output_plot_18_legends.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_18_legends.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_18_legends.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_