altair_nx package

Submodules

altair_nx.core module

altair_nx.core.to_chart(G, pos)

Construct a single Altair Chart for the given Graph and node positions.

Parameters:
  • G (Graph) – The graph to draw.

  • pos (dict[..., tuple[float, float]]) – The node positions of G, as produced by any of the nx.*_layout functions, e.g. nx.kamada_kawai_layout. Note that most layouts use random seeds; for reproducible results set np.random.seed(…) before they are called.

Returns:

An Altair chart with layers for G’s edges and nodes.

altair_nx.core.to_pandas_edge_arrows(G, pos, length, length_is_relative=False, control_points=None)

Convert Graph edge arrows to pandas DataFrame meant for drawing with Altair.

Note that arrows are not drawn for self-loops since they would convey no extra information (and also to avoid unnecessary clutter).

Parameters:
  • G (Graph) – The graph to draw.

  • pos (dict[..., tuple[float, float]]) – The node positions of G, as produced by any of the nx.*_layout functions, e.g. nx.kamada_kawai_layout. Note that most layouts use random seeds; for reproducible results set np.random.seed(…) before they are called.

  • length (float) – A relative (i.e. proportion of edge length) or absolute measure of arrow length (the interpretation is determined by length_is_relative).

  • length_is_relative – Whether arrow_length should be interpreted as a proportion of its edge length instead of an absolute measure.

  • control_points (list[tuple[float, float]]) – Points to insert in the dataframe between the source and target point rows of each edge; they should be expressed as a tuple of coordinates relative to their straight edge: (proportion of edge length parallel to the edge, proportion of edge length perpendicular (anticlockwise) to the edge). E.g. [(.5, .1)] is a single control point halfway along the edge and .1 of its length to the left of it.

Returns:

A pandas DataFrame of edge arrows.

altair_nx.core.to_pandas_edges(G, pos, control_points=None, loop_radius=0.05, loop_angle=90.0, loop_n_points=30)

Convert Graph edges to pandas DataFrame meant for drawing with Altair.

Parameters:
  • G (Graph) – The graph to draw.

  • pos (dict[..., tuple[float, float]]) – The node positions of G, as produced by any of the nx.*_layout functions, e.g. nx.kamada_kawai_layout. Note that most layouts use random seeds; for reproducible results set np.random.seed(…) before they are called.

  • control_points (list[tuple[float, float]]) – Points to insert in the dataframe between the source and target point rows of each edge; they should be expressed as a tuple of coordinates relative to their straight edge: (proportion of edge length parallel to the edge, proportion of edge length perpendicular (anticlockwise) to the edge). E.g. [(.5, .1)] is a single control point halfway along the edge and .1 of its length to the left of it.

  • loop_radius – The radius of self-loop edges.

  • loop_angle – The direction (in degrees) in which self-loops are drawn from the node; above it by default.

  • loop_n_points – Number of points (INCLUDING the node itself) to draw self-loop edges. The default value is high in order to approximate a circle in both straight (i.e. control_points is None) and curved edge drawing cases, HOWEVER, low values such as 2, 3, and 4 produce nice shapes pointed at the node in both cases (in the straight-edge case, respectively: a short segment, a triangle, and a square). NOTE: to draw straight edges but interpolation-curved loops (rather than by high manual point count), set control_points to [] rather than None (or really any list of points whose 2nd coordinate is 0).

Returns:

A pandas DataFrame of edges.

altair_nx.core.to_pandas_nodes(G, pos)

Convert Graph nodes to pandas DataFrame meant for drawing with Altair.

Parameters:
  • G (Graph) – The graph to draw.

  • pos (dict[..., tuple[float, float]]) – The node positions of G, as produced by any of the nx.*_layout functions, e.g. nx.kamada_kawai_layout. Note that most layouts use random seeds; for reproducible results set np.random.seed(…) before they are called.

Returns:

A pandas DataFrame of nodes.

altair_nx.draw_altair module

altair_nx.draw_altair.draw_networkx(G=None, pos=None, chart=None, node_subset=None, edge_subset=None, show_orphans=True, show_self_loops=True, node_size=400, node_shape='circle', node_colour='teal', node_cmap=None, node_alpha=1.0, node_outline_width=1.0, node_outline_dash_and_gap_lengths=None, node_outline_colour=None, node_mark_kwargs=None, node_encode_kwargs=None, node_label=None, node_font_size=15, node_font_colour='black', node_tooltip=None, node_legend=False, node_label_mark_kwargs=None, node_label_encode_kwargs=None, edge_width=1, edge_dash_and_gap_lengths=None, edge_colour='grey', edge_cmap=None, edge_alpha=1.0, edge_tooltip=None, edge_legend=False, loop_radius=0.05, loop_angle=90.0, loop_n_points=30, curved_edges=False, edge_control_points=None, edge_interpolation='basis', edge_mark_kwargs=None, edge_encode_kwargs=None, arrow_width=2, arrow_length=0.1, arrow_length_is_relative=True, arrow_colour='black', arrow_cmap=None, arrow_alpha=1.0, arrow_legend=False, arrow_mark_kwargs=None, arrow_encode_kwargs=None, chart_width=500.0, chart_height=300.0, chart_padding=0.05)

Draw the graph G using Altair, with control over node, edge and arrow features, including filtering and curved edges.

Note that for arguments which accept node or edge attributes as alternatives to fixed values there are additional options generated in the drawing process: for nodes these are their name (‘node’) and position coordinates (‘x’, ‘y’), while for edges they are their name (‘edge’), order of nodes (‘order’), node pair names individually and together (‘source’, ‘target’, ‘pair’), and node coordinates (‘x’, ‘y’).

Extra customisation can be performed through Altair chart properties, e.g. .properties(title = ‘…’) to set properties, or .configure_view(stroke = None) to remove chart borders or set other configuration settings.

Also note that there is a pair of *_kwargs arguments for each layer to allow customisability without restrictions or safeguards by directly injecting fields into the Altair .mark_* and .encode calls. They are mostly meant for deeper levels of interactivity, e.g. adding value toggles by passing alt.param`s with fixed options bound with `alt.binding_select, (then requiring .add_params(…) on the final chart), but standard fields can also be passed in to override arguments otherwise processed by draw_networkx, e.g. to set conditional values with alt.condition.

This function ensures that graph and chart aspect ratios always match by adapting one to the other or vice versa depending on whether one or none of chart_width and chart_height is None. For example, if all node coordinates (in pos) were along a thin rectangle, a square chart would not be ideal to draw them; this function will ensure that both are the same shape. Scaling issue context: although axes are not shown, any mismatch of chart and graph aspect ratios would result in unequal x and y axes’ units; it is therefore best to avoid setting either or both sizes on the output chart (with .properties(width = …, height = …)) instead of using chart_width and chart_height, since this would deform coordinate-dependent shapes to the new aspect ratio (e.g. a (0,0)-(1,1) square would be drawn as a rectangle if width != height, while, say, node shapes themselves would be unaffected).

Parameters:
  • G (Graph) – The graph to draw.

  • pos (dict[..., tuple[float, float]]) – The node positions of G, as produced by any of the nx.*_layout functions, e.g. nx.kamada_kawai_layout, which is the default if pos is None (called with no edge attribute to use as weights, hence possibly missing out on more meaningful non-default positions). Note that most layouts use random seeds; for reproducible results set np.random.seed(…) before they are called.

  • chart (Chart) – A pre-existing chart to draw over.

  • node_subset (list) – Subset of nodes to draw.

  • edge_subset (list) – Subset of edges to draw.

  • show_orphans – Whether to draw nodes with no edges.

  • show_self_loops – Whether to draw edges starting and ending on the same node; nodes with only self-loops will still be drawn (though edge-less) unless show_orphans is also False.

  • node_size (int | str) – Either an int or a node attribute containing ints.

  • node_shape – Either an Altair point-mark shape specifier or a node attribute containing the same. Note that this also includes SVG path strings; see https://altair-viz.github.io/user_guide/marks/point.html.

  • node_colour – A colour string, None, or a node attribute containing colour strings if cmap is None and floats if not None. Setting it to None (or any non-colour and non-column string) produces nodes with outlines and no fill, but this makes summoning tooltips on hover difficult.

  • node_cmap (str) – Colourmap for mapping intensities of nodes (requires node_colour to be a node attribute containing floats).

  • node_alpha – Node opacity.

  • node_outline_width (float | str) – Either a single float for all nodes or a node attribute containing floats.

  • node_outline_dash_and_gap_lengths (tuple[float, float] | str) – None for a continuous outline, or a pair of numbers representing the lengths of dashes and gaps between dashes, or a node attribute containing the same or containing strings.

  • node_outline_colour (str) – A colour string, None, or a node attribute containing colour strings if cmap is None and floats if not None. Setting it to None makes it match the fill colour.

  • node_mark_kwargs (dict[str, ...]) – Custom fields to inject into the node layer’s .mark_point call without restrictions or safeguards; will overwrite existing fields on overlaps.

  • node_encode_kwargs (dict[str, ...]) – Custom fields to inject into the node layer’s .encode call without restrictions or safeguards; will overwrite existing fields on overlaps.

  • node_label (str) – Either a string to use as identical label for all nodes or a node attribute containing strings. Note that the auto-generated ‘node’ attribute contains node names.

  • node_font_size – Either an int or a node attribute containing ints.

  • node_font_colour – Either a colour string or a node attribute containing colour strings.

  • node_tooltip (list[str]) – Node attributes to show on hover.

  • node_legend – Whether to show a legend for attribute-controlled node features.

  • node_label_mark_kwargs (dict[str, ...]) – Custom fields to inject into the label layer’s .mark_text call without restrictions or safeguards; will overwrite existing fields on overlaps.

  • node_label_encode_kwargs (dict[str, ...]) – Custom fields to inject into the label layer’s .encode call without restrictions or safeguards; will overwrite existing fields on overlaps.

  • edge_width – Either an int or an edge attribute containing ints.

  • edge_dash_and_gap_lengths (tuple[float, float] | str) – None for a continuous line, or a pair of numbers representing the lengths of dashes and gaps between dashes, or an edge attribute containing the same or containing strings.

  • edge_colour – Either a colour string or an edge attribute containing colour strings if edge_cmap is None and floats if not None.

  • edge_cmap (str) – Colourmap for mapping intensities of edges (requires edge_colour to be an edge attribute containing floats).

  • edge_alpha – Edge opacity.

  • edge_tooltip (list[str]) – Edge attributes to show on hover (applies to arrows as well). By default, the tooltip is only* triggered on an invisible point at the edge midpoint (or slightly off of it if the edge is curved); providing any edge_control_points overrides this default. To set these hoverable points’ properties, pass them under the key ‘point’ through edge_mark_kwargs; the default is equivalent to edge_mark_kwargs = dict(point = alt.OverlayMarkDef(opacity = 0, size = 400)). (*: the tooltip is also shown at the base of arrows (with invisible point size of 100, overridable as above) if G is directed and arrow tooltips are not overridden through arrow_encode_kwargs).

  • edge_legend – Whether to show a legend for attribute-controlled edge features.

  • loop_radius – The radius of self-loop edges. Note that using too small a value may result in the nodes covering self-loops completely (interactive zoom may also cause this). Also note that changing the chart’s aspect ratio outside of this function (with .properties(width = …, height = …)) will NOT rescale coordinates and will result in unequal x and y axes’ units, deforming the loops to match the new aspect ratio; use the chart_width and chart_height arguments instead.

  • loop_angle – The direction (in degrees) in which self-loops are drawn from the node; above it by default.

  • loop_n_points – Number of points (INCLUDING the node itself) to draw self-loop edges. The default value is high in order to approximate a circle in both straight and curved edge drawing cases, HOWEVER, low values such as 2, 3, and 4 produce nice shapes pointed at the node in both cases (in the straight-edge case, respectively: a short segment, a triangle, and a square). NOTE: to draw straight edges but interpolation-curved loops (rather than by high manual point count), set control_points to [] rather than None (or really any list of points whose 2nd coordinate is 0).

  • curved_edges – Whether edges should be curved (using control_points and interpolate arguments).

  • edge_control_points (list[tuple[float, float]]) – The intermediate points to place along edges, which, depending on curved_edges, are either used for interpolation or are connected with straight lines; they should be expressed as a tuple of coordinates relative to their straight edge: (proportion of edge length parallel to the edge, proportion of edge length perpendicular (anticlockwise) to the edge). E.g. the default value is [(.5, .1)] if curved_edges is True and [(.5, 0.)] if False, i.e. is a single control point halfway along the edge, either .1 of its length to the left of it or on it.

  • edge_interpolation

    Interpolation method for curved edges (which are built on control points provided in edge-relative coordinates in control_points). The default interpolation is a cubic spline (i.e. ‘basis’). Interactive examples of possible values: https://altair-viz.github.io/user_guide/marks/line.html Corresponding descriptions: https://d3js.org/d3-shape/curve Recommendation:

    • ’basis’, ‘catmull-rom’ and ‘bundle’ (and of course ‘linear’ and ‘monotone’) are best since they do not overshoot if control points are close to endpoints

    • the open and closed interpolation varieties are not appropriate for edges

    • the ‘natural’ cubic and ‘cardinal’ interpolations tend to overshoot for control points close to endpoints

  • edge_mark_kwargs (dict[str, ...]) – Custom fields to inject into the edge layer’s .mark_line call without restrictions or safeguards; will overwrite existing fields on overlaps.

  • edge_encode_kwargs (dict[str, ...]) – Custom fields to inject into the edge layer’s .encode call without restrictions or safeguards; will overwrite existing fields on overlaps.

  • arrow_width – Either an int or an edge attribute containing ints.

  • arrow_length – The proportion of the line to be occupied by the arrow.

  • arrow_length_is_relative – Whether arrow_length should be interpreted as a proportion of its edge length instead of an absolute measure.

  • arrow_colour – Either a colour string or an edge attribute containing colour strings if arrow_cmap is None and floats if not None.

  • arrow_cmap (str) – Colourmap for mapping intensities of arrows (requires arrow_colour to be an edge attribute containing floats).

  • arrow_alpha – Arrow opacity.

  • arrow_legend – Whether to show a legend for attribute-controlled arrow features.

  • arrow_mark_kwargs (dict[str, ...]) – Custom fields to inject into the arrow layer’s .mark_line call without restrictions or safeguards; will overwrite existing fields on overlaps.

  • arrow_encode_kwargs (dict[str, ...]) – Custom fields to inject into the arrow layer’s .encode call without restrictions or safeguards; will overwrite existing fields on overlaps.

  • chart_width (float | None) – Width to set the chart to (while maintaining equality between the drawn axes’ units). Works in combination with chart_height: either size is allowed to be None (though not both), letting the graph’s own aspect ratio determine the other; if both are not None, then the graph’s aspect ratio is reshaped to the given one.

  • chart_height (float | None) – Height to set the chart to (while maintaining equality between the drawn axes’ units). Works in combination with chart_width: either size is allowed to be None (though not both), letting the graph’s own aspect ratio determine the other; if both are not None, then the graph’s aspect ratio is reshaped to the given one.

  • chart_padding – Proportion of the larger of the two axes to use as padding between graph and chart borders (followed by a slight padding increase due to final aspect ratio adjustments).

Returns:

An Altair chart of the given graph; its possible layers (.layer) are [edges, arrows, nodes, labels], in this order, but arrows are present only if G is directed and labels only if node_label is not None. Note: the node coordinates will be different from those in the input pos, as scaling takes place to ensure that graph and chart aspect ratios match (so that the x and y axes’ units are equal); in particular, the shorter axis will have length of approximately 1 + 2 * chart_padding (this is to ensure that the size of self-loops for a given loop_radius value is consistent relative to chart size). The new coordinates can be extracted from the dataframe of the node layer: output.layer[index_of_node_layer].data. The chart size and axes’ domains can be copied to a new chart with the copy_size_and_axes function (exported by default), or can be extracted manually with, e.g. output_chart.width and output_chart.encoding.x[‘scale’][‘domain’].

altair_nx.draw_altair.draw_networkx_arrows(G=None, pos=None, chart=None, layer=None, subset=None, width=1, dash_and_gap_lengths=None, length=0.1, length_is_relative=True, colour='grey', cmap=None, alpha=1.0, tooltip=None, legend=False, curved_edges=False, control_points=None, mark_kwargs=None, encode_kwargs=None)

Draw the edges of graph G using Altair, with control over various features, including filtering and curve.

Note that for arguments which accept edge attributes as alternatives to fixed values there are additional options generated in the drawing process: their name (‘edge’), order of nodes (‘order’), node pair names individually and together (‘source’, ‘target’, ‘pair’), and node coordinates (‘x’, ‘y’).

Parameters:
  • G (Graph) – The graph to draw.

  • pos (dict[..., tuple[float, float]]) – The node positions of G, as produced by any of the nx.*_layout functions, e.g. nx.kamada_kawai_layout, which is the default if pos is None (called with no edge attribute to use as weights, hence possibly missing out on more meaningful non-default positions). Note that most layouts use random seeds; for reproducible results set np.random.seed(…) before they are called.

  • chart (Chart) – A pre-existing chart to draw over.

  • layer (Chart) – A pre-existing chart layer to draw in.

  • subset (list) – Subset of edges for which to draw arrows.

  • width – Either an int or an edge attribute containing ints.

  • dash_and_gap_lengths (tuple[float, float] | str) – None for a continuous line, or a pair of numbers representing the lengths of dashes and gaps between dashes, or an edge attribute containing the same or containing strings.

  • length – A relative (i.e. proportion of edge length) or absolute measure of arrow length (the interpretation is determined by length_is_relative).

  • length_is_relative – Whether arrow_length should be interpreted as a proportion of its edge length instead of an absolute measure.

  • colour – Either a colour string or an edge attribute containing colour strings if cmap is None and floats if not None.

  • cmap (str) – Colourmap for mapping intensities of arrows (requires colour to be an edge attribute containing floats).

  • alpha – Edge opacity.

  • tooltip (list[str]) – Edge attributes to show on hover for arrows. (NOTE that draw_networkx passes edge_tooltip to this argument; to set different tooltip attributes from edges, pass them under the key ‘tooltip’ through arrow_encode_kwargs). The tooltip is only triggered on an invisible point at the base of the arrow; to set this hoverable point’s properties, pass them under the key ‘point’ through mark_kwargs; the default is equivalent to mark_kwargs = dict(point = alt.OverlayMarkDef(opacity = 0, size = 100)).

  • legend – Whether to show a legend for attribute-controlled arrow features.

  • curved_edges – Whether the edges for which arrows are to be drawn are curved.

  • control_points (list[tuple[float, float]]) – The intermediate points placed along edges, based on which (along with curved_edges) arrows are to be drawn.

  • mark_kwargs (dict[str, ...]) – Custom fields to inject into the .mark_line call without restrictions or safeguards; will overwrite existing fields on overlaps.

  • encode_kwargs (dict[str, ...]) – Custom fields to inject into the .encode call without restrictions or safeguards; will overwrite existing fields on overlaps.

Returns:

An Altair chart of the edges of given graph.

altair_nx.draw_altair.draw_networkx_edges(G=None, pos=None, chart=None, layer=None, subset=None, width=1, dash_and_gap_lengths=None, colour='grey', cmap=None, alpha=1.0, tooltip=None, legend=False, loop_radius=0.05, loop_angle=90.0, loop_n_points=30, curved_edges=False, control_points=None, interpolation='basis', mark_kwargs=None, encode_kwargs=None)

Draw the edges of graph G using Altair, with control over various features, including filtering and curve.

Note that for arguments which accept edge attributes as alternatives to fixed values there are additional options generated in the drawing process: their name (‘edge’), order of nodes (‘order’), node pair names individually and together (‘source’, ‘target’, ‘pair’), and node coordinates (‘x’, ‘y’).

Parameters:
  • G (Graph) – The graph to draw.

  • pos (dict[..., tuple[float, float]]) – The node positions of G, as produced by any of the nx.*_layout functions, e.g. nx.kamada_kawai_layout, which is the default if pos is None (called with no edge attribute to use as weights, hence possibly missing out on more meaningful non-default positions). Note that most layouts use random seeds; for reproducible results set np.random.seed(…) before they are called.

  • chart (Chart) – A pre-existing chart to draw over.

  • layer (Chart) – A pre-existing chart layer to draw in.

  • subset (list) – Subset of edges to draw.

  • width – Either an int or an edge attribute containing ints.

  • dash_and_gap_lengths (tuple[float, float] | str) – None for a continuous line, or a pair of numbers representing the lengths of dashes and gaps between dashes, or an edge attribute containing the same or containing strings.

  • colour – Either a colour string or an edge attribute containing colour strings if cmap is None and floats if not None.

  • cmap (str) – Colourmap for mapping intensities of edges (requires colour to be an edge attribute containing floats).

  • alpha – Edge opacity.

  • tooltip (list[str]) – Edge attributes to show on hover. By default, the tooltip is only* triggered on an invisible point at the edge midpoint (or slightly off of it if the edge is curved); providing any control_points overrides this default. To set these hoverable points’ properties, pass them under the key ‘point’ through edge_mark_kwargs; the default is equivalent to mark_kwargs = dict(point = alt.OverlayMarkDef(opacity = 0, size = 400)). (*: the tooltip is also shown at the base of arrows (with invisible point size of 100, overridable as above) if G is directed and arrow tooltips are not overridden through arrow_encode_kwargs).

  • legend – Whether to show a legend for attribute-controlled edge features.

  • loop_radius – The radius of self-loop edges. Note that using too small a value may result in the nodes covering self-loops completely (interactive zoom may also cause this). Also note that changing the chart’s aspect ratio outside of this function (with .properties(width = …, height = …)) will NOT rescale coordinates and will result in unequal x and y axes’ units, deforming the loops to match the new aspect ratio; use the chart_width and chart_height arguments instead.

  • loop_angle – The direction (in degrees) in which self-loops are drawn from the node; above it by default.

  • loop_n_points – Number of points (INCLUDING the node itself) to draw self-loop edges. The default value is high in order to approximate a circle in both straight and curved edge drawing cases, HOWEVER, low values such as 2, 3, and 4 produce nice shapes pointed at the node in both cases (in the straight-edge case, respectively: a short segment, a triangle, and a square). NOTE: to draw straight edges but interpolation-curved loops (rather than by high manual point count), set control_points to [] rather than None (or really any list of points whose 2nd coordinate is 0).

  • curved_edges – Whether edges should be curved (using control_points and interpolate arguments).

  • control_points (list[tuple[float, float]]) – The intermediate points to place along edges, which, depending on curved_edges, are either used for interpolation or are connected with straight lines; they should be expressed as a tuple of coordinates relative to their straight edge: (proportion of edge length parallel to the edge, proportion of edge length perpendicular (anticlockwise) to the edge). E.g. the default value is [(.5, .1)] if curved_edges is True and [(.5, 0.)] if False, i.e. is a single control point halfway along the edge, either .1 of its length to the left of it or on it.

  • interpolation

    Interpolation method for curved edges (which are built on control points provided in edge-relative coordinates in control_points). The default interpolation is a cubic spline (i.e. ‘basis’). Interactive examples of possible values: https://altair-viz.github.io/user_guide/marks/line.html Corresponding descriptions: https://d3js.org/d3-shape/curve Recommendation:

    • ’basis’, ‘catmull-rom’ and ‘bundle’ (and of course ‘linear’ and ‘monotone’) are best since they do not overshoot if control points are close to endpoints

    • the open and closed interpolation varieties are not appropriate for edges

    • the ‘natural’ cubic and ‘cardinal’ interpolations tend to overshoot for control points close to endpoints

  • mark_kwargs (dict[str, ...]) – Custom fields to inject into the .mark_line call without restrictions or safeguards; will overwrite existing fields on overlaps.

  • encode_kwargs (dict[str, ...]) – Custom fields to inject into the .encode call without restrictions or safeguards; will overwrite existing fields on overlaps.

Returns:

An Altair chart of the edges of given graph.

altair_nx.draw_altair.draw_networkx_labels(G=None, pos=None, chart=None, layer=None, subset=None, label=None, font_size=15, font_colour='black', mark_kwargs=None, encode_kwargs=None)

Draw the node labels of graph G using Altair, with control over various features, including node filtering, and font.

Note that for arguments which accept node attributes as alternatives to fixed values there are additional options generated in the drawing process: their name (‘node’) and position coordinates (‘x’, ‘y’).

Parameters:
  • G (Graph) – The graph to draw.

  • pos (dict[..., tuple[float, float]]) – The node positions of G, as produced by any of the nx.*_layout functions, e.g. nx.kamada_kawai_layout, which is the default if pos is None (called with no edge attribute to use as weights, hence possibly missing out on more meaningful non-default positions). Note that most layouts use random seeds; for reproducible results set np.random.seed(…) before they are called.

  • chart (Chart) – A pre-existing chart to draw over.

  • layer (Chart) – A pre-existing chart layer to draw in.

  • subset (list) – Subset of nodes to draw.

  • label (str) – Either a string to use as identical label for all nodes or a node attribute containing strings. Note that the auto-generated ‘node’ attribute contains node names.

  • font_size – Either an int or a node attribute containing ints.

  • font_colour – Either a colour string or a node attribute containing colour strings.

  • mark_kwargs (dict[str, ...]) – Custom fields to inject into the .mark_text call without restrictions or safeguards; will overwrite existing fields on overlaps.

  • encode_kwargs (dict[str, ...]) – Custom fields to inject into the .encode call without restrictions or safeguards; will overwrite existing fields on overlaps.

Returns:

An Altair chart of the node labels of the given graph.

altair_nx.draw_altair.draw_networkx_nodes(G=None, pos=None, chart=None, layer=None, subset=None, size=400, shape='circle', colour='teal', cmap=None, alpha=1.0, outline_width=1.0, outline_dash_and_gap_lengths=None, outline_colour=None, tooltip=None, legend=False, mark_kwargs=None, encode_kwargs=None)

Draw the nodes of graph G using Altair, with control over various features, including filtering, and sizes.

Note that for arguments which accept node attributes as alternatives to fixed values there are additional options generated in the drawing process: their name (‘node’) and position coordinates (‘x’, ‘y’).

Parameters:
  • G (Graph) – The graph to draw.

  • pos (dict[..., tuple[float, float]]) – The node positions of G, as produced by any of the nx.*_layout functions, e.g. nx.kamada_kawai_layout, which is the default if pos is None (called with no edge attribute to use as weights, hence possibly missing out on more meaningful non-default positions). Note that most layouts use random seeds; for reproducible results set np.random.seed(…) before they are called.

  • chart (Chart) – A pre-existing chart to draw over.

  • layer (Chart) – A pre-existing chart layer to draw in.

  • subset (list) – Subset of nodes to draw.

  • size (int | str) – Either an int or a node attribute containing ints.

  • shape – Either an Altair point-mark shape specifier or a node attribute containing the same. Note that this also includes SVG path strings; see https://altair-viz.github.io/user_guide/marks/point.html.

  • colour – A colour string, None, or a node attribute containing colour strings if cmap is None and floats if not None. Setting it to None (or any non-colour and non-column string) produces nodes with outlines and no fill, but this makes summoning tooltips on hover difficult.

  • cmap (str) – Colourmap for mapping intensities of nodes (requires node_colour to be a node attribute containing floats).

  • alpha – Node opacity.

  • outline_width (float | str) – Either a single float for all nodes or a node attribute containing floats.

  • outline_dash_and_gap_lengths (tuple[float, float] | str) – None for a continuous outline, or a pair of numbers representing the lengths of dashes and gaps between dashes, or a node attribute containing the same or containing strings.

  • outline_colour (str) – A colour string, None, or a node attribute containing colour strings if cmap is None and floats if not None. Setting it to None makes it match the fill colour.

  • tooltip (list[str]) – Node attributes to show on hover.

  • legend – Whether to show a legend for attribute-controlled node features.

  • mark_kwargs (dict[str, ...]) – Custom fields to inject into the .mark_point call without restrictions or safeguards; will overwrite existing fields on overlaps.

  • encode_kwargs (dict[str, ...]) – Custom fields to inject into the .encode call without restrictions or safeguards; will overwrite existing fields on overlaps.

Returns:

An Altair chart of the nodes of the given graph.

altair_nx.util module

altair_nx.util.copy_size_and_axes(source_chart, target_chart)

Set the height & width, as well as the x and y axes’ ranges of the target_chart to the same values as the source_chart (ASSUMING they are all defined, as is the case for outputs of draw_networkx). This is useful to preserve the drawn aspect ratio when reassembling or concatenating layers with other charts.

Parameters:
  • source_chart (Chart)

  • target_chart (Chart)

altair_nx.util.despine(chart)

Despine an altair chart (i.e. remove ticks, grid, domain and labels).

Parameters:

chart (Chart)

altair_nx.util.is_arraylike(obj)

Return True if array-like (accepts lists, numpy.ndarray, pandas.Series, pandas.DataFrame).