NetworkChart
Nodes joined by edges, placed by a layout. The Network Chart guide shows every feature on real data; this page is the contract: the function, the shape of its data, the keys style takes, and the constant each parameter accepts.
Function
datachart.charts.NetworkChart
NetworkChart(
data: (
NetworkSingleChartAttrs
| list[NetworkSingleChartAttrs]
),
*,
layout: NETWORK_LAYOUT | str | None = None,
directed: bool | None = None,
seed: int | None = None,
label_position: (
NETWORK_LABEL_POSITION | str | None
) = None,
show_values: bool | None = None,
value_format: VALUE_FORMAT | str | None = None,
show_legend: bool | None = None,
legend: LegendSettingAttrs | None = None,
title: str | None = None,
subtitle: str | list[str | None] | None = None,
emphasis: None = None,
emphasis_rule: EmphasisRuleAttrs | None = None,
figsize: FIG_SIZE | tuple[float, float] | None = None,
subplots: bool | None = None,
max_cols: int | None = None,
style: (
NetworkStyleAttrs
| list[NetworkStyleAttrs | None]
| None
) = None,
texts: (
TextSettingAttrs
| list[TextSettingAttrs]
| list[
TextSettingAttrs | list[TextSettingAttrs] | None
]
| None
) = None
) -> plt.Figure
Creates the network chart.
A network chart draws relational data as a node-link diagram — module
dependencies, who works with whom, co-occurring terms, flows between
peers. Nodes are placed by a layout and joined by edges; an edge's weight
sets its width, a node's size its marker area, its group its color. Use
it when the question is what is connected to what; for weighted flows
through ordered stages use SankeyChart.
Every edge is its own patch and the spring layout weighs every pair of nodes, so the chart is meant for networks that can be read, not for whole graphs. Without a problem: up to about 1,000 nodes and 3,000 edges under the spring layout (a few seconds), up to about 5,000 nodes and 15,000 edges under the circular or fixed layout (under a minute). Beyond that the spring layout grows with the square of the node count — 2,000 nodes take half a minute, 5,000 several minutes and gigabytes of memory — and every layout pays a few milliseconds per edge to draw and again to save. Aggregate or filter a larger graph first.
Examples:
>>> from datachart.charts import NetworkChart
>>> figure = NetworkChart(
... data={
... "edges": [
... {"source": "core", "target": "utils"},
... {"source": "cli", "target": "core"},
... {"source": "api", "target": "core"},
... {"source": "web", "target": "api"},
... ]
... },
... directed=True,
... title="Module dependencies",
... )
| PARAMETER | DESCRIPTION |
|---|---|
data
|
The chart data: a
TYPE:
|
layout
|
How the nodes are placed: a
TYPE:
|
directed
|
Whether the edges end in an arrowhead at the target. When
TYPE:
|
seed
|
The seed of the spring layouts (default 0); another seed gives another arrangement of the same data.
TYPE:
|
label_position
|
Where the node names print: a
TYPE:
|
show_values
|
Whether to write each edge's weight at its midpoint.
TYPE:
|
value_format
|
The format of the edge values: a
TYPE:
|
show_legend
|
Whether to list the node groups in a legend.
TYPE:
|
legend
|
The per-figure legend setting: title, location, column count
and alignment; each field falls back to the theme. See
TYPE:
|
title
|
The title of the chart.
TYPE:
|
subtitle
|
The subtitle(s) for individual charts.
TYPE:
|
emphasis
|
Not supported: emphasis is set per node through its
TYPE:
|
emphasis_rule
|
A rule that highlights the nodes matching it and mutes
the rest:
TYPE:
|
figsize
|
The size of the figure.
TYPE:
|
subplots
|
Whether to show each chart in its own subplot; several charts always split into subplots.
TYPE:
|
max_cols
|
Maximum number of columns in subplots.
TYPE:
|
style
|
Style configuration(s) for the chart(s). The edge geometry,
TYPE:
|
texts
|
Text annotation(s) to draw. Data coordinates are the 0–1 layout
space, so under
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
plt.Figure
|
The figure containing the network chart. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Data
data is one NetworkSingleChartAttrs, or a list of them for subplots, with NetworkNodeAttrs and NetworkEdgeAttrs inside.
datachart.typings.NetworkSingleChartAttrs
Bases: TypedDict
The single chart attributes for the network chart.
| ATTRIBUTE | DESCRIPTION |
|---|---|
nodes |
The nodes; inferred from the edges when omitted.
TYPE:
|
edges |
The edges.
TYPE:
|
subtitle |
The subtitle of the chart.
TYPE:
|
style |
The style of the chart.
TYPE:
|
texts |
The text annotations to be drawn.
TYPE:
|
datachart.typings.NetworkNodeAttrs
Bases: TypedDict
The node record attributes for the network chart.
| ATTRIBUTE | DESCRIPTION |
|---|---|
id |
The node identifier the edges refer to; unique within a chart.
TYPE:
|
label |
The drawn label; defaults to
TYPE:
|
size |
The node size, mapped by square root to marker area; must be greater than 0.
TYPE:
|
group |
The group the node is colored by.
TYPE:
|
emphasis |
The emphasis role of the node.
TYPE:
|
x |
The node's horizontal position in the 0–1 layout space;
TYPE:
|
y |
The node's vertical position in the 0–1 layout space;
TYPE:
|
datachart.typings.NetworkEdgeAttrs
Bases: TypedDict
The edge record attributes for the network chart.
| ATTRIBUTE | DESCRIPTION |
|---|---|
source |
The id of the node the edge leaves.
TYPE:
|
target |
The id of the node the edge enters.
TYPE:
|
weight |
The edge weight, mapped to its width and, under the weighted layouts, its pull; must be greater than 0.
TYPE:
|
Style
style takes the keys of NetworkStyleAttrs. The chart also reads the shared groups it draws: value labels (ValueLabelStyleAttrs) and text annotations (TextStyleAttrs). Every key falls back to the theme, so the same keys set the default look through config.
datachart.typings.NetworkStyleAttrs
Bases: TypedDict
The typing for the network chart style.
| ATTRIBUTE | DESCRIPTION |
|---|---|
plot_network_node_color |
The node marker color; overrides the color cycle.
TYPE:
|
plot_network_node_alpha |
The alpha value of the node markers.
TYPE:
|
plot_network_node_marker |
The node marker shape.
TYPE:
|
plot_network_node_size |
The marker area of a node without
TYPE:
|
plot_network_node_size_min |
The marker area of the smallest sized node.
TYPE:
|
plot_network_node_size_max |
The marker area of the largest sized node.
TYPE:
|
plot_network_node_edge_color |
The node stroke color.
TYPE:
|
plot_network_node_edge_width |
The node stroke width.
TYPE:
|
plot_network_edge_style |
The edge geometry:
TYPE:
|
plot_network_edge_curve |
The bow of a curved edge; the sign picks the side.
TYPE:
|
plot_network_edge_color |
The edge color.
TYPE:
|
plot_network_edge_alpha |
The edge alpha.
TYPE:
|
plot_network_edge_width_min |
The width of the lightest edge, and of an edge without
TYPE:
|
plot_network_edge_width_max |
The width of the heaviest edge.
TYPE:
|
plot_network_highlight_edge_width |
The stroke width of a highlighted node.
TYPE:
|
plot_network_label_halo_width |
The width of the halo, in the axes face color, behind labels; 0 disables it.
TYPE:
|
plot_network_group_alpha |
The alpha of the disc in the group color behind each cluster of the grouped layout; 0 disables it.
TYPE:
|
plot_network_group_linestyle |
Draws each cluster's mark as a ring in this line style and the edge color instead of a disc.
TYPE:
|
plot_network_edge_ink_stroke |
The pen the edges are drawn with, as for
TYPE:
|
Constants
The parameters that accept a constant, with the class in datachart.constants that lists its values.
| Parameter | Constant |
|---|---|
layout |
NETWORK_LAYOUT |
label_position |
NETWORK_LABEL_POSITION |
value_format |
VALUE_FORMAT |
legend={"location": ..., "alignment": ...} |
LEGEND_LOCATION, LEGEND_ALIGN |
figsize |
FIG_SIZE |