python - 创建一个图形,节点为带有文本的圆圈
问题描述
使用 Networkx,我正在创建一个图表,
我想将每个节点创建为一个圆圈,然后定义边缘。
from tkinter import *
import networkx as nx
G=nx.Graph()
G.add_node(0) # text say ,"Hello" placed in circle/rectangle
从此处给出的示例中,我尝试使用画布创建文本标签
Canvas.drawText(10, 20, "A Text String")
我收到以下错误,
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: type object 'Canvas' has no attribute 'drawText'
有关如何解决此问题的任何建议,或者是否有任何替代方法可以将每个节点创建为包含文本的圆形/矩形?
解决方案
绘制 Networkx 图的最简单方法是通过它的绘图API。
例如:
import networkx as nx
import matplotlib.pyplot as plt
G = nx.barabasi_albert_graph(20, 2)
nx.draw_networkx(G, with_labels=True, labels={node : 'some text {}'.format(node) for node in G.nodes()})
plt.show()
推荐阅读
- c++ - for循环的两个代码有什么不同?
- java - 为什么java main 方法参数名称是args?
- plot - 在 R-Studio 终端上显示绘图
- excel - 在 Excel 中使用 Power Query / Get & Transform 访问 Cloudflare 保护背后的文件
- oracle - 如果是星期日,则在 oracle 报告中显示 Week rest else
- android - 在 React Native 上安装 firebase 时出错
- c++ - 防止打印失败检查
- wildfly - Wildfly没有启动
- c# - .NET Worker 服务中的构造函数注入
- angular-material - 使用 angular 10 和行跨度导出 excel,使用分页的 colspan