Visualization (fa2.viz)¶
Render graph layouts to matplotlib figures or export to various formats.
Requires: pip install fa2[viz]
Quick Examples¶
from fa2.viz import plot_layout, export_layout
# Render to matplotlib
fig = plot_layout(G, positions, color_by_degree=True, title="My Graph")
# Export to D3.js-compatible JSON
export_layout(G, positions, fmt="json", path="graph.json")
# Export to PNG/SVG
export_layout(G, positions, fmt="png", path="graph.png")
# Export to Gephi format
export_layout(G, positions, fmt="gexf", path="graph.gexf")
Functions¶
plot_layout
¶
plot_layout(G, positions, node_color: Optional[Union[str, list, ndarray]] = None, node_size: Optional[Union[float, list, ndarray]] = None, edge_alpha: float = 0.15, node_alpha: float = 0.8, figsize: tuple = (10, 8), title: Optional[str] = None, color_by_degree: bool = False, cmap: str = 'viridis', show_labels: bool = False, ax=None)
Render a graph layout as a matplotlib figure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
numpy.ndarray, scipy.sparse matrix, networkx.Graph, or igraph.Graph
|
The graph. |
required |
positions
|
dict, list, numpy.ndarray, or igraph.Layout
|
Node positions from |
required |
node_color
|
str, list, or ndarray
|
Node colors. If None and |
None
|
node_size
|
float, list, or ndarray
|
Node sizes. If None, sizes are proportional to degree. |
None
|
edge_alpha
|
float
|
Edge transparency (0-1). Default 0.15. |
0.15
|
node_alpha
|
float
|
Node transparency (0-1). Default 0.8. |
0.8
|
figsize
|
tuple
|
Figure size in inches. Default (10, 8). |
(10, 8)
|
title
|
str
|
Figure title. |
None
|
color_by_degree
|
bool
|
Color nodes by degree using colormap. Default False. |
False
|
cmap
|
str
|
Matplotlib colormap name. Default "viridis". |
'viridis'
|
show_labels
|
bool
|
Show node labels. Default False. |
False
|
ax
|
Axes
|
Axes to draw on. If None, creates a new figure. |
None
|
Returns:
| Type | Description |
|---|---|
Figure
|
The matplotlib figure. |
Source code in fa2/viz.py
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 | |
export_layout
¶
export_layout(G, positions, fmt: str = 'json', path: Optional[str] = None, **plot_kwargs) -> Optional[bytes]
Export a graph layout to various formats.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
numpy.ndarray, scipy.sparse matrix, networkx.Graph, or igraph.Graph
|
The graph. |
required |
positions
|
dict, list, numpy.ndarray, or igraph.Layout
|
Node positions. |
required |
fmt
|
str
|
Output format: |
'json'
|
path
|
str
|
File path to write to. If None, returns bytes (for |
None
|
**plot_kwargs
|
Additional arguments passed to |
{}
|
Returns:
| Type | Description |
|---|---|
bytes, dict, or None
|
Content if |