Ну, сначала вам нужно добавить вершины на график
# your code
# from graph_tool.all import * # avoid importing all, it makes it inclear what you are using afterwards
import graph_tool as gt
class Student:
def __init__(self, name):
self.name = name
class Professor:
def __init__(self, name):
self.name = name
g = gt.Graph()
s = Student("Luke")
p = Professor("Adam")
print(s.name)
print(p.name)
Luke
Adam
, с помощью которого вы создаете карту свойств для типа ваших пользователей
g.vertex_properties['user_type'] = g.new_vertex_property('string')
Это будетсвойство для ваших узлов.Вы можете создать, сколько вы хотите, а затем установить их user_type. Например:
# creating a graph of 3 users
g.add_vertex(3)
# Let's set the first vertex as a professor
g.vp['user_type'][g.vertex(0)] = 'professor'
# and the subsequent as students
for v in g.get_vertices()[1:]:
g.vp['user_type'][v] = 'student'
# let's add an edge from the professor to the first student
g.add_edge(g.vertex(0), g.vertex(1))
<Edge object with source '0' and target '1' at 0x7f2c30370c30>
Теперь вы можете нарисовать график, используя свойство
def translate_elts_to_gtshape(g, source_prop='user_type', tdict=None):
"""This function adds a new property map called 'shape' to graph g. It is populated by translating the source_prop with the tdict (translation dictionnary) which should map source_property entries to shapes"""
if tdict is None:
tdict = {'professor': 'square',
'student': 'circle'}
# source_property map array. It's of string type so we use get_2d_array
svp = g.vp[source_prop].get_2d_array([0])[0]
for k, v in tdict.items():
# for each key, value pair of tdict we change the source value (ie k) to the shape (v)
svp[svp == k] = v
# and now we create the new property shape
g.vertex_properties['shape'] = g.new_vertex_property('string')
for v, p in zip(g.vertices(), svp):
# and copy in it the shape
g.vp['shape'][v] = p
# we return the new property created, although it is already added to the graph as an internal property
return g.vp['shape']
translate_elts_to_gtshape(g)
<PropertyMap object with key type 'Vertex' and value type 'string', for Graph 0x7f2c5c3d6510, at 0x7f2c2997c950>
# let's just check what we have in our newly created shape property
[g.vp['shape'][v] for v in g.vertices()]
['square', 'circle', 'circle']
# and now let's use it to draw our graph
import graph_tool.draw as gtd
pos = gtd.sfdp_layout(g)
gtd.graph_draw(g, pos=pos,
output_size = (300, 300),
output_file = 'mynetwork.png', # where your file goes
vertex_shape=g.vp['shape'],
vertex_text=g.vertex_index)
<PropertyMap object with key type 'Vertex' and value type 'vector<double>', for Graph 0x7f2c5c3d6510, at 0x7f2c14291590>