用python graphtool自定义绘图

2024-04-19 16:36:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我喜欢使用graph-tool绘制一个图,其中边权重被写入边

我可以画一张图,但边权重的注释很奇怪:

import graph_tool.all as gt
g = gt.Graph(directed=True)

import numpy as np
import random
dim = 7 
sparsity = .27

# create adjacency matrix
M1 = np.matrix(random.choices([0,1], weights=[1-sparsity, sparsity], k=dim*dim)).reshape(dim,-1)
M = np.multiply(np.random.randint(1, 10, (dim, dim)), M1)    # element-wise multiplication by M1
np.fill_diagonal(M, 0)  # inplace

adjlist = np.transpose(M.nonzero())

g.add_edge_list(adjlist)
weights = M[M.nonzero()]

#add weights as an edge propertyMap
ew = g.new_edge_property("double")
ew.a = weights 
g.ep['edge_weight'] = ew


gt.graph_draw(g, vertex_text=g.vertex_index, vertex_font_size=18, edge_text=ew, edge_font_size=18, output_size=(400, 400),
            output="/home/info/99_deleteme/mygraph.png")

这是生成的图形:

Graph

在哪里可以找到https://graph-tool.skewed.de/static/doc/draw.html#graph_tool.draw.graph_draw中建议的所有可能的vertex_*edge_*属性的列表(甚至文档)

谷歌搜索只能得到像https://graph-tool.skewed.de/static/doc/_modules/graph_tool/flow.html这样的例子


Tags: importgtasnprandomtoolgraphvertex