import matplotlib.pyplot as plt # 将图形绘制成一个矩形布局 pos = nx.spring_layout(G, k=0.5, iterations=50) # 绘制图形的节点和边 nx.draw_networkx_nodes(G, pos) nx.draw_networkx_edges(G, pos) # 添加节点标签 node_labels = {node: str(node) for node in G.nodes()} nx.draw_networkx_labels(G, pos, node_labels) # 添加边权重标签 edge_labels = {(u, v): str(d['weight']) for u, v, d in G.edges(data=True)} nx.draw_networkx_edge_labels(G, pos, edge_labels) # 显示图形 plt.show() 修改代码,将图的标题改为“网络图”
时间: 2024-03-14 20:42:51 浏览: 62
import numpy as np import matplotlib.pyplot as plt import math
可以在最后一行代码前添加以下代码来修改图的标题:
```
plt.title("网络图")
```
修改后的代码如下:
```
import matplotlib.pyplot as plt
pos = nx.spring_layout(G, k=0.5, iterations=50)
nx.draw_networkx_nodes(G, pos)
nx.draw_networkx_edges(G, pos)
node_labels = {node: str(node) for node in G.nodes()}
nx.draw_networkx_labels(G, pos, node_labels)
edge_labels = {(u, v): str(d['weight']) for u, v, d in G.edges(data=True)}
nx.draw_networkx_edge_labels(G, pos, edge_labels)
plt.title("网络图")
plt.show()
```
阅读全文