display#
- display(G, canvas=None, **kwargs)[source]#
Draw the graph G.
Draw the graph as a collection of nodes connected by edges. The exact details of what the graph looks like are controled by the below attributes. All nodes and nodes at the end of visible edges must have a position set, but nearly all other node and edge attributes are options and nodes or edges missing the attribute will use the default listed below. A more complete discription of each parameter is given below this summary.
Default Visualization Attributes# Parameter
Default Attribute
Default Value
pos
"pos"
If there is not position, a layout will be calculated with
nx.spring_layout
.node_visible
"visible"
True
node_color
"color"
#1f78b4
node_size
"size"
300
node_label
"label"
Dict describing the node label. Defaults create a black text with the node name as the label. The dict respects these keys and defaults:
size : 12
color : black
family : sans serif
weight : normal
alpha : 1.0
h_align : center
v_align : center
bbox : Dict describing a
matplotlib.patches.FancyBboxPatch
. Default is None.
node_shape
"shape"
“o”
node_alpha
"alpha"
1.0
node_border_width
"border_width"
1.0
node_border_color
"border_color"
Matching node_color
edge_visible
"visible"
True
edge_width
"width"
1.0
edge_color
"color"
Black (#000000)
edge_label
"label"
Dict describing the edge label. Defaults create black text with a white bounding box. The dictionary respects these keys and defaults:
size : 12
color : black
family : sans serif
weight : normal
alpha : 1.0
bbox : Dict describing a
matplotlib.patches.FancyBboxPatch
. Default {“boxstyle”: “round”, “ec”: (1.0, 1.0, 1.0), “fc”: (1.0, 1.0, 1.0)}h_align : “center”
v_align : “center”
pos : 0.5
rotate : True
edge_style
"style"
“-”
edge_alpha
"alpha"
1.0
arrowstyle
"arrowstyle"
"-|>"
ifG
is directed else"-"
arrowsize
"arrowsize"
10 if
G
is directed else 0edge_curvature
"curvature"
arc3
edge_source_margin
"source_margin"
0
edge_target_margin
"target_margin"
0
- Parameters:
- Ggraph
A networkx graph
- canvasMatplotlib Axes object, optional
Draw the graph in specified Matplotlib axes
- posstring or function, default “pos”
A string naming the node attribute storing the position of nodes as a tuple. Or a function to be called with input
G
which returns the layout as a dict keyed by node to position tuple like the NetworkX layout functions. If no nodes in the graph has the attribute, a spring layout is calculated.- node_visiblestring or bool, default visible
A string naming the node attribute which stores if a node should be drawn. If
True
, all nodes will be visible while ifFalse
no nodes will be visible. If incomplete, nodes missing this attribute will be shown by default.- node_colorstring, default “color”
A string naming the node attribute which stores the color of each node. Visible nodes without this attribute will use ‘#1f78b4’ as a default.
- node_sizestring or number, default “size”
A string naming the node attribute which stores the size of each node. Visible nodes without this attribute will use a default size of 300.
- node_labelstring or bool, default “label”
A string naming the node attribute which stores the label of each node. The attribute value can be a string, False (no label for that node), True (the node is the label) or a dict keyed by node to the label.
If a dict is specified, these keys are read to further control the label:
label : The text of the label; default: name of the node
size : Font size of the label; default: 12
color : Font color of the label; default: black
family : Font family of the label; default: “sans-serif”
weight : Font weight of the label; default: “normal”
alpha : Alpha value of the label; default: 1.0
- h_alignThe horizontal alignment of the label.
one of “left”, “center”, “right”; default: “center”
- v_alignThe vertical alignment of the label.
one of “top”, “center”, “bottom”; default: “center”
bbox : A dict of parameters for
matplotlib.patches.FancyBboxPatch
.
Visible nodes without this attribute will be treated as if the value was True.
- node_shapestring, default “shape”
A string naming the node attribute which stores the label of each node. The values of this attribute are expected to be one of the matplotlib shapes, one of ‘so^>v<dph8’. Visible nodes without this attribute will use ‘o’.
- node_alphastring, default “alpha”
A string naming the node attribute which stores the alpha of each node. The values of this attribute are expected to be floats between 0.0 and 1.0. Visible nodes without this attribute will be treated as if the value was 1.0.
- node_border_widthstring, default “border_width”
A string naming the node attribute storing the width of the border of the node. The values of this attribute are expected to be numeric. Visible nodes without this attribute will use the assumed default of 1.0.
- node_border_colorstring, default “border_color”
A string naming the node attribute which storing the color of the border of the node. Visible nodes missing this attribute will use the final node_color value.
- edge_visiblestring or bool, default “visible”
A string nameing the edge attribute which stores if an edge should be drawn. If
True
, all edges will be drawn while ifFalse
no edges will be visible. If incomplete, edges missing this attribute will be shown by default. Values of this attribute are expected to be booleans.- edge_widthstring or int, default “width”
A string nameing the edge attribute which stores the width of each edge. Visible edges without this attribute will use a default width of 1.0.
- edge_colorstring or color, default “color”
A string nameing the edge attribute which stores of color of each edge. Visible edges without this attribute will be drawn black. Each color can be a string or rgb (or rgba) tuple of floats from 0.0 to 1.0.
- edge_labelstring, default “label”
A string naming the edge attribute which stores the label of each edge. The values of this attribute can be a string, number or False or None. In the latter two cases, no edge label is displayed.
If a dict is specified, these keys are read to further control the label:
label : The text of the label, or the name of an edge attribute holding the label.
size : Font size of the label; default: 12
color : Font color of the label; default: black
family : Font family of the label; default: “sans-serif”
weight : Font weight of the label; default: “normal”
alpha : Alpha value of the label; default: 1.0
- h_alignThe horizontal alignment of the label.
one of “left”, “center”, “right”; default: “center”
- v_alignThe vertical alignment of the label.
one of “top”, “center”, “bottom”; default: “center”
bbox : A dict of parameters for
matplotlib.patches.FancyBboxPatch
.rotate : Whether to rotate labels to lie parallel to the edge, default: True.
pos : A float showing how far along the edge to put the label; default: 0.5.
- edge_stylestring, default “style”
A string naming the edge attribute which stores the style of each edge. Visible edges without this attribute will be drawn solid. Values of this attribute can be line styles, e.g. ‘-’, ‘–’, ‘-.’ or ‘:’ or words like ‘solid’ or ‘dashed’. If no edge in the graph has this attribute and it is a non-default value, assume that it describes the edge style for all edges in the graph.
- edge_alphastring or float, default “alpha”
A string naming the edge attribute which stores the alpha value of each edge. Visible edges without this attribute will use an alpha value of 1.0.
- arrowstylestring, default “arrow”
A string naming the edge attribute which stores the type of arrowhead to use for each edge. Visible edges without this attribute use
"-"
for undirected graphs and"-|>"
for directed graphs.See
matplotlib.patches.ArrowStyle
for more options- arrowsizestring or int, default “arrow_size”
A string naming the edge attribute which stores the size of the arrowhead for each edge. Visible edges without this attribute will use a default value of 10.
- edge_curvaturestring, default “curvature”
A string naming the edge attribute storing the curvature and connection style of each edge. Visible edges without this attribute will use “arc3” as a default value, resulting an a straight line between the two nodes. Curvature can be given as ‘arc3,rad=0.2’ to specify both the style and radius of curvature.
Please see
matplotlib.patches.ConnectionStyle
andmatplotlib.patches.FancyArrowPatch
for more information.- edge_source_marginstring or int, default “source_margin”
A string naming the edge attribute which stores the minimum margin (gap) between the source node and the start of the edge. Visible edges without this attribute will use a default value of 0.
- edge_target_marginstring or int, default “target_margin”
A string naming the edge attribute which stores the minimumm margin (gap) between the target node and the end of the edge. Visible edges without this attribute will use a default value of 0.
- hide_ticksbool, default True
Weather to remove the ticks from the axes of the matplotlib object.
- Returns:
- The input graph. This is potentially useful for dispatching visualization
- functions.
- Raises:
- NetworkXError
If a node or edge is missing a required parameter such as
pos
or ifdisplay
receives an argument not listed above.- ValueError
If a node or edge has an invalid color format, i.e. not a color string, rgb tuple or rgba tuple.