Skip to content

back port: generic NetworkX '<name>_layout' layouts - #43030

Merged
vbraun merged 2 commits into
sagemath:developfrom
dcoudert:graphs/netwokx_layouts
Oct 28, 2026
Merged

vbraun merged 2 commits into
sagemath:developfrom
dcoudert:graphs/netwokx_layouts

Conversation

@dcoudert

Copy link
Copy Markdown
Collaborator

This is a back port of passagemath/passagemath#2693.
It provides access to layout methods of NetworkX.

For instance

sage: import networkx as nx
sage: G = graphs.PetersenGraph()
sage: pos = nx.kamada_kawai_layout(G.networkx_graph())
G.plot(pos=pos)

can now be done as

sage: G = graphs.PetersenGraph()
sage: G.plot(layout='kamada_kawai')

This PR adds method layout_networkx that takes as an input the name of a networkx layout and calls the correct method.
The layouts can also be accessed via methods plot and layout.

sage: G.plot(layout='kamada_kawai')
sage: G.layout('kamada_kawai')
{0: (0.5451030292151609, -0.6871287549350156),
 1: (0.9697074521819642, 0.2991504179239621),
 2: (0.44373004519448467, 0.926367292534036),
 3: (-0.5585232389843457, 0.8620312293856005),
 4: (-0.49740545350471793, -0.19968189731452146),
 5: (-0.45277609856742673, -0.7511824689728754),
 6: (-0.018824402522943536, 0.29314732071879973),
 7: (0.5188539214937454, -0.1344405019393949),
 8: (-1.0, 0.1727126671526057),
 9: (0.05013474549407914, -0.7809753045531966)}

📝 Checklist

  • The title is concise and informative.
  • The description explains in detail what this PR is about.
  • I have linked a relevant issue or discussion.
  • I have created tests covering the changes.
  • I have updated the documentation and checked the documentation preview.

⌛ Dependencies

@dcoudert dcoudert self-assigned this Oct 20, 2026
dcoudert added a commit to dcoudert/sage that referenced this pull request Oct 20, 2026
@github-actions

github-actions Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Documentation preview for this PR (built with commit 4739967; changes) is ready! 🎉
This preview will update shortly after each push to this PR.

@dimpase
dimpase force-pushed the graphs/netwokx_layouts branch from 03106e5 to 4739967 Compare October 23, 2026 00:06
vbraun pushed a commit to vbraun/sage that referenced this pull request Oct 27, 2026
Release Manager
sagemathgh-43030: back port: generic NetworkX '<name>_layout' layouts
    
This is a back port of
passagemath/passagemath#2693.
It provides access to layout methods of NetworkX.

For instance
```sage
sage: import networkx as nx
sage: G = graphs.PetersenGraph()
sage: pos = nx.kamada_kawai_layout(G.networkx_graph())
G.plot(pos=pos)
```
can now be done as
```sage
sage: G = graphs.PetersenGraph()
sage: G.plot(layout='kamada_kawai')
```

This PR adds method `layout_networkx` that takes as an input the name of
a networkx layout and calls the correct method.
The layouts can also be accessed via methods `plot` and `layout`.

```sage
sage: G.plot(layout='kamada_kawai')
sage: G.layout('kamada_kawai')
{0: (0.5451030292151609, -0.6871287549350156),
 1: (0.9697074521819642, 0.2991504179239621),
 2: (0.44373004519448467, 0.926367292534036),
 3: (-0.5585232389843457, 0.8620312293856005),
 4: (-0.49740545350471793, -0.19968189731452146),
 5: (-0.45277609856742673, -0.7511824689728754),
 6: (-0.018824402522943536, 0.29314732071879973),
 7: (0.5188539214937454, -0.1344405019393949),
 8: (-1.0, 0.1727126671526057),
 9: (0.05013474549407914, -0.7809753045531966)}
```

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [x] I have created tests covering the changes.
- [x] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->
    
URL: sagemath#43030
Reported by: David Coudert
Reviewer(s):
@vbraun
vbraun merged commit c8ef425 into sagemath:develop Oct 28, 2026
21 of 23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants