- 导入包networkx
pip install networkx
-
import networkx as nx
G = nx.Graph()
G.add_node(1)
G.add_nodes_from([2,3])
G.add_nodes_from([
(4, {"color": "red"}),
(5, {"color": "green"})])
G.add_edge(1,2)
G.add_edge(*(2,3))
G.add_edges_from(([3,4],[1,5],[2,4]))
G.add_edges_from([(1,3), (2,5)])
G.add_node("node")
G.add_edges_from([("node",1), ("node", 3), ("node", 5)])
print(G.number_of_nodes(), G.number_of_edges())
print(list(G.nodes))
print(list(G.edges))
print(list(G.adj["node"]))
print(G.degree["node"])
```python
pandas 与 图
cora_graph = nx.from_pandas_edgelist(citations.sample(n=1500))#可以直接将pandas中数据边导入图中
G = nx.Graph()
G.add_edges_from(([2,3],[3,4]), [4,5], [5,6])
nx.draw(G,with_labels = True)
plt.show()