spatial_graph_algorithms.plot
Visualisation helpers for SpatialGraph objects.
Plot gallery
| Function | Output | Use when |
|---|---|---|
plot_network |
2-D graph layout | Inspect a simulated graph; false edges shown in red |
plot_network_3d |
3-D graph layout | Graphs with dim=3 positions |
plot_edge_length_histogram |
Length distribution | Check connectivity density and false-edge length |
plot_comparison |
Side-by-side original vs reconstructed | Visually judge reconstruction quality |
plot_provenance_comparison |
Side-by-side with spatial pattern overlay | Visually diagnose reconstruction quality with a user-chosen color pattern |
render_simulation_visualization_bundle |
Saves all relevant plots | One-call output for a run |
API Reference
spatial_graph_algorithms.plot.plot_network(sn, *, figsize=(6.0, 4.5), node_size=None, edge_alpha=None, true_edge_color='#777777', false_edge_color='#d62728', false_edge_linewidth=None, true_edge_linewidth=None, false_edge_alpha=None, node_alpha=None, edge_display='auto', max_edges=None, edge_sample_seed=0, save=False, output_dir=DEFAULT_VISUALIZATION_DIR, filename='network.png')
Plot a 2-D spatial graph with optional false-edge highlighting.
True edges are drawn in grey; false (injected noise) edges are drawn as
solid red lines when edge_metadata["is_false"] is present. When
edge_display is "auto", dense graphs use adaptive styling and edge
sampling to preserve readability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sn
|
SpatialGraph
|
Graph to plot. Must have 2-D positions. |
required |
figsize
|
tuple of float
|
Figure size |
(6.0, 4.5)
|
node_size
|
float
|
Scatter marker size. If |
None
|
edge_alpha
|
float
|
Opacity of true edges. If |
None
|
true_edge_color
|
str
|
Hex colour for true edges. |
'#777777'
|
false_edge_color
|
str
|
Hex colour for false edges. |
'#d62728'
|
false_edge_linewidth
|
float
|
Line width for false edges. If |
None
|
true_edge_linewidth
|
float
|
Line width for true edges. If |
None
|
false_edge_alpha
|
float
|
Opacity of false edges. Default is high enough to keep false edges visible above true edges, with automatic reduction when many false edges are drawn. |
None
|
node_alpha
|
float
|
Opacity of nodes. If |
None
|
edge_display
|
('auto', 'all', 'sample', 'none')
|
Edge rendering mode. |
"auto"
|
max_edges
|
int
|
Maximum number of edges to draw for |
None
|
edge_sample_seed
|
int
|
Random seed used when sampling edges. Default 0. |
0
|
save
|
bool
|
If |
False
|
output_dir
|
str or Path
|
Directory for saved output. |
DEFAULT_VISUALIZATION_DIR
|
filename
|
str
|
Filename for saved output. |
'network.png'
|
Returns:
| Type | Description |
|---|---|
Figure
|
The rendered figure. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If positions is |
Source code in src/spatial_graph_algorithms/plot/network.py
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 | |
spatial_graph_algorithms.plot.plot_network_3d(sn, *, figsize=(6.0, 4.5), edge_display='auto', max_edges=None, edge_sample_seed=0, save=False, output_dir=DEFAULT_VISUALIZATION_DIR, filename='network_3d.png')
Plot a 3-D spatial graph using matplotlib's 3-D projection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sn
|
SpatialGraph
|
Graph to plot. Must have 3-D positions. |
required |
figsize
|
tuple of float
|
Figure size in inches. |
(6.0, 4.5)
|
edge_display
|
('auto', 'all', 'sample', 'none')
|
Edge rendering mode. Large graphs are sampled in |
"auto"
|
max_edges
|
int
|
Maximum number of edges to draw for |
None
|
edge_sample_seed
|
int
|
Random seed used when sampling edges. Default 0. |
0
|
save
|
bool
|
Save figure to output_dir / filename when |
False
|
output_dir
|
str or Path
|
Directory for saved output. |
DEFAULT_VISUALIZATION_DIR
|
filename
|
str
|
Filename for saved output. |
'network_3d.png'
|
Returns:
| Type | Description |
|---|---|
Figure
|
The rendered figure. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If positions is |
Source code in src/spatial_graph_algorithms/plot/network.py
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 | |
spatial_graph_algorithms.plot.plot_edge_length_histogram(sn, *, bins=30, figsize=(4.5, 3.0), density=False, with_all_pairs=False, max_edge_sample=50000, max_pair_sample=200000, seed=None, save=False, output_dir=DEFAULT_VISUALIZATION_DIR, filename='edge_length_histogram.png')
Plot the distribution of Euclidean edge lengths.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sn
|
SpatialGraph
|
Graph to analyse. Requires positions. |
required |
bins
|
int
|
Number of histogram bins. |
30
|
figsize
|
tuple of float
|
Figure size in inches. |
(4.5, 3.0)
|
density
|
bool
|
If |
False
|
with_all_pairs
|
bool
|
If |
False
|
max_edge_sample
|
int or None
|
Maximum number of edges to use when computing edge lengths. When the
graph has more edges than this, a random subset is drawn. |
50000
|
max_pair_sample
|
int
|
Maximum number of node-pair distances to sample when computing the
all-pairs reference. For a graph with n nodes there are
n(n-1)/2 pairs; above max_pair_sample a random subset is drawn.
Only used when with_all_pairs is |
200000
|
seed
|
int or None
|
Random seed for reproducible sampling. |
None
|
save
|
bool
|
Save figure to output_dir / filename when |
False
|
output_dir
|
str or Path
|
Directory for saved output. |
DEFAULT_VISUALIZATION_DIR
|
filename
|
str
|
Filename for saved output. |
'edge_length_histogram.png'
|
Returns:
| Type | Description |
|---|---|
Figure
|
The rendered figure. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If positions is |
Source code in src/spatial_graph_algorithms/plot/network.py
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 | |
spatial_graph_algorithms.plot.plot_comparison(sn, *, figsize=(12.0, 4.5), node_size=8.0, cmap='viridis', save=False, output_dir=DEFAULT_VISUALIZATION_DIR, filename='reconstruction_comparison.png')
Plot original and reconstructed positions side by side.
Nodes are coloured by their angle from the centroid in the original space, using the same colour map in both panels. Consistent colouring makes it easy to judge whether the spatial structure was recovered.
Procrustes alignment (scipy.spatial.procrustes — standardizes both arrays to
unit Frobenius norm, then finds optimal rotation + reflection) is applied
to the reconstructed positions before plotting so that orientation
differences do not obscure reconstruction quality.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sn
|
SpatialGraph
|
Graph with both positions and reconstructed_positions set. |
required |
figsize
|
tuple of float
|
Total figure size |
(12.0, 4.5)
|
node_size
|
float
|
Scatter marker size. |
8.0
|
cmap
|
str
|
Matplotlib colour map name for node colouring. |
'viridis'
|
save
|
bool
|
Save figure to output_dir / filename when |
False
|
output_dir
|
str or Path
|
Directory for saved output. |
DEFAULT_VISUALIZATION_DIR
|
filename
|
str
|
Filename for saved output. |
'reconstruction_comparison.png'
|
Returns:
| Type | Description |
|---|---|
Figure
|
Figure containing two subplots: original (left) and reconstructed (right). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If positions or reconstructed_positions is |
Source code in src/spatial_graph_algorithms/plot/network.py
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 | |
spatial_graph_algorithms.plot.plot_provenance_comparison(sn, *, pattern='grid', figsize=(12.0, 4.5), node_size=8.0, save=False, output_dir=DEFAULT_VISUALIZATION_DIR, filename='provenance_comparison.png', **pattern_kwargs)
Plot original and reconstructed positions side by side with a spatial color pattern.
A color pattern is derived from ground-truth positions and applied identically to both panels. If reconstruction is good, the pattern appears intact on the right; distortion or scrambling signals poor quality.
Procrustes alignment (scipy.spatial.procrustes — standardizes both arrays
to unit Frobenius norm, then finds optimal rotation + reflection) is applied to
the reconstructed positions before plotting so that orientation differences do
not obscure quality. The alignment is for display only and does not mutate
sn.reconstructed_positions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sn
|
SpatialGraph
|
Graph with both positions and reconstructed_positions set. Both must be exactly 2-D. |
required |
pattern
|
str
|
Pattern kind passed to :func: |
'grid'
|
figsize
|
tuple of float
|
Total figure size |
(12.0, 4.5)
|
node_size
|
float
|
Scatter marker size. Default 8.0. |
8.0
|
save
|
bool
|
If |
False
|
output_dir
|
str or Path
|
Directory for saved output. |
DEFAULT_VISUALIZATION_DIR
|
filename
|
str
|
Filename for saved output. |
'provenance_comparison.png'
|
**pattern_kwargs
|
Additional keyword arguments forwarded to
:func: |
{}
|
Returns:
| Type | Description |
|---|---|
Figure
|
Figure with two subplots: original (left) and reconstructed (right). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If positions or reconstructed_positions is |
Examples:
>>> from spatial_graph_algorithms.simulate import generate
>>> from spatial_graph_algorithms.reconstruct import reconstruct
>>> sn = generate(n=200, seed=0)
>>> sn_rec = reconstruct(sn, method="mds", seed=0)
>>> fig = plot_provenance_comparison(sn_rec, pattern="grid", grid_size=(4, 4))
Source code in src/spatial_graph_algorithms/plot/network.py
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 | |
spatial_graph_algorithms.plot.plot_denoising_evaluation(sg, scores, *, method, figsize=(15.0, 5.0), output_path=None)
Three-panel evaluation plot for a denoising scoring run.
Panels
- Score vs edge length scatter. Points coloured red for false edges
and blue for true edges when
is_falselabels are present; grey otherwise. Pearson and Spearman correlations are annotated. - ROC curve with AUC (requires
is_falselabels). - Precision-Recall curve with AUC and optimal-F1 threshold marker
(requires
is_falselabels).
Panels 2 and 3 degrade gracefully when ground-truth is unavailable, showing an explanatory message instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sg
|
SpatialGraph
|
Graph used for scoring. Must have |
required |
scores
|
dict[tuple[int, int], float]
|
Edge scores from :class: |
required |
method
|
str
|
Scoring method that produced scores. Score polarity is looked up
in :data: |
required |
figsize
|
tuple of float
|
Figure dimensions |
(15.0, 5.0)
|
output_path
|
str or Path
|
If given, saves the figure at 300 DPI with tight layout. |
None
|
Returns:
| Type | Description |
|---|---|
Figure
|
The rendered figure. |
Examples:
>>> from spatial_graph_algorithms.simulate import generate
>>> from spatial_graph_algorithms.denoise import score_edges
>>> from spatial_graph_algorithms.plot.denoise import plot_denoising_evaluation
>>> sg = generate(n=200, false_edges_fraction=0.10, seed=42)
>>> scores = score_edges(sg, method="jaccard")
>>> fig = plot_denoising_evaluation(sg, scores, method="jaccard")
Source code in src/spatial_graph_algorithms/plot/denoise.py
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 | |
spatial_graph_algorithms.plot.render_simulation_visualization_bundle(sn, *, output_dir=DEFAULT_VISUALIZATION_DIR, prefix='simulation')
Save the standard visualisation bundle (network + edge-length histogram) to disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sn
|
SpatialGraph
|
Graph to visualise. |
required |
output_dir
|
str or Path
|
Directory where plots are written. |
DEFAULT_VISUALIZATION_DIR
|
prefix
|
str
|
Filename prefix for all saved plots. |
'simulation'
|
Returns:
| Type | Description |
|---|---|
dict
|
Mapping of |