.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "sphinx_gallery_output/plot_20_geometric_layout.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_20_geometric_layout.py: Geometric node layout ===================== Infer node positions given the length of the edges between them. .. GENERATED FROM PYTHON SOURCE LINES 9-51 .. image-sg:: /sphinx_gallery_output/images/sphx_glr_plot_20_geometric_layout_001.png :alt: Original node positions, Reconstructed node positions :srcset: /sphinx_gallery_output/images/sphx_glr_plot_20_geometric_layout_001.png :class: sphx-glr-single-img .. code-block:: default import numpy as np import matplotlib.pyplot as plt import networkx as nx from netgraph import Graph # create a random geometric graph g = nx.random_geometric_graph(50, 0.3, seed=2) original_positions = nx.get_node_attributes(g, 'pos') fig, (ax1, ax2) = plt.subplots(1, 2) plot_instance = Graph(g, node_layout=original_positions, node_size=1, node_edge_width=0.1, edge_width=0.1, ax=ax1, ) ax1.axis([0, 1, 0, 1]) ax1.set_title('Original node positions') # compute edge lengths edge_length = dict() for (source, target) in g.edges: x1, y1 = original_positions[source] x2, y2 = original_positions[target] edge_length[(source, target)] = np.sqrt((x2 - x1)**2 + (y2 - y1)**2) # use non-linear optimisation to infer node positions given the edge lengths Graph(g, node_layout="geometric", node_layout_kwargs=dict(edge_length=edge_length, tol=1e-3), node_size=1, node_edge_width=0.1, edge_width=0.1, ax=ax2, ) ax2.axis([0, 1, 0, 1]) ax2.set_title('Reconstructed node positions') plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 5.051 seconds) .. _sphx_glr_download_sphinx_gallery_output_plot_20_geometric_layout.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_20_geometric_layout.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_20_geometric_layout.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_