draw_networkx

draw_networkx(G, pos=None, arrows=None, with_labels=True, **kwds)[source]

Draw the graph G using Matplotlib.

Draw the graph with Matplotlib with options for node positions, labeling, titles, and many other drawing features. See draw() for simple drawing without labels or axes.

Parameters
Ggraph

A networkx graph

posdictionary, optional

A dictionary with nodes as keys and positions as values. If not specified a spring layout positioning will be computed. See networkx.drawing.layout for functions that compute node positions.

arrowsbool or None, optional (default=None)

If None, directed graphs draw arrowheads with FancyArrowPatch, while undirected graphs draw edges via LineCollection for speed. If True, draw arrowheads with FancyArrowPatches (bendable and stylish). If False, draw edges using LineCollection (linear and fast). For directed graphs, if True draw arrowheads. Note: Arrows will be the same color as edges.

arrowstylestr (default=’-|>’)

For directed graphs, choose the style of the arrowsheads. See matplotlib.patches.ArrowStyle for more options.

arrowsizeint or list (default=10)

For directed graphs, choose the size of the arrow head’s length and width. A list of values can be passed in to assign a different size for arrow head’s length and width. See matplotlib.patches.FancyArrowPatch for attribute mutation_scale for more info.

with_labelsbool (default=True)

Set to True to draw labels on the nodes.

axMatplotlib Axes object, optional

Draw the graph in the specified Matplotlib axes.

nodelistlist (default=list(G))

Draw only specified nodes

edgelistlist (default=list(G.edges()))

Draw only specified edges

node_sizescalar or array (default=300)

Size of nodes. If an array is specified it must be the same length as nodelist.

node_colorcolor or array of colors (default=’#1f78b4’)

Node color. Can be a single color or a sequence of colors with the same length as nodelist. Color can be string or rgb (or rgba) tuple of floats from 0-1. If numeric values are specified they will be mapped to colors using the cmap and vmin,vmax parameters. See matplotlib.scatter for more details.

node_shapestring (default=’o’)

The shape of the node. Specification is as matplotlib.scatter marker, one of ‘so^>v<dph8’.

alphafloat or None (default=None)

The node and edge transparency

cmapMatplotlib colormap, optional

Colormap for mapping intensities of nodes

vmin,vmaxfloat, optional

Minimum and maximum for node colormap scaling

linewidthsscalar or sequence (default=1.0)

Line width of symbol border

widthfloat or array of floats (default=1.0)

Line width of edges

edge_colorcolor or array of colors (default=’k’)

Edge color. Can be a single color or a sequence of colors with the same length as edgelist. Color can be string or rgb (or rgba) tuple of floats from 0-1. If numeric values are specified they will be mapped to colors using the edge_cmap and edge_vmin,edge_vmax parameters.

edge_cmapMatplotlib colormap, optional

Colormap for mapping intensities of edges

edge_vmin,edge_vmaxfloats, optional

Minimum and maximum for edge colormap scaling

stylestring (default=solid line)

Edge line style e.g.: ‘-’, ‘–’, ‘-.’, ‘:’ or words like ‘solid’ or ‘dashed’. (See matplotlib.patches.FancyArrowPatch: linestyle)

labelsdictionary (default=None)

Node labels in a dictionary of text labels keyed by node

font_sizeint (default=12 for nodes, 10 for edges)

Font size for text labels

font_colorstring (default=’k’ black)

Font color string

font_weightstring (default=’normal’)

Font weight

font_familystring (default=’sans-serif’)

Font family

labelstring, optional

Label for graph legend

kwdsoptional keywords

See networkx.draw_networkx_nodes(), networkx.draw_networkx_edges(), and networkx.draw_networkx_labels() for a description of optional keywords.

Notes

For directed graphs, arrows are drawn at the head end. Arrows can be turned off with keyword arrows=False.

Examples

>>> G = nx.dodecahedral_graph()
>>> nx.draw(G)
>>> nx.draw(G, pos=nx.spring_layout(G))  # use spring layout
>>> import matplotlib.pyplot as plt
>>> limits = plt.axis("off")  # turn off axis

Also see the NetworkX drawing examples at https://networkx.org/documentation/latest/auto_examples/index.html