Nx draw python параметры

draw#

Draw the graph as a simple representation with no node labels or edge labels and using the full Matplotlib figure area and no axis labels by default. See draw_networkx() for more full-featured drawing that allows title, axis labels etc.

Parameters : G graph

pos dictionary, 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.

ax Matplotlib Axes object, optional

Draw the graph in specified Matplotlib axes.

kwds optional keywords

See networkx.draw_networkx() for a description of optional keywords.

This function has the same name as pylab.draw and pyplot.draw so beware when using from networkx import *

since you might overwrite the pylab.draw function.

>>> import matplotlib.pyplot as plt >>> G = nx.dodecahedral_graph() >>> nx.draw(G) # networkx draw() >>> plt.draw() # pyplot draw() 
>>> G = nx.dodecahedral_graph() >>> nx.draw(G) >>> nx.draw(G, pos=nx.spring_layout(G)) # use spring layout 

Источник

Drawing#

NetworkX provides basic functionality for visualizing graphs, but its main goal is to enable graph analysis rather than perform graph visualization. In the future, graph visualization functionality may be removed from NetworkX or only available as an add-on package.

Proper graph visualization is hard, and we highly recommend that people visualize their graphs with tools dedicated to that task. Notable examples of dedicated and fully-featured graph visualization tools are Cytoscape, Gephi, Graphviz and, for LaTeX typesetting, PGF/TikZ. To use these and other such tools, you should export your NetworkX graph into a format that can be read by those tools. For example, Cytoscape can read the GraphML format, and so, networkx.write_graphml(G, path) might be an appropriate choice.

Matplotlib#

Draw networks with matplotlib.

Examples#

>>> G = nx.complete_graph(5) >>> nx.draw(G) 

See Also#

Draw the graph G with Matplotlib.

Draw the graph G using Matplotlib.

Draw the nodes of the graph G.

Draw the edges of the graph G.

Draw node labels on the graph G.

Draw the graph G with a circular layout.

Draw the graph G with a Kamada-Kawai force-directed layout.

Draw a planar networkx graph G with planar layout.

Draw the graph G with a random layout.

Draw the graph G with a spectral 2D layout.

Draw the graph G with a spring layout.

Draw networkx graph G with shell layout.

Graphviz AGraph (dot)#

Interface to pygraphviz AGraph class.

Examples#

>>> G = nx.complete_graph(5) >>> A = nx.nx_agraph.to_agraph(G) >>> H = nx.nx_agraph.from_agraph(A) 

See Also#

Returns a NetworkX Graph or DiGraph from a PyGraphviz graph.

Returns a pygraphviz graph from a NetworkX graph N.

Write NetworkX graph G to Graphviz dot format on path.

Returns a NetworkX graph from a dot file on path.

Create node positions for G using Graphviz.

Create node positions for G using Graphviz.

Graphviz with pydot#

Import and export NetworkX graphs in Graphviz dot format using pydot.

Either this module or nx_agraph can be used to interface with graphviz.

Читайте также:  Знакомство с GET-запросами

Examples#

>>> G = nx.complete_graph(5) >>> PG = nx.nx_pydot.to_pydot(G) >>> H = nx.nx_pydot.from_pydot(PG) 

See Also#

Returns a NetworkX graph from a Pydot graph.

Returns a pydot graph from a NetworkX graph N.

Write NetworkX graph G to Graphviz dot format on path.

Returns a NetworkX MultiGraph or MultiDiGraph from the dot file with the passed path.

Create node positions using Pydot and Graphviz.

Create node positions using pydot and Graphviz.

Graph Layout#

Node positioning algorithms for graph drawing.

For random_layout() the possible resulting shape is a square of side [0, scale] (default: [0, 1]) Changing center shifts the layout by that amount.

For the other layout routines, the extent is [center — scale, center + scale] (default: [-1, 1]).

Warning: Most layout routines have only been tested in 2-dimensions.

Position nodes in two straight lines.

Position nodes on a circle.

Position nodes using Kamada-Kawai path-length cost-function.

Position nodes without edge intersections.

Position nodes uniformly at random in the unit square.

Returns scaled position array to (-scale, scale) in all axes.

Return a dictionary of scaled positions keyed by node

Position nodes in concentric circles.

Position nodes using Fruchterman-Reingold force-directed algorithm.

Position nodes using the eigenvectors of the graph Laplacian.

Position nodes in a spiral layout.

Position nodes in layers of straight lines.

LaTeX Code#

Export NetworkX graphs in LaTeX format using the TikZ library within TeX/LaTeX. Usually, you will want the drawing to appear in a figure environment so you use to_latex(G, caption=»A caption») . If you want the raw drawing commands without a figure environment use to_latex_raw() . And if you want to write to a file instead of just returning the latex code as a string, use write_latex(G, «filename.tex», caption=»A caption») .

To construct a figure with subfigures for each graph to be shown, provide to_latex or write_latex a list of graphs, a list of subcaptions, and a number of rows of subfigures inside the figure.

To be able to refer to the figures or subfigures in latex using \\ref , the keyword latex_label is available for figures and sub_labels for a list of labels, one for each subfigure.

We intend to eventually provide an interface to the TikZ Graph features which include e.g. layout algorithms.

Let us know via github what you’d like to see available, or better yet give us some code to do it, or even better make a github pull request to add the feature.

The TikZ approach#

Drawing options can be stored on the graph as node/edge attributes, or can be provided as dicts keyed by node/edge to a string of the options for that node/edge. Similarly a label can be shown for each node/edge by specifying the labels as graph node/edge attributes or by providing a dict keyed by node/edge to the text to be written for that node/edge.

Options for the tikzpicture environment (e.g. “[scale=2]”) can be provided via a keyword argument. Similarly default node and edge options can be provided through keywords arguments. The default node options are applied to the single TikZ “path” that draws all nodes (and no edges). The default edge options are applied to a TikZ “scope” which contains a path for each edge.

Читайте также:  Си шарп код юнити

Examples#

>>> G = nx.path_graph(3) >>> nx.write_latex(G, "just_my_figure.tex", as_document=True) >>> nx.write_latex(G, "my_figure.tex", caption="A path graph", latex_label="fig1") >>> latex_code = nx.to_latex(G) # a string rather than a file 

You can change many features of the nodes and edges.

>>> G = nx.path_graph(4, create_using=nx.DiGraph) >>> pos = n: (n, n) for n in G> # nodes set on a line 
>>> G.nodes[0]["style"] = "blue" >>> G.nodes[2]["style"] = "line width=3,draw" >>> G.nodes[3]["label"] = "Stop" >>> G.edges[(0, 1)]["label"] = "1st Step" >>> G.edges[(0, 1)]["label_opts"] = "near start" >>> G.edges[(1, 2)]["style"] = "line width=3" >>> G.edges[(1, 2)]["label"] = "2nd Step" >>> G.edges[(2, 3)]["style"] = "green" >>> G.edges[(2, 3)]["label"] = "3rd Step" >>> G.edges[(2, 3)]["label_opts"] = "near end" 
>>> nx.write_latex(G, "latex_graph.tex", pos=pos, as_document=True) 

Then compile the LaTeX using something like pdflatex latex_graph.tex and view the pdf file created: latex_graph.pdf .

If you want subfigures each containing one graph, you can input a list of graphs.

>>> H1 = nx.path_graph(4) >>> H2 = nx.complete_graph(4) >>> H3 = nx.path_graph(8) >>> H4 = nx.complete_graph(8) >>> graphs = [H1, H2, H3, H4] >>> caps = ["Path 4", "Complete graph 4", "Path 8", "Complete graph 8"] >>> lbls = ["fig2a", "fig2b", "fig2c", "fig2d"] >>> nx.write_latex(graphs, "subfigs.tex", n_rows=2, sub_captions=caps, sub_labels=lbls) >>> latex_code = nx.to_latex(graphs, n_rows=2, sub_captions=caps, sub_labels=lbls) 
>>> node_color = 0: "red", 1: "orange", 2: "blue", 3: "gray!90"> >>> edge_width = e: "line width=1.5" for e in H3.edges> >>> pos = nx.circular_layout(H3) >>> latex_code = nx.to_latex(H3, pos, node_options=node_color, edge_options=edge_width) >>> print(latex_code) \documentclass \usepackage \usepackage \begin \begin \begin \draw (1.0, 0.0) node[red] (0) (0.707, 0.707) node[orange] (1) (-0.0, 1.0) node[blue] (2) (-0.707, 0.707) node[gray!90] (3) (-1.0, -0.0) node (4) (-0.707, -0.707) node (5) (0.0, -1.0) node (6) (0.707, -0.707) node (7); \begin[-] \draw[line width=1.5] (0) to (1); \draw[line width=1.5] (1) to (2); \draw[line width=1.5] (2) to (3); \draw[line width=1.5] (3) to (4); \draw[line width=1.5] (4) to (5); \draw[line width=1.5] (5) to (6); \draw[line width=1.5] (6) to (7); \end \end \end \end

Notes#

If you want to change the preamble/postamble of the figure/document/subfigure environment, use the keyword arguments: figure_wrapper , document_wrapper , subfigure_wrapper . The default values are stored in private variables e.g. nx.nx_layout._DOCUMENT_WRAPPER

References#

Return a string of the LaTeX/TikZ code to draw G

Return latex code to draw the graph(s) in Gbunch

Write the latex code to draw the graph(s) onto path .

Источник

draw_networkx#

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 : G graph

pos dictionary, 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.

arrows bool 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.

arrowstyle str (default=’-|>’ for directed graphs)

For directed graphs, choose the style of the arrowsheads. For undirected graphs default to ‘-’

arrowsize int 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_labels bool (default=True)

Set to True to draw labels on the nodes.

ax Matplotlib Axes object, optional

Draw the graph in the specified Matplotlib axes.

nodelist list (default=list(G))

Draw only specified nodes

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

Draw only specified edges

node_size scalar or array (default=300)

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

node_color color 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_shape string (default=’o’)

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

alpha float or None (default=None)

The node and edge transparency

cmap Matplotlib colormap, optional

Colormap for mapping intensities of nodes

vmin,vmax float, optional

Minimum and maximum for node colormap scaling

linewidths scalar or sequence (default=1.0)

Line width of symbol border

width float or array of floats (default=1.0)

edge_color color 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_cmap Matplotlib colormap, optional

Colormap for mapping intensities of edges

edge_vmin,edge_vmax floats, optional

Minimum and maximum for edge colormap scaling

style string (default=solid line)

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

labels dictionary (default=None)

Node labels in a dictionary of text labels keyed by node

font_size int (default=12 for nodes, 10 for edges)

Font size for text labels

font_color string (default=’k’ black)

font_weight string (default=’normal’)

font_family string (default=’sans-serif’)

label string, optional

kwds optional keywords

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

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

>>> 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 

Источник

Оцените статью