Introduction to Python networkx Drawing Network Diagram __python

Source: Internet
Author: User
turn from: http://www.cnblogs.com/huiyang865/p/5677449.html draw a basic network Diagram

To draw a network diagram with Matplotlib
Basic process:
1. Import Networkx,matplotlib Package
2. Network creation
3. Draw Network Nx.draw ()
4. Set up the layout pos = nx.spring_layout beautification function
Most basic Drawing program

1 Import networkx as NX             #导入networkx包
2 import matplotlib.pyplot as Plt 
3 G = Nx.random_graphs.barabasi_albert _graph (100,1)   #生成一个BA无标度网络G
4 nx.draw (G)                               #绘制网络G
5 plt.savefig ("Ba.png")           #输出方式1: Save image as a picture file in PNG format
6 plt.show ()                            

The

Networkx function that provides paint is: Draw (G,[pos,ax,hold]) Draw_networkx (G,[pos,with_labels]) draw_networkx_nodes (g,pos,[nodelist ) Draw the network G node graph Draw_networkx_edges (G,pos[edgelist]) to draw the edge map of the network G Draw_networkx_edge_labels (g, pos[, ...)) to draw the Bentu of the network G, with label on the Edge
---The dividing line of the layout layout drawing function---draw_circular (g, **kwargs)  draw the graph G with a circular layout. Draw_random (g, **kw args)  draw the graph G with a random layout. Draw_spectral (g, **kwargs)  draw the graph G with a spectral layout. Draw_spring (g, **kwargs)  draw the graph G with a spring layout. Draw_shell (G, **kwargs)  draw networkx graph with Shell layout. Draw_graphviz (g[, prog])  draw networkx graph with Graphviz layout.

networkx Paint parameter:
- node_size: Specifies the size of the node (default is 300, the unit is unknown, which is the largest point in the picture above)
- node_color: Specifies the color of the node (the default is red, you can use a simple string to identify the color, such as ' R ' for Red, ' B ' for green, and so on, specifically to view the manual, and you must value the dictionary (. VALUES ()) When you assign a value to the data dictionary, and then assign the
- node_shape: The shape of the node (the default is circular, with the string ' O ' identification, the specific view manual)
- alpha: Transparency (default is 1.0, opaque, 0 is fully transparent)
- width: Edge width (default is 1.0)
-  Edge_color: Edge Color (default is black)
- style: Side style (default to implementation, Optional: Solid|dashed|dotted,dashdot)
- with_labels: Whether the node has a label (default is True)
- font_size: node label font size (defaults to)
- font_color: node label font color (default is black)
e.g. Nx.draw (g, Node_size = With_label = False)
Draws a node with a network diagram with a size of 30 and no labels.

networkx Drawing parameters:
-Node_size: Specifies the size of the node (default is 300, the unit is unknown, that is, the large point in the picture above)
-Node_color: Specifies the color of the node (the default is red, you can use a simple string to identify the color, such as ' R ' is red, ' B ' is green, etc., you can view the manual, you must value the dictionary (. VALUES ()) When you assign a value to a "data dictionary", and then assign a value
-Node_shape: The Shape of the node (the default is circular, with the string ' o ' logo, specific to see the manual)
-Alpha: Transparency (default is 1.0, opaque, 0 is fully transparent)
-Width: Edge widths (defaults to 1.0)
-Edge_color: The Color of the edge (default is black)
-style: Side styles (default for implementation, Optional: Solid|dashed|dotted,dashdot)
-With_labels: Node with label (default is True)
-Font_size: Node label font size (defaults to 12)
-Font_color: Node label font color (default is black)
e.g. Nx.draw (g,node_size =, With_label = False)
Draws the node's size to 30, without the label of the Network Diagram.

layout Specify node arrangement form

pos = Nx.spring_layout

Set up the layout, the layout of the map to beautify, NETWORKX provided by the layout of the way are:
-Circular_layout: The nodes are evenly distributed on a ring
-Random_layout: Node random distribution
-Shell_layout: Nodes are distributed on concentric circles
-Spring_layout: Arranging nodes with Fruchterman-reingold algorithm
-Spectral_layout: Arranging knots according to the Laplace eigenvector of graphs
The layout can also be specified with the POS parameter, for example, Nx.draw (g, pos = Spring_layout (g)), thus designating the networkx as a central radial distribution. plot a partitioned community

First look at a piece of code, the code from the site

Partition = Community.best_partition (User)
size = float (len (Set (Partition.values ()))
pos = nx.spring_layout (G)
Count = 0.

For COM in set (Partition.values ()):
    count = Count + 1.
    List_nodes = [nodes for nodes in Partition.keys ()
                                if partition[nodes] = = com]                 
    nx.draw_networkx_nodes (G, POS, List _nodes, node_size = m,
                                node_color = str (count/size))

nx.draw_networkx_edges (user,pos,with_labels = True, alpha=0.5)
plt.show ()

Communit.best_partition is a community partitioning method, which is based on the optimization method of modularity heuristic, which is proposed by Vincent D.blondel and others in 2008.
The results of the partition exist in dictionary data types:
{' 1 ': 0, ' 3 ': 1, ' 2 ': 0, ' 5 ': 1, ' 4 ': 0, ' 6 ': 0}
The data in single quotes is key, which is the number of nodes in the network.
The value following the colon that indicates which community the number of nodes in the network belongs to. Which is the community label. As ' 6 ': 0 means 6 nodes belong to 0 communities

Each cycle list_nodes result is the corresponding user number of the community I.
As the first loop result is com = 0, list_nodes= [' 1 ', ' 2 ', ' 4 ', ' 6 ']
The result of the second cycle is com = 1, list_nodes = [' 3 ', ' 6 ']
So every time you loop, draw all the nodes of a community:

Nx.draw_networkx_nodes (G, POS, list_nodes, node_size = m,
                                node_color = str (count/size))

After the loop is over, color is used to identify different communities.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.