File size: 66,075 Bytes
9c6594c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 |
"""
******
Layout
******
Node positioning algorithms for graph drawing.
For `random_layout()` the possible resulting shape
is a square of side [0, scale] (default: [0, 1])
Changing `center` shifts the layout by that amount.
For the other layout routines, the extent is
[center - scale, center + scale] (default: [-1, 1]).
Warning: Most layout routines have only been tested in 2-dimensions.
"""
import networkx as nx
from networkx.utils import np_random_state
__all__ = [
"bipartite_layout",
"circular_layout",
"forceatlas2_layout",
"kamada_kawai_layout",
"random_layout",
"rescale_layout",
"rescale_layout_dict",
"shell_layout",
"spring_layout",
"spectral_layout",
"planar_layout",
"fruchterman_reingold_layout",
"spiral_layout",
"multipartite_layout",
"bfs_layout",
"arf_layout",
]
def _process_params(G, center, dim):
# Some boilerplate code.
import numpy as np
if not isinstance(G, nx.Graph):
empty_graph = nx.Graph()
empty_graph.add_nodes_from(G)
G = empty_graph
if center is None:
center = np.zeros(dim)
else:
center = np.asarray(center)
if len(center) != dim:
msg = "length of center coordinates must match dimension of layout"
raise ValueError(msg)
return G, center
@np_random_state(3)
def random_layout(G, center=None, dim=2, seed=None, store_pos_as=None):
"""Position nodes uniformly at random in the unit square.
For every node, a position is generated by choosing each of dim
coordinates uniformly at random on the interval [0.0, 1.0).
NumPy (http://scipy.org) is required for this function.
Parameters
----------
G : NetworkX graph or list of nodes
A position will be assigned to every node in G.
center : array-like or None
Coordinate pair around which to center the layout.
dim : int
Dimension of layout.
seed : int, RandomState instance or None optional (default=None)
Set the random state for deterministic node layouts.
If int, `seed` is the seed used by the random number generator,
if numpy.random.RandomState instance, `seed` is the random
number generator,
if None, the random number generator is the RandomState instance used
by numpy.random.
store_pos_as : str, default None
If non-None, the position of each node will be stored on the graph as
an attribute with this string as its name, which can be accessed with
``G.nodes[...][store_pos_as]``. The function still returns the dictionary.
Returns
-------
pos : dict
A dictionary of positions keyed by node
Examples
--------
>>> from pprint import pprint
>>> G = nx.lollipop_graph(4, 3)
>>> pos = nx.random_layout(G)
>>> # suppress the returned dict and store on the graph directly
>>> _ = nx.random_layout(G, seed=42, store_pos_as="pos")
>>> pprint(nx.get_node_attributes(G, "pos"))
{0: array([0.37454012, 0.9507143 ], dtype=float32),
1: array([0.7319939, 0.5986585], dtype=float32),
2: array([0.15601864, 0.15599452], dtype=float32),
3: array([0.05808361, 0.8661761 ], dtype=float32),
4: array([0.601115 , 0.7080726], dtype=float32),
5: array([0.02058449, 0.96990985], dtype=float32),
6: array([0.83244264, 0.21233912], dtype=float32)}
"""
import numpy as np
G, center = _process_params(G, center, dim)
pos = seed.rand(len(G), dim) + center
pos = pos.astype(np.float32)
pos = dict(zip(G, pos))
if store_pos_as is not None:
nx.set_node_attributes(G, pos, store_pos_as)
return pos
def circular_layout(G, scale=1, center=None, dim=2, store_pos_as=None):
# dim=2 only
"""Position nodes on a circle.
Parameters
----------
G : NetworkX graph or list of nodes
A position will be assigned to every node in G.
scale : number (default: 1)
Scale factor for positions.
center : array-like or None
Coordinate pair around which to center the layout.
dim : int
Dimension of layout.
If dim>2, the remaining dimensions are set to zero
in the returned positions.
If dim<2, a ValueError is raised.
store_pos_as : str, default None
If non-None, the position of each node will be stored on the graph as
an attribute with this string as its name, which can be accessed with
``G.nodes[...][store_pos_as]``. The function still returns the dictionary.
Returns
-------
pos : dict
A dictionary of positions keyed by node
Raises
------
ValueError
If dim < 2
Examples
--------
>>> from pprint import pprint
>>> G = nx.path_graph(4)
>>> pos = nx.circular_layout(G)
>>> # suppress the returned dict and store on the graph directly
>>> _ = nx.circular_layout(G, store_pos_as="pos")
>>> pprint(nx.get_node_attributes(G, "pos"))
{0: array([9.99999986e-01, 2.18556937e-08]),
1: array([-3.57647606e-08, 1.00000000e+00]),
2: array([-9.9999997e-01, -6.5567081e-08]),
3: array([ 1.98715071e-08, -9.99999956e-01])}
Notes
-----
This algorithm currently only works in two dimensions and does not
try to minimize edge crossings.
"""
import numpy as np
if dim < 2:
raise ValueError("cannot handle dimensions < 2")
G, center = _process_params(G, center, dim)
paddims = max(0, (dim - 2))
if len(G) == 0:
pos = {}
elif len(G) == 1:
pos = {nx.utils.arbitrary_element(G): center}
else:
# Discard the extra angle since it matches 0 radians.
theta = np.linspace(0, 1, len(G) + 1)[:-1] * 2 * np.pi
theta = theta.astype(np.float32)
pos = np.column_stack(
[np.cos(theta), np.sin(theta), np.zeros((len(G), paddims))]
)
pos = rescale_layout(pos, scale=scale) + center
pos = dict(zip(G, pos))
if store_pos_as is not None:
nx.set_node_attributes(G, pos, store_pos_as)
return pos
def shell_layout(
G, nlist=None, rotate=None, scale=1, center=None, dim=2, store_pos_as=None
):
"""Position nodes in concentric circles.
Parameters
----------
G : NetworkX graph or list of nodes
A position will be assigned to every node in G.
nlist : list of lists
List of node lists for each shell.
rotate : angle in radians (default=pi/len(nlist))
Angle by which to rotate the starting position of each shell
relative to the starting position of the previous shell.
To recreate behavior before v2.5 use rotate=0.
scale : number (default: 1)
Scale factor for positions.
center : array-like or None
Coordinate pair around which to center the layout.
dim : int
Dimension of layout, currently only dim=2 is supported.
Other dimension values result in a ValueError.
store_pos_as : str, default None
If non-None, the position of each node will be stored on the graph as
an attribute with this string as its name, which can be accessed with
``G.nodes[...][store_pos_as]``. The function still returns the dictionary.
Returns
-------
pos : dict
A dictionary of positions keyed by node
Raises
------
ValueError
If dim != 2
Examples
--------
>>> from pprint import pprint
>>> G = nx.path_graph(4)
>>> shells = [[0], [1, 2, 3]]
>>> pos = nx.shell_layout(G, shells)
>>> # suppress the returned dict and store on the graph directly
>>> _ = nx.shell_layout(G, shells, store_pos_as="pos")
>>> pprint(nx.get_node_attributes(G, "pos"))
{0: array([0., 0.]),
1: array([-5.00000000e-01, -4.37113883e-08]),
2: array([ 0.24999996, -0.43301272]),
3: array([0.24999981, 0.43301281])}
Notes
-----
This algorithm currently only works in two dimensions and does not
try to minimize edge crossings.
"""
import numpy as np
if dim != 2:
raise ValueError("can only handle 2 dimensions")
G, center = _process_params(G, center, dim)
if len(G) == 0:
return {}
if len(G) == 1:
return {nx.utils.arbitrary_element(G): center}
if nlist is None:
# draw the whole graph in one shell
nlist = [list(G)]
radius_bump = scale / len(nlist)
if len(nlist[0]) == 1:
# single node at center
radius = 0.0
else:
# else start at r=1
radius = radius_bump
if rotate is None:
rotate = np.pi / len(nlist)
first_theta = rotate
npos = {}
for nodes in nlist:
# Discard the last angle (endpoint=False) since 2*pi matches 0 radians
theta = (
np.linspace(0, 2 * np.pi, len(nodes), endpoint=False, dtype=np.float32)
+ first_theta
)
pos = radius * np.column_stack([np.cos(theta), np.sin(theta)]) + center
npos.update(zip(nodes, pos))
radius += radius_bump
first_theta += rotate
if store_pos_as is not None:
nx.set_node_attributes(G, npos, store_pos_as)
return npos
def bipartite_layout(
G,
nodes=None,
align="vertical",
scale=1,
center=None,
aspect_ratio=4 / 3,
store_pos_as=None,
):
"""Position nodes in two straight lines.
Parameters
----------
G : NetworkX graph or list of nodes
A position will be assigned to every node in G.
nodes : collection of nodes
Nodes in one node set of the graph. This set will be placed on
left or top. If `None` (the default), a node set is chosen arbitrarily
if the graph if bipartite.
align : string (default='vertical')
The alignment of nodes. Vertical or horizontal.
scale : number (default: 1)
Scale factor for positions.
center : array-like or None
Coordinate pair around which to center the layout.
aspect_ratio : number (default=4/3):
The ratio of the width to the height of the layout.
store_pos_as : str, default None
If non-None, the position of each node will be stored on the graph as
an attribute with this string as its name, which can be accessed with
``G.nodes[...][store_pos_as]``. The function still returns the dictionary.
Returns
-------
pos : dict
A dictionary of positions keyed by node.
Raises
------
NetworkXError
If ``nodes=None`` and `G` is not bipartite.
Examples
--------
>>> G = nx.complete_bipartite_graph(3, 3)
>>> pos = nx.bipartite_layout(G)
The ordering of the layout (i.e. which nodes appear on the left/top) can
be specified with the `nodes` parameter:
>>> top, bottom = nx.bipartite.sets(G)
>>> pos = nx.bipartite_layout(G, nodes=bottom) # "bottom" set appears on the left
`store_pos_as` can be used to store the node positions for the computed layout
directly on the nodes:
>>> _ = nx.bipartite_layout(G, nodes=bottom, store_pos_as="pos")
>>> from pprint import pprint
>>> pprint(nx.get_node_attributes(G, "pos"))
{0: array([ 1. , -0.75]),
1: array([1., 0.]),
2: array([1. , 0.75]),
3: array([-1. , -0.75]),
4: array([-1., 0.]),
5: array([-1. , 0.75])}
The ``bipartite_layout`` function can be used with non-bipartite graphs
by explicitly specifying how the layout should be partitioned with `nodes`:
>>> G = nx.complete_graph(5) # Non-bipartite
>>> pos = nx.bipartite_layout(G, nodes={0, 1, 2})
Notes
-----
This algorithm currently only works in two dimensions and does not
try to minimize edge crossings.
"""
import numpy as np
if align not in ("vertical", "horizontal"):
msg = "align must be either vertical or horizontal."
raise ValueError(msg)
G, center = _process_params(G, center=center, dim=2)
if len(G) == 0:
return {}
height = 1
width = aspect_ratio * height
offset = (width / 2, height / 2)
if nodes is None:
top, bottom = nx.bipartite.sets(G)
nodes = list(G)
else:
top = set(nodes)
bottom = set(G) - top
# Preserves backward-compatible node ordering in returned pos dict
nodes = list(top) + list(bottom)
left_xs = np.repeat(0, len(top))
right_xs = np.repeat(width, len(bottom))
left_ys = np.linspace(0, height, len(top))
right_ys = np.linspace(0, height, len(bottom))
top_pos = np.column_stack([left_xs, left_ys]) - offset
bottom_pos = np.column_stack([right_xs, right_ys]) - offset
pos = np.concatenate([top_pos, bottom_pos])
pos = rescale_layout(pos, scale=scale) + center
if align == "horizontal":
pos = pos[:, ::-1] # swap x and y coords
pos = dict(zip(nodes, pos))
if store_pos_as is not None:
nx.set_node_attributes(G, pos, store_pos_as)
return pos
@np_random_state(10)
def spring_layout(
G,
k=None,
pos=None,
fixed=None,
iterations=50,
threshold=1e-4,
weight="weight",
scale=1,
center=None,
dim=2,
seed=None,
store_pos_as=None,
*,
method="auto",
gravity=1.0,
):
"""Position nodes using Fruchterman-Reingold force-directed algorithm.
The algorithm simulates a force-directed representation of the network
treating edges as springs holding nodes close, while treating nodes
as repelling objects, sometimes called an anti-gravity force.
Simulation continues until the positions are close to an equilibrium.
There are some hard-coded values: minimal distance between
nodes (0.01) and "temperature" of 0.1 to ensure nodes don't fly away.
During the simulation, `k` helps determine the distance between nodes,
though `scale` and `center` determine the size and place after
rescaling occurs at the end of the simulation.
Fixing some nodes doesn't allow them to move in the simulation.
It also turns off the rescaling feature at the simulation's end.
In addition, setting `scale` to `None` turns off rescaling.
Parameters
----------
G : NetworkX graph or list of nodes
A position will be assigned to every node in G.
k : float (default=None)
Optimal distance between nodes. If None the distance is set to
1/sqrt(n) where n is the number of nodes. Increase this value
to move nodes farther apart.
pos : dict or None optional (default=None)
Initial positions for nodes as a dictionary with node as keys
and values as a coordinate list or tuple. If None, then use
random initial positions.
fixed : list or None optional (default=None)
Nodes to keep fixed at initial position.
Nodes not in ``G.nodes`` are ignored.
ValueError raised if `fixed` specified and `pos` not.
iterations : int optional (default=50)
Maximum number of iterations taken
threshold: float optional (default = 1e-4)
Threshold for relative error in node position changes.
The iteration stops if the error is below this threshold.
weight : string or None optional (default='weight')
The edge attribute that holds the numerical value used for
the edge weight. Larger means a stronger attractive force.
If None, then all edge weights are 1.
scale : number or None (default: 1)
Scale factor for positions. Not used unless `fixed is None`.
If scale is None, no rescaling is performed.
center : array-like or None
Coordinate pair around which to center the layout.
Not used unless `fixed is None`.
dim : int
Dimension of layout.
seed : int, RandomState instance or None optional (default=None)
Used only for the initial positions in the algorithm.
Set the random state for deterministic node layouts.
If int, `seed` is the seed used by the random number generator,
if numpy.random.RandomState instance, `seed` is the random
number generator,
if None, the random number generator is the RandomState instance used
by numpy.random.
store_pos_as : str, default None
If non-None, the position of each node will be stored on the graph as
an attribute with this string as its name, which can be accessed with
``G.nodes[...][store_pos_as]``. The function still returns the dictionary.
method : str optional (default='auto')
The method to compute the layout.
If 'force', the force-directed Fruchterman-Reingold algorithm [1]_ is used.
If 'energy', the energy-based optimization algorithm [2]_ is used with absolute
values of edge weights and gravitational forces acting on each connected component.
If 'auto', we use 'force' if ``len(G) < 500`` and 'energy' otherwise.
gravity: float optional (default=1.0)
Used only for the method='energy'.
The positive coefficient of gravitational forces per connected component.
Returns
-------
pos : dict
A dictionary of positions keyed by node
Examples
--------
>>> from pprint import pprint
>>> G = nx.path_graph(4)
>>> pos = nx.spring_layout(G)
>>> # suppress the returned dict and store on the graph directly
>>> _ = nx.spring_layout(G, seed=123, store_pos_as="pos")
>>> pprint(nx.get_node_attributes(G, "pos"))
{0: array([-0.61520994, -1. ]),
1: array([-0.21840965, -0.35501755]),
2: array([0.21841264, 0.35502078]),
3: array([0.61520696, 0.99999677])}
# The same using longer but equivalent function name
>>> pos = nx.fruchterman_reingold_layout(G)
References
----------
.. [1] Fruchterman, Thomas MJ, and Edward M. Reingold.
"Graph drawing by force-directed placement."
Software: Practice and experience 21, no. 11 (1991): 1129-1164.
http://dx.doi.org/10.1002/spe.4380211102
.. [2] Hamaguchi, Hiroki, Naoki Marumo, and Akiko Takeda.
"Initial Placement for Fruchterman--Reingold Force Model With Coordinate Newton Direction."
arXiv preprint arXiv:2412.20317 (2024).
https://arxiv.org/abs/2412.20317
"""
import numpy as np
if method not in ("auto", "force", "energy"):
raise ValueError("the method must be either auto, force, or energy.")
if method == "auto":
method = "force" if len(G) < 500 else "energy"
G, center = _process_params(G, center, dim)
if fixed is not None:
if pos is None:
raise ValueError("nodes are fixed without positions given")
for node in fixed:
if node not in pos:
raise ValueError("nodes are fixed without positions given")
nfixed = {node: i for i, node in enumerate(G)}
fixed = np.asarray([nfixed[node] for node in fixed if node in nfixed])
if pos is not None:
# Determine size of existing domain to adjust initial positions
dom_size = max(coord for pos_tup in pos.values() for coord in pos_tup)
if dom_size == 0:
dom_size = 1
pos_arr = seed.rand(len(G), dim) * dom_size + center
for i, n in enumerate(G):
if n in pos:
pos_arr[i] = np.asarray(pos[n])
else:
pos_arr = None
dom_size = 1
if len(G) == 0:
return {}
if len(G) == 1:
pos = {nx.utils.arbitrary_element(G.nodes()): center}
if store_pos_as is not None:
nx.set_node_attributes(G, pos, store_pos_as)
return pos
# Sparse matrix
if len(G) >= 500 or method == "energy":
A = nx.to_scipy_sparse_array(G, weight=weight, dtype="f")
if k is None and fixed is not None:
# We must adjust k by domain size for layouts not near 1x1
nnodes, _ = A.shape
k = dom_size / np.sqrt(nnodes)
pos = _sparse_fruchterman_reingold(
A, k, pos_arr, fixed, iterations, threshold, dim, seed, method, gravity
)
else:
A = nx.to_numpy_array(G, weight=weight)
if k is None and fixed is not None:
# We must adjust k by domain size for layouts not near 1x1
nnodes, _ = A.shape
k = dom_size / np.sqrt(nnodes)
pos = _fruchterman_reingold(
A, k, pos_arr, fixed, iterations, threshold, dim, seed
)
if fixed is None and scale is not None:
pos = rescale_layout(pos, scale=scale) + center
pos = dict(zip(G, pos))
if store_pos_as is not None:
nx.set_node_attributes(G, pos, store_pos_as)
return pos
fruchterman_reingold_layout = spring_layout
@np_random_state(7)
def _fruchterman_reingold(
A, k=None, pos=None, fixed=None, iterations=50, threshold=1e-4, dim=2, seed=None
):
# Position nodes in adjacency matrix A using Fruchterman-Reingold
# Entry point for NetworkX graph is fruchterman_reingold_layout()
import numpy as np
try:
nnodes, _ = A.shape
except AttributeError as err:
msg = "fruchterman_reingold() takes an adjacency matrix as input"
raise nx.NetworkXError(msg) from err
if pos is None:
# random initial positions
pos = np.asarray(seed.rand(nnodes, dim), dtype=A.dtype)
else:
# make sure positions are of same type as matrix
pos = pos.astype(A.dtype)
# optimal distance between nodes
if k is None:
k = np.sqrt(1.0 / nnodes)
# the initial "temperature" is about .1 of domain area (=1x1)
# this is the largest step allowed in the dynamics.
# We need to calculate this in case our fixed positions force our domain
# to be much bigger than 1x1
t = max(max(pos.T[0]) - min(pos.T[0]), max(pos.T[1]) - min(pos.T[1])) * 0.1
# simple cooling scheme.
# linearly step down by dt on each iteration so last iteration is size dt.
dt = t / (iterations + 1)
delta = np.zeros((pos.shape[0], pos.shape[0], pos.shape[1]), dtype=A.dtype)
# the inscrutable (but fast) version
# this is still O(V^2)
# could use multilevel methods to speed this up significantly
for iteration in range(iterations):
# matrix of difference between points
delta = pos[:, np.newaxis, :] - pos[np.newaxis, :, :]
# distance between points
distance = np.linalg.norm(delta, axis=-1)
# enforce minimum distance of 0.01
np.clip(distance, 0.01, None, out=distance)
# displacement "force"
displacement = np.einsum(
"ijk,ij->ik", delta, (k * k / distance**2 - A * distance / k)
)
# update positions
length = np.linalg.norm(displacement, axis=-1)
length = np.where(length < 0.01, 0.1, length)
delta_pos = np.einsum("ij,i->ij", displacement, t / length)
if fixed is not None:
# don't change positions of fixed nodes
delta_pos[fixed] = 0.0
pos += delta_pos
# cool temperature
t -= dt
if (np.linalg.norm(delta_pos) / nnodes) < threshold:
break
return pos
@np_random_state(7)
def _sparse_fruchterman_reingold(
A,
k=None,
pos=None,
fixed=None,
iterations=50,
threshold=1e-4,
dim=2,
seed=None,
method="energy",
gravity=1.0,
):
# Position nodes in adjacency matrix A using Fruchterman-Reingold
# Entry point for NetworkX graph is fruchterman_reingold_layout()
# Sparse version
import numpy as np
import scipy as sp
try:
nnodes, _ = A.shape
except AttributeError as err:
msg = "fruchterman_reingold() takes an adjacency matrix as input"
raise nx.NetworkXError(msg) from err
if pos is None:
# random initial positions
pos = np.asarray(seed.rand(nnodes, dim), dtype=A.dtype)
else:
# make sure positions are of same type as matrix
pos = pos.astype(A.dtype)
# no fixed nodes
if fixed is None:
fixed = []
# optimal distance between nodes
if k is None:
k = np.sqrt(1.0 / nnodes)
if method == "energy":
return _energy_fruchterman_reingold(
A, nnodes, k, pos, fixed, iterations, threshold, dim, gravity
)
# make sure we have a LIst of Lists representation
try:
A = A.tolil()
except AttributeError:
A = (sp.sparse.coo_array(A)).tolil()
# the initial "temperature" is about .1 of domain area (=1x1)
# this is the largest step allowed in the dynamics.
t = max(max(pos.T[0]) - min(pos.T[0]), max(pos.T[1]) - min(pos.T[1])) * 0.1
# simple cooling scheme.
# linearly step down by dt on each iteration so last iteration is size dt.
dt = t / (iterations + 1)
displacement = np.zeros((dim, nnodes))
for iteration in range(iterations):
displacement *= 0
# loop over rows
for i in range(A.shape[0]):
if i in fixed:
continue
# difference between this row's node position and all others
delta = (pos[i] - pos).T
# distance between points
distance = np.sqrt((delta**2).sum(axis=0))
# enforce minimum distance of 0.01
distance = np.where(distance < 0.01, 0.01, distance)
# the adjacency matrix row
Ai = A.getrowview(i).toarray() # TODO: revisit w/ sparse 1D container
# displacement "force"
displacement[:, i] += (
delta * (k * k / distance**2 - Ai * distance / k)
).sum(axis=1)
# update positions
length = np.sqrt((displacement**2).sum(axis=0))
length = np.where(length < 0.01, 0.1, length)
delta_pos = (displacement * t / length).T
pos += delta_pos
# cool temperature
t -= dt
if (np.linalg.norm(delta_pos) / nnodes) < threshold:
break
return pos
def _energy_fruchterman_reingold(
A, nnodes, k, pos, fixed, iterations, threshold, dim, gravity
):
# Entry point for NetworkX graph is fruchterman_reingold_layout()
# energy-based version
import numpy as np
import scipy as sp
if gravity <= 0:
raise ValueError(f"the gravity must be positive.")
# make sure we have a Compressed Sparse Row format
try:
A = A.tocsr()
except AttributeError:
A = sp.sparse.csr_array(A)
# Take absolute values of edge weights and symmetrize it
A = np.abs(A)
A = (A + A.T) / 2
n_components, labels = sp.sparse.csgraph.connected_components(A, directed=False)
bincount = np.bincount(labels)
batchsize = 500
def _cost_FR(x):
pos = x.reshape((nnodes, dim))
grad = np.zeros((nnodes, dim))
cost = 0.0
for l in range(0, nnodes, batchsize):
r = min(l + batchsize, nnodes)
# difference between selected node positions and all others
delta = pos[l:r, np.newaxis, :] - pos[np.newaxis, :, :]
# distance between points with a minimum distance of 1e-5
distance2 = np.sum(delta * delta, axis=2)
distance2 = np.maximum(distance2, 1e-10)
distance = np.sqrt(distance2)
# temporary variable for calculation
Ad = A[l:r] * distance
# attractive forces and repulsive forces
grad[l:r] = 2 * np.einsum("ij,ijk->ik", Ad / k - k**2 / distance2, delta)
# integrated attractive forces
cost += np.sum(Ad * distance2) / (3 * k)
# integrated repulsive forces
cost -= k**2 * np.sum(np.log(distance))
# gravitational force from the centroids of connected components to (0.5, ..., 0.5)^T
centers = np.zeros((n_components, dim))
np.add.at(centers, labels, pos)
delta0 = centers / bincount[:, np.newaxis] - 0.5
grad += gravity * delta0[labels]
cost += gravity * 0.5 * np.sum(bincount * np.linalg.norm(delta0, axis=1) ** 2)
# fix positions of fixed nodes
grad[fixed] = 0.0
return cost, grad.ravel()
# Optimization of the energy function by L-BFGS algorithm
options = {"maxiter": iterations, "gtol": threshold}
return sp.optimize.minimize(
_cost_FR, pos.ravel(), method="L-BFGS-B", jac=True, options=options
).x.reshape((nnodes, dim))
def kamada_kawai_layout(
G,
dist=None,
pos=None,
weight="weight",
scale=1,
center=None,
dim=2,
store_pos_as=None,
):
"""Position nodes using Kamada-Kawai path-length cost-function.
Parameters
----------
G : NetworkX graph or list of nodes
A position will be assigned to every node in G.
dist : dict (default=None)
A two-level dictionary of optimal distances between nodes,
indexed by source and destination node.
If None, the distance is computed using shortest_path_length().
pos : dict or None optional (default=None)
Initial positions for nodes as a dictionary with node as keys
and values as a coordinate list or tuple. If None, then use
circular_layout() for dim >= 2 and a linear layout for dim == 1.
weight : string or None optional (default='weight')
The edge attribute that holds the numerical value used for
the edge weight. If None, then all edge weights are 1.
scale : number (default: 1)
Scale factor for positions.
center : array-like or None
Coordinate pair around which to center the layout.
dim : int
Dimension of layout.
store_pos_as : str, default None
If non-None, the position of each node will be stored on the graph as
an attribute with this string as its name, which can be accessed with
``G.nodes[...][store_pos_as]``. The function still returns the dictionary.
Returns
-------
pos : dict
A dictionary of positions keyed by node
Examples
--------
>>> from pprint import pprint
>>> G = nx.path_graph(4)
>>> pos = nx.kamada_kawai_layout(G)
>>> # suppress the returned dict and store on the graph directly
>>> _ = nx.kamada_kawai_layout(G, store_pos_as="pos")
>>> pprint(nx.get_node_attributes(G, "pos"))
{0: array([0.99996577, 0.99366857]),
1: array([0.32913544, 0.33543827]),
2: array([-0.33544334, -0.32910684]),
3: array([-0.99365787, -1. ])}
"""
import numpy as np
G, center = _process_params(G, center, dim)
nNodes = len(G)
if nNodes == 0:
return {}
if dist is None:
dist = dict(nx.shortest_path_length(G, weight=weight))
dist_mtx = 1e6 * np.ones((nNodes, nNodes))
for row, nr in enumerate(G):
if nr not in dist:
continue
rdist = dist[nr]
for col, nc in enumerate(G):
if nc not in rdist:
continue
dist_mtx[row][col] = rdist[nc]
if pos is None:
if dim >= 3:
pos = random_layout(G, dim=dim)
elif dim == 2:
pos = circular_layout(G, dim=dim)
else:
pos = dict(zip(G, np.linspace(0, 1, len(G))))
pos_arr = np.array([pos[n] for n in G])
pos = _kamada_kawai_solve(dist_mtx, pos_arr, dim)
pos = rescale_layout(pos, scale=scale) + center
pos = dict(zip(G, pos))
if store_pos_as is not None:
nx.set_node_attributes(G, pos, store_pos_as)
return pos
def _kamada_kawai_solve(dist_mtx, pos_arr, dim):
# Anneal node locations based on the Kamada-Kawai cost-function,
# using the supplied matrix of preferred inter-node distances,
# and starting locations.
import numpy as np
import scipy as sp
meanwt = 1e-3
costargs = (np, 1 / (dist_mtx + np.eye(dist_mtx.shape[0]) * 1e-3), meanwt, dim)
optresult = sp.optimize.minimize(
_kamada_kawai_costfn,
pos_arr.ravel(),
method="L-BFGS-B",
args=costargs,
jac=True,
)
return optresult.x.reshape((-1, dim))
def _kamada_kawai_costfn(pos_vec, np, invdist, meanweight, dim):
# Cost-function and gradient for Kamada-Kawai layout algorithm
nNodes = invdist.shape[0]
pos_arr = pos_vec.reshape((nNodes, dim))
delta = pos_arr[:, np.newaxis, :] - pos_arr[np.newaxis, :, :]
nodesep = np.linalg.norm(delta, axis=-1)
direction = np.einsum("ijk,ij->ijk", delta, 1 / (nodesep + np.eye(nNodes) * 1e-3))
offset = nodesep * invdist - 1.0
offset[np.diag_indices(nNodes)] = 0
cost = 0.5 * np.sum(offset**2)
grad = np.einsum("ij,ij,ijk->ik", invdist, offset, direction) - np.einsum(
"ij,ij,ijk->jk", invdist, offset, direction
)
# Additional parabolic term to encourage mean position to be near origin:
sumpos = np.sum(pos_arr, axis=0)
cost += 0.5 * meanweight * np.sum(sumpos**2)
grad += meanweight * sumpos
return (cost, grad.ravel())
def spectral_layout(G, weight="weight", scale=1, center=None, dim=2, store_pos_as=None):
"""Position nodes using the eigenvectors of the graph Laplacian.
Using the unnormalized Laplacian, the layout shows possible clusters of
nodes which are an approximation of the ratio cut. If dim is the number of
dimensions then the positions are the entries of the dim eigenvectors
corresponding to the ascending eigenvalues starting from the second one.
Parameters
----------
G : NetworkX graph or list of nodes
A position will be assigned to every node in G.
weight : string or None optional (default='weight')
The edge attribute that holds the numerical value used for
the edge weight. If None, then all edge weights are 1.
scale : number (default: 1)
Scale factor for positions.
center : array-like or None
Coordinate pair around which to center the layout.
dim : int
Dimension of layout.
store_pos_as : str, default None
If non-None, the position of each node will be stored on the graph as
an attribute with this string as its name, which can be accessed with
``G.nodes[...][store_pos_as]``. The function still returns the dictionary.
Returns
-------
pos : dict
A dictionary of positions keyed by node
Examples
--------
>>> from pprint import pprint
>>> G = nx.path_graph(4)
>>> pos = nx.spectral_layout(G)
>>> # suppress the returned dict and store on the graph directly
>>> _ = nx.spectral_layout(G, store_pos_as="pos")
>>> pprint(nx.get_node_attributes(G, "pos"))
{0: array([-1. , 0.76536686]),
1: array([-0.41421356, -0.76536686]),
2: array([ 0.41421356, -0.76536686]),
3: array([1. , 0.76536686])}
Notes
-----
Directed graphs will be considered as undirected graphs when
positioning the nodes.
For larger graphs (>500 nodes) this will use the SciPy sparse
eigenvalue solver (ARPACK).
"""
# handle some special cases that break the eigensolvers
import numpy as np
G, center = _process_params(G, center, dim)
if len(G) <= 2:
if len(G) == 0:
pos = np.array([])
elif len(G) == 1:
pos = np.array([center])
else:
pos = np.array([np.zeros(dim), np.array(center) * 2.0])
return dict(zip(G, pos))
try:
# Sparse matrix
if len(G) < 500: # dense solver is faster for small graphs
raise ValueError
A = nx.to_scipy_sparse_array(G, weight=weight, dtype="d")
# Symmetrize directed graphs
if G.is_directed():
A = A + np.transpose(A)
pos = _sparse_spectral(A, dim)
except (ImportError, ValueError):
# Dense matrix
A = nx.to_numpy_array(G, weight=weight)
# Symmetrize directed graphs
if G.is_directed():
A += A.T
pos = _spectral(A, dim)
pos = rescale_layout(pos, scale=scale) + center
pos = dict(zip(G, pos))
if store_pos_as is not None:
nx.set_node_attributes(G, pos, store_pos_as)
return pos
def _spectral(A, dim=2):
# Input adjacency matrix A
# Uses dense eigenvalue solver from numpy
import numpy as np
try:
nnodes, _ = A.shape
except AttributeError as err:
msg = "spectral() takes an adjacency matrix as input"
raise nx.NetworkXError(msg) from err
# form Laplacian matrix where D is diagonal of degrees
D = np.identity(nnodes, dtype=A.dtype) * np.sum(A, axis=1)
L = D - A
eigenvalues, eigenvectors = np.linalg.eig(L)
# sort and keep smallest nonzero
index = np.argsort(eigenvalues)[1 : dim + 1] # 0 index is zero eigenvalue
return np.real(eigenvectors[:, index])
def _sparse_spectral(A, dim=2):
# Input adjacency matrix A
# Uses sparse eigenvalue solver from scipy
# Could use multilevel methods here, see Koren "On spectral graph drawing"
import numpy as np
import scipy as sp
try:
nnodes, _ = A.shape
except AttributeError as err:
msg = "sparse_spectral() takes an adjacency matrix as input"
raise nx.NetworkXError(msg) from err
# form Laplacian matrix
# TODO: Rm csr_array wrapper in favor of spdiags array constructor when available
D = sp.sparse.csr_array(sp.sparse.spdiags(A.sum(axis=1), 0, nnodes, nnodes))
L = D - A
k = dim + 1
# number of Lanczos vectors for ARPACK solver.What is the right scaling?
ncv = max(2 * k + 1, int(np.sqrt(nnodes)))
# return smallest k eigenvalues and eigenvectors
eigenvalues, eigenvectors = sp.sparse.linalg.eigsh(L, k, which="SM", ncv=ncv)
index = np.argsort(eigenvalues)[1:k] # 0 index is zero eigenvalue
return np.real(eigenvectors[:, index])
def planar_layout(G, scale=1, center=None, dim=2, store_pos_as=None):
"""Position nodes without edge intersections.
Parameters
----------
G : NetworkX graph or list of nodes
A position will be assigned to every node in G. If G is of type
nx.PlanarEmbedding, the positions are selected accordingly.
scale : number (default: 1)
Scale factor for positions.
center : array-like or None
Coordinate pair around which to center the layout.
dim : int
Dimension of layout.
store_pos_as : str, default None
If non-None, the position of each node will be stored on the graph as
an attribute with this string as its name, which can be accessed with
``G.nodes[...][store_pos_as]``. The function still returns the dictionary.
Returns
-------
pos : dict
A dictionary of positions keyed by node
Raises
------
NetworkXException
If G is not planar
Examples
--------
>>> from pprint import pprint
>>> G = nx.path_graph(4)
>>> pos = nx.planar_layout(G)
>>> # suppress the returned dict and store on the graph directly
>>> _ = nx.planar_layout(G, store_pos_as="pos")
>>> pprint(nx.get_node_attributes(G, "pos"))
{0: array([-0.77777778, -0.33333333]),
1: array([ 1. , -0.33333333]),
2: array([0.11111111, 0.55555556]),
3: array([-0.33333333, 0.11111111])}
"""
import numpy as np
if dim != 2:
raise ValueError("can only handle 2 dimensions")
G, center = _process_params(G, center, dim)
if len(G) == 0:
return {}
if isinstance(G, nx.PlanarEmbedding):
embedding = G
else:
is_planar, embedding = nx.check_planarity(G)
if not is_planar:
raise nx.NetworkXException("G is not planar.")
pos = nx.combinatorial_embedding_to_pos(embedding)
node_list = list(embedding)
pos = np.vstack([pos[x] for x in node_list])
pos = pos.astype(np.float64)
pos = rescale_layout(pos, scale=scale) + center
pos = dict(zip(node_list, pos))
if store_pos_as is not None:
nx.set_node_attributes(G, pos, store_pos_as)
return pos
def spiral_layout(
G,
scale=1,
center=None,
dim=2,
resolution=0.35,
equidistant=False,
store_pos_as=None,
):
"""Position nodes in a spiral layout.
Parameters
----------
G : NetworkX graph or list of nodes
A position will be assigned to every node in G.
scale : number (default: 1)
Scale factor for positions.
center : array-like or None
Coordinate pair around which to center the layout.
dim : int, default=2
Dimension of layout, currently only dim=2 is supported.
Other dimension values result in a ValueError.
resolution : float, default=0.35
The compactness of the spiral layout returned.
Lower values result in more compressed spiral layouts.
equidistant : bool, default=False
If True, nodes will be positioned equidistant from each other
by decreasing angle further from center.
If False, nodes will be positioned at equal angles
from each other by increasing separation further from center.
store_pos_as : str, default None
If non-None, the position of each node will be stored on the graph as
an attribute with this string as its name, which can be accessed with
``G.nodes[...][store_pos_as]``. The function still returns the dictionary.
Returns
-------
pos : dict
A dictionary of positions keyed by node
Raises
------
ValueError
If dim != 2
Examples
--------
>>> from pprint import pprint
>>> G = nx.path_graph(4)
>>> pos = nx.spiral_layout(G)
>>> nx.draw(G, pos=pos)
>>> # suppress the returned dict and store on the graph directly
>>> _ = nx.spiral_layout(G, store_pos_as="pos")
>>> pprint(nx.get_node_attributes(G, "pos"))
{0: array([-0.64153279, -0.68555087]),
1: array([-0.03307913, -0.46344795]),
2: array([0.34927952, 0.14899882]),
3: array([0.32533239, 1. ])}
Notes
-----
This algorithm currently only works in two dimensions.
"""
import numpy as np
if dim != 2:
raise ValueError("can only handle 2 dimensions")
G, center = _process_params(G, center, dim)
if len(G) == 0:
return {}
if len(G) == 1:
pos = {nx.utils.arbitrary_element(G): center}
if store_pos_as is not None:
nx.set_node_attributes(G, pos, store_pos_as)
return pos
pos = []
if equidistant:
chord = 1
step = 0.5
theta = resolution
theta += chord / (step * theta)
for _ in range(len(G)):
r = step * theta
theta += chord / r
pos.append([np.cos(theta) * r, np.sin(theta) * r])
else:
dist = np.arange(len(G), dtype=float)
angle = resolution * dist
pos = np.transpose(dist * np.array([np.cos(angle), np.sin(angle)]))
pos = rescale_layout(np.array(pos), scale=scale) + center
pos = dict(zip(G, pos))
if store_pos_as is not None:
nx.set_node_attributes(G, pos, store_pos_as)
return pos
def multipartite_layout(
G, subset_key="subset", align="vertical", scale=1, center=None, store_pos_as=None
):
"""Position nodes in layers of straight lines.
Parameters
----------
G : NetworkX graph or list of nodes
A position will be assigned to every node in G.
subset_key : string or dict (default='subset')
If a string, the key of node data in G that holds the node subset.
If a dict, keyed by layer number to the nodes in that layer/subset.
align : string (default='vertical')
The alignment of nodes. Vertical or horizontal.
scale : number (default: 1)
Scale factor for positions.
center : array-like or None
Coordinate pair around which to center the layout.
store_pos_as : str, default None
If non-None, the position of each node will be stored on the graph as
an attribute with this string as its name, which can be accessed with
``G.nodes[...][store_pos_as]``. The function still returns the dictionary.
Returns
-------
pos : dict
A dictionary of positions keyed by node.
Examples
--------
>>> G = nx.complete_multipartite_graph(28, 16, 10)
>>> pos = nx.multipartite_layout(G)
>>> # suppress the returned dict and store on the graph directly
>>> G = nx.complete_multipartite_graph(28, 16, 10)
>>> _ = nx.multipartite_layout(G, store_pos_as="pos")
or use a dict to provide the layers of the layout
>>> G = nx.Graph([(0, 1), (1, 2), (1, 3), (3, 4)])
>>> layers = {"a": [0], "b": [1], "c": [2, 3], "d": [4]}
>>> pos = nx.multipartite_layout(G, subset_key=layers)
Notes
-----
This algorithm currently only works in two dimensions and does not
try to minimize edge crossings.
Network does not need to be a complete multipartite graph. As long as nodes
have subset_key data, they will be placed in the corresponding layers.
"""
import numpy as np
if align not in ("vertical", "horizontal"):
msg = "align must be either vertical or horizontal."
raise ValueError(msg)
G, center = _process_params(G, center=center, dim=2)
if len(G) == 0:
return {}
try:
# check if subset_key is dict-like
if len(G) != sum(len(nodes) for nodes in subset_key.values()):
raise nx.NetworkXError(
"all nodes must be in one subset of `subset_key` dict"
)
except AttributeError:
# subset_key is not a dict, hence a string
node_to_subset = nx.get_node_attributes(G, subset_key)
if len(node_to_subset) != len(G):
raise nx.NetworkXError(
f"all nodes need a subset_key attribute: {subset_key}"
)
subset_key = nx.utils.groups(node_to_subset)
# Sort by layer, if possible
try:
layers = dict(sorted(subset_key.items()))
except TypeError:
layers = subset_key
pos = None
nodes = []
width = len(layers)
for i, layer in enumerate(layers.values()):
height = len(layer)
xs = np.repeat(i, height)
ys = np.arange(0, height, dtype=float)
offset = ((width - 1) / 2, (height - 1) / 2)
layer_pos = np.column_stack([xs, ys]) - offset
if pos is None:
pos = layer_pos
else:
pos = np.concatenate([pos, layer_pos])
nodes.extend(layer)
pos = rescale_layout(pos, scale=scale) + center
if align == "horizontal":
pos = pos[:, ::-1] # swap x and y coords
pos = dict(zip(nodes, pos))
if store_pos_as is not None:
nx.set_node_attributes(G, pos, store_pos_as)
return pos
@np_random_state("seed")
def arf_layout(
G,
pos=None,
scaling=1,
a=1.1,
etol=1e-6,
dt=1e-3,
max_iter=1000,
*,
seed=None,
store_pos_as=None,
):
"""Arf layout for networkx
The attractive and repulsive forces (arf) layout [1] improves the spring
layout in three ways. First, it prevents congestion of highly connected nodes
due to strong forcing between nodes. Second, it utilizes the layout space
more effectively by preventing large gaps that spring layout tends to create.
Lastly, the arf layout represents symmetries in the layout better than the
default spring layout.
Parameters
----------
G : nx.Graph or nx.DiGraph
Networkx graph.
pos : dict
Initial position of the nodes. If set to None a
random layout will be used.
scaling : float
Scales the radius of the circular layout space.
a : float
Strength of springs between connected nodes. Should be larger than 1.
The greater a, the clearer the separation of unconnected sub clusters.
etol : float
Gradient sum of spring forces must be larger than `etol` before successful
termination.
dt : float
Time step for force differential equation simulations.
max_iter : int
Max iterations before termination of the algorithm.
seed : int, RandomState instance or None optional (default=None)
Set the random state for deterministic node layouts.
If int, `seed` is the seed used by the random number generator,
if numpy.random.RandomState instance, `seed` is the random
number generator,
if None, the random number generator is the RandomState instance used
by numpy.random.
store_pos_as : str, default None
If non-None, the position of each node will be stored on the graph as
an attribute with this string as its name, which can be accessed with
``G.nodes[...][store_pos_as]``. The function still returns the dictionary.
Returns
-------
pos : dict
A dictionary of positions keyed by node.
Examples
--------
>>> G = nx.grid_graph((5, 5))
>>> pos = nx.arf_layout(G)
>>> # suppress the returned dict and store on the graph directly
>>> G = nx.grid_graph((5, 5))
>>> _ = nx.arf_layout(G, store_pos_as="pos")
References
----------
.. [1] "Self-Organization Applied to Dynamic Network Layout", M. Geipel,
International Journal of Modern Physics C, 2007, Vol 18, No 10,
pp. 1537-1549.
https://doi.org/10.1142/S0129183107011558 https://arxiv.org/abs/0704.1748
"""
import warnings
import numpy as np
if a <= 1:
msg = "The parameter a should be larger than 1"
raise ValueError(msg)
pos_tmp = nx.random_layout(G, seed=seed)
if pos is None:
pos = pos_tmp
else:
for node in G.nodes():
if node not in pos:
pos[node] = pos_tmp[node].copy()
# Initialize spring constant matrix
N = len(G)
# No nodes no computation
if N == 0:
return pos
# init force of springs
K = np.ones((N, N)) - np.eye(N)
node_order = {node: i for i, node in enumerate(G)}
for x, y in G.edges():
if x != y:
idx, jdx = (node_order[i] for i in (x, y))
K[idx, jdx] = a
# vectorize values
p = np.asarray(list(pos.values()))
# equation 10 in [1]
rho = scaling * np.sqrt(N)
# looping variables
error = etol + 1
n_iter = 0
while error > etol:
diff = p[:, np.newaxis] - p[np.newaxis]
A = np.linalg.norm(diff, axis=-1)[..., np.newaxis]
# attraction_force - repulsions force
# suppress nans due to division; caused by diagonal set to zero.
# Does not affect the computation due to nansum
with warnings.catch_warnings():
warnings.simplefilter("ignore")
change = K[..., np.newaxis] * diff - rho / A * diff
change = np.nansum(change, axis=0)
p += change * dt
error = np.linalg.norm(change, axis=-1).sum()
if n_iter > max_iter:
break
n_iter += 1
pos = dict(zip(G.nodes(), p))
if store_pos_as is not None:
nx.set_node_attributes(G, pos, store_pos_as)
return pos
@np_random_state("seed")
@nx._dispatchable(edge_attrs="weight", mutates_input={"store_pos_as": 15})
def forceatlas2_layout(
G,
pos=None,
*,
max_iter=100,
jitter_tolerance=1.0,
scaling_ratio=2.0,
gravity=1.0,
distributed_action=False,
strong_gravity=False,
node_mass=None,
node_size=None,
weight=None,
dissuade_hubs=False,
linlog=False,
seed=None,
dim=2,
store_pos_as=None,
):
"""Position nodes using the ForceAtlas2 force-directed layout algorithm.
This function applies the ForceAtlas2 layout algorithm [1]_ to a NetworkX graph,
positioning the nodes in a way that visually represents the structure of the graph.
The algorithm uses physical simulation to minimize the energy of the system,
resulting in a more readable layout.
Parameters
----------
G : nx.Graph
A NetworkX graph to be laid out.
pos : dict or None, optional
Initial positions of the nodes. If None, random initial positions are used.
max_iter : int (default: 100)
Number of iterations for the layout optimization.
jitter_tolerance : float (default: 1.0)
Controls the tolerance for adjusting the speed of layout generation.
scaling_ratio : float (default: 2.0)
Determines the scaling of attraction and repulsion forces.
gravity : float (default: 1.0)
Determines the amount of attraction on nodes to the center. Prevents islands
(i.e. weakly connected or disconnected parts of the graph)
from drifting away.
distributed_action : bool (default: False)
Distributes the attraction force evenly among nodes.
strong_gravity : bool (default: False)
Applies a strong gravitational pull towards the center.
node_mass : dict or None, optional
Maps nodes to their masses, influencing the attraction to other nodes.
node_size : dict or None, optional
Maps nodes to their sizes, preventing crowding by creating a halo effect.
weight : string or None, optional (default: None)
The edge attribute that holds the numerical value used for
the edge weight. If None, then all edge weights are 1.
dissuade_hubs : bool (default: False)
Prevents the clustering of hub nodes.
linlog : bool (default: False)
Uses logarithmic attraction instead of linear.
seed : int, RandomState instance or None optional (default=None)
Used only for the initial positions in the algorithm.
Set the random state for deterministic node layouts.
If int, `seed` is the seed used by the random number generator,
if numpy.random.RandomState instance, `seed` is the random
number generator,
if None, the random number generator is the RandomState instance used
by numpy.random.
dim : int (default: 2)
Sets the dimensions for the layout. Ignored if `pos` is provided.
store_pos_as : str, default None
If non-None, the position of each node will be stored on the graph as
an attribute with this string as its name, which can be accessed with
``G.nodes[...][store_pos_as]``. The function still returns the dictionary.
Examples
--------
>>> import networkx as nx
>>> G = nx.florentine_families_graph()
>>> pos = nx.forceatlas2_layout(G)
>>> nx.draw(G, pos=pos)
>>> # suppress the returned dict and store on the graph directly
>>> pos = nx.forceatlas2_layout(G, store_pos_as="pos")
>>> _ = nx.forceatlas2_layout(G, store_pos_as="pos")
References
----------
.. [1] Jacomy, M., Venturini, T., Heymann, S., & Bastian, M. (2014).
ForceAtlas2, a continuous graph layout algorithm for handy network
visualization designed for the Gephi software. PloS one, 9(6), e98679.
https://doi.org/10.1371/journal.pone.0098679
"""
import numpy as np
if len(G) == 0:
return {}
# parse optional pos positions
if pos is None:
pos = nx.random_layout(G, dim=dim, seed=seed)
pos_arr = np.array(list(pos.values()))
elif len(pos) == len(G):
pos_arr = np.array([pos[node].copy() for node in G])
else:
# set random node pos within the initial pos values
pos_init = np.array(list(pos.values()))
max_pos = pos_init.max(axis=0)
min_pos = pos_init.min(axis=0)
dim = max_pos.size
pos_arr = min_pos + seed.rand(len(G), dim) * (max_pos - min_pos)
for idx, node in enumerate(G):
if node in pos:
pos_arr[idx] = pos[node].copy()
mass = np.zeros(len(G))
size = np.zeros(len(G))
# Only adjust for size when the users specifies size other than default (1)
adjust_sizes = False
if node_size is None:
node_size = {}
else:
adjust_sizes = True
if node_mass is None:
node_mass = {}
for idx, node in enumerate(G):
mass[idx] = node_mass.get(node, G.degree(node) + 1)
size[idx] = node_size.get(node, 1)
n = len(G)
gravities = np.zeros((n, dim))
attraction = np.zeros((n, dim))
repulsion = np.zeros((n, dim))
A = nx.to_numpy_array(G, weight=weight)
def estimate_factor(n, swing, traction, speed, speed_efficiency, jitter_tolerance):
"""Computes the scaling factor for the force in the ForceAtlas2 layout algorithm.
This helper function adjusts the speed and
efficiency of the layout generation based on the
current state of the system, such as the number of
nodes, current swing, and traction forces.
Parameters
----------
n : int
Number of nodes in the graph.
swing : float
The current swing, representing the oscillation of the nodes.
traction : float
The current traction force, representing the attraction between nodes.
speed : float
The current speed of the layout generation.
speed_efficiency : float
The efficiency of the current speed, influencing how fast the layout converges.
jitter_tolerance : float
The tolerance for jitter, affecting how much speed adjustment is allowed.
Returns
-------
tuple
A tuple containing the updated speed and speed efficiency.
Notes
-----
This function is a part of the ForceAtlas2 layout algorithm and is used to dynamically adjust the
layout parameters to achieve an optimal and stable visualization.
"""
import numpy as np
# estimate jitter
opt_jitter = 0.05 * np.sqrt(n)
min_jitter = np.sqrt(opt_jitter)
max_jitter = 10
min_speed_efficiency = 0.05
other = min(max_jitter, opt_jitter * traction / n**2)
jitter = jitter_tolerance * max(min_jitter, other)
if swing / traction > 2.0:
if speed_efficiency > min_speed_efficiency:
speed_efficiency *= 0.5
jitter = max(jitter, jitter_tolerance)
if swing == 0:
target_speed = np.inf
else:
target_speed = jitter * speed_efficiency * traction / swing
if swing > jitter * traction:
if speed_efficiency > min_speed_efficiency:
speed_efficiency *= 0.7
elif speed < 1000:
speed_efficiency *= 1.3
max_rise = 0.5
speed = speed + min(target_speed - speed, max_rise * speed)
return speed, speed_efficiency
speed = 1
speed_efficiency = 1
swing = 1
traction = 1
for _ in range(max_iter):
# compute pairwise difference
diff = pos_arr[:, None] - pos_arr[None]
# compute pairwise distance
distance = np.linalg.norm(diff, axis=-1)
# linear attraction
if linlog:
attraction = -np.log(1 + distance) / distance
np.fill_diagonal(attraction, 0)
attraction = np.einsum("ij, ij -> ij", attraction, A)
attraction = np.einsum("ijk, ij -> ik", diff, attraction)
else:
attraction = -np.einsum("ijk, ij -> ik", diff, A)
if distributed_action:
attraction /= mass[:, None]
# repulsion
tmp = mass[:, None] @ mass[None]
if adjust_sizes:
distance += -size[:, None] - size[None]
d2 = distance**2
# remove self-interaction
np.fill_diagonal(tmp, 0)
np.fill_diagonal(d2, 1)
factor = (tmp / d2) * scaling_ratio
repulsion = np.einsum("ijk, ij -> ik", diff, factor)
# gravity
pos_centered = pos_arr - np.mean(pos_arr, axis=0)
if strong_gravity:
gravities = -gravity * mass[:, None] * pos_centered
else:
# hide warnings for divide by zero. Then change nan to 0
with np.errstate(divide="ignore", invalid="ignore"):
unit_vec = pos_centered / np.linalg.norm(pos_centered, axis=-1)[:, None]
unit_vec = np.nan_to_num(unit_vec, nan=0)
gravities = -gravity * mass[:, None] * unit_vec
# total forces
update = attraction + repulsion + gravities
# compute total swing and traction
swing += (mass * np.linalg.norm(pos_arr - update, axis=-1)).sum()
traction += (0.5 * mass * np.linalg.norm(pos_arr + update, axis=-1)).sum()
speed, speed_efficiency = estimate_factor(
n,
swing,
traction,
speed,
speed_efficiency,
jitter_tolerance,
)
# update pos
if adjust_sizes:
df = np.linalg.norm(update, axis=-1)
swinging = mass * df
factor = 0.1 * speed / (1 + np.sqrt(speed * swinging))
factor = np.minimum(factor * df, 10.0 * np.ones(df.shape)) / df
else:
swinging = mass * np.linalg.norm(update, axis=-1)
factor = speed / (1 + np.sqrt(speed * swinging))
factored_update = update * factor[:, None]
pos_arr += factored_update
if abs(factored_update).sum() < 1e-10:
break
pos = dict(zip(G, pos_arr))
if store_pos_as is not None:
nx.set_node_attributes(G, pos, store_pos_as)
return pos
def rescale_layout(pos, scale=1):
"""Returns scaled position array to (-scale, scale) in all axes.
The function acts on NumPy arrays which hold position information.
Each position is one row of the array. The dimension of the space
equals the number of columns. Each coordinate in one column.
To rescale, the mean (center) is subtracted from each axis separately.
Then all values are scaled so that the largest magnitude value
from all axes equals `scale` (thus, the aspect ratio is preserved).
The resulting NumPy Array is returned (order of rows unchanged).
Parameters
----------
pos : numpy array
positions to be scaled. Each row is a position.
scale : number (default: 1)
The size of the resulting extent in all directions.
attribute : str, default None
If non-None, the position of each node will be stored on the graph as
an attribute named `attribute` which can be accessed with
`G.nodes[...][attribute]`. The function still returns the dictionary.
Returns
-------
pos : numpy array
scaled positions. Each row is a position.
See Also
--------
rescale_layout_dict
"""
import numpy as np
# Find max length over all dimensions
pos -= pos.mean(axis=0)
lim = np.abs(pos).max() # max coordinate for all axes
# rescale to (-scale, scale) in all directions, preserves aspect
if lim > 0:
pos *= scale / lim
return pos
def rescale_layout_dict(pos, scale=1):
"""Return a dictionary of scaled positions keyed by node
Parameters
----------
pos : A dictionary of positions keyed by node
scale : number (default: 1)
The size of the resulting extent in all directions.
Returns
-------
pos : A dictionary of positions keyed by node
Examples
--------
>>> import numpy as np
>>> pos = {0: np.array((0, 0)), 1: np.array((1, 1)), 2: np.array((0.5, 0.5))}
>>> nx.rescale_layout_dict(pos)
{0: array([-1., -1.]), 1: array([1., 1.]), 2: array([0., 0.])}
>>> pos = {0: np.array((0, 0)), 1: np.array((-1, 1)), 2: np.array((-0.5, 0.5))}
>>> nx.rescale_layout_dict(pos, scale=2)
{0: array([ 2., -2.]), 1: array([-2., 2.]), 2: array([0., 0.])}
See Also
--------
rescale_layout
"""
import numpy as np
if not pos: # empty_graph
return {}
pos_v = np.array(list(pos.values()))
pos_v = rescale_layout(pos_v, scale=scale)
return dict(zip(pos, pos_v))
def bfs_layout(G, start, *, align="vertical", scale=1, center=None, store_pos_as=None):
"""Position nodes according to breadth-first search algorithm.
Parameters
----------
G : NetworkX graph
A position will be assigned to every node in G.
start : node in `G`
Starting node for bfs
center : array-like or None
Coordinate pair around which to center the layout.
store_pos_as : str, default None
If non-None, the position of each node will be stored on the graph as
an attribute with this string as its name, which can be accessed with
``G.nodes[...][store_pos_as]``. The function still returns the dictionary.
Returns
-------
pos : dict
A dictionary of positions keyed by node.
Examples
--------
>>> from pprint import pprint
>>> G = nx.path_graph(4)
>>> pos = nx.bfs_layout(G, 0)
>>> # suppress the returned dict and store on the graph directly
>>> _ = nx.bfs_layout(G, 0, store_pos_as="pos")
>>> pprint(nx.get_node_attributes(G, "pos"))
{0: array([-1., 0.]),
1: array([-0.33333333, 0. ]),
2: array([0.33333333, 0. ]),
3: array([1., 0.])}
Notes
-----
This algorithm currently only works in two dimensions and does not
try to minimize edge crossings.
"""
G, center = _process_params(G, center, 2)
# Compute layers with BFS
layers = dict(enumerate(nx.bfs_layers(G, start)))
if len(G) != sum(len(nodes) for nodes in layers.values()):
raise nx.NetworkXError(
"bfs_layout didn't include all nodes. Perhaps use input graph:\n"
" G.subgraph(nx.node_connected_component(G, start))"
)
# Compute node positions with multipartite_layout
pos = multipartite_layout(
G, subset_key=layers, align=align, scale=scale, center=center
)
if store_pos_as is not None:
nx.set_node_attributes(G, pos, store_pos_as)
return pos
|