当前位置:网站首页>Networkx usage and nx Draw() related parameters

Networkx usage and nx Draw() related parameters

2022-06-10 20:35:00 Salted fish lying flat occasionally

networkx stay 02 year 5 Monthly production , Yes, it is python Software package written in , It is convenient for users to create complex networks 、 Operation and learning . utilize networkx The network can be stored in standardized and non-standard data formats 、 Generate a variety of random networks and classical networks 、 Analyze the network structure 、 Build a network model 、 Design a new network algorithm 、 Network drawing, etc .

Official documents :https://www.osgeo.cn/networkx/reference/introduction.html

Code

First import the package
import networkx as nx
Create an empty graph

G = nx.Graph()

Add nodes and edges

G.add_node(1) # Add a single node 
G.add_node("x",name='tome') # Add a single node and attributes 
G.add_nodes_from([2,3]) # Add multiple nodes from an iteratable container 
G.add_nodes_from([(4, {
    "color": "red"}), (5, {
    "color": "green"})])#  Add nodes and attributes 
H = nx.path_graph(10) # Create a new map 
G.clear() #  Empty map 
G.add_nodes_from(H) # Add nodes from another diagram 
print(list(H.nodes))
print(list(G.nodes))
G.add_edge(1,3)# Add an edge 
G.add_edges_from([(2,3),(3,4)])#  Add multiple edges 

View nodes and edges

list(G.nodes) # Look at the node 
for k, v in G.nodes.items():  print(k,v)  # View nodes and attributes 
G.number_of_nodes() # View several nodes 
G.adj[1] # View neighbor nodes 
G[1] # View neighbor nodes 
list(G.neighbors(1)) # View neighbor nodes 

G.number_of_edges() # View several edges 
G.add_edge(1,3) # View specific edges 
G.edges() # View edges 
G.add_edge(2,3)
G.edges([1]) # see 1 All edges connected 
G.edges([3]) # see 1 All edges connected 

G.degree[1] # Viewing degree 

for node,neighbors in g.adjacency():
    print(node, neighbors) #  View the content of adjacency matrix 

Delete nodes and edges

G.remove_node(4)
G.remove_edge(1,3)

Build a new graph from an existing graph

#  Build a directed graph 
g = nx.DiGraph(G)
nx.draw(g)

Get nodes and edges

G = nx.Graph([(1,2,{
    'color':'red'})])
G[1] # obtain 1 Neighbor node 
G[1][2] # Get edge properties 
G.edges[1,2] # Get edge properties 

chart 、 node 、 Edge properties

#  Graph level properties 
g = nx.Graph(day = 'none')
g.graph # Output graph level attribute information 
g.graph['day']= 'tom' # Modify graph level attribute information 
g.graph['date']= 'now' # Add graph level attribute information 
#  Node properties 
g.add_node(1, time='now')
g.add_nodes_from([2,3,4,5,6], time='yes')
g.nodes.data()
g.nodes[2]
#  Edge properties 
g.add_edge(1,2,time='now')
g.add_edges_from([(1,2,{
    'time':'now'}),(1,3,{
    'time':'naw'})])
G[1][2]['color'] = 'blue'
G.edges[1,2]['color'] ='Y'
g.edges.data()

Analysis chart

#  Analysis chart : Connected component 
g.clear()
g.add_edges_from([(1,2),(3,2)])
g.add_nodes_from("spam")
list(nx.connected_components(g)) # Yes 5 Connected components 
nx.draw(g) # visualization 
# Analysis chart : Sort by degrees 
sorted(((node,degree) for node,degree in g.degree), key= lambda d:d[1],reverse = True)

Figure Visualization

#  Figure Visualization 
g = nx.petersen_graph()
nx.draw(g, with_labels = True) # mark label

from edgelist Read graph

#  from edgelist Read graph 
g = nx.read_edgelist('edglist.txt')
nx.draw(g, with_labels= True)

Save map

plt.savefig("path.png")

networkx–nx.draw() Parameters

x.draw() Method , Accept at least one parameter : The network to be plotted G

Run style

  - `node_size`:   Specify the size of the node ( The default is 300)
  - `node_color`:   Specify the color of the node  ( The default is red , You can simply identify colors with strings , for example 'r' In red ,'b' For green, etc )
  - `node_shape`:   The shape of the node ( The default is circle , Use string 'o' identification )
  - `alpha`:  transparency  ( The default is 1.0, Opaque ,0 Is completely transparent )
  - `width`:  The width of the edge  ( The default is 1.0)
  - `edge_color`:  The color of the edges ( Default is black )
  - `style`:  The pattern of the sides ( The default is to implement , Optional : solid|dashed|dotted,dashdot)
  - `with_labels`:  Whether the node is labeled ( The default is True)
  - `font_size`:  Node label font size  ( The default is 12)
  - `font_color`:  Node label font color ( Default is black )

Use layout :

circular_layout: Nodes are evenly distributed on a ring
random_layout: Nodes are randomly distributed
shell_layout: Nodes are distributed on concentric circles
spring_layout:
use Fruchterman-Reingold The algorithm arranges the nodes ( It looks like multicentric radiation )
spectral_layout: The nodes are arranged according to the Laplacian eigenvector of the graph

To add text :

use plt.title() Method to add a title to the drawing , This method takes a string as an argument .
fontsize Parameter is used to specify the size of the title . for example :plt.title(“BA Networks”, fontsize = 20).
If you want to add text anywhere , You can use plt.text() Method .

原网站

版权声明
本文为[Salted fish lying flat occasionally]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101928350591.html