Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
428 views
in Technique[技术] by (71.8m points)

How do I access Class fields in Python Graph-Tool property maps?

I'm trying to draw a graph with a class as a vertex property. How do I draw the graph with the vertex_text set to the name field of the classes they contain?

from graph_tool.all import *

class Node(object):
    def __init__(self, name, age):
        self.symbol = name
        self.named_entity = age


#create your graph object
g = Graph()

#add the property to vertex object
vprop = g.new_vertex_property("object") 

#add vertex 
v1 = g.add_vertex() #here you create a vertex
v2 = g.add_vertex() #here you create a vertex

#set the value to the vertex property
vx1 = Node("John", 15)
vx2 = Node("Sarah", 22)

vprop[v1] = vx1
vprop[v2] = vx2

#assign properties as a dic value
g.vertex_properties["node"]=vprop 

#add edge
g.add_edge(vertex_1,vertex_2) #add an edge 

#draw you graph 
graph_draw(
    g,
    vertex_text=g.vertex_properties["node"].name,
    vertex_font_size=18,
    output_size=(200, 200), 
)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The code example you give is full of basic errors. You define vprop but then you use v_prop. The code would not work in any case, as the property map is defined to be of string type, but you are setting to it a class object.

But answer to the underlying question is simply to set a the names to a string property map and use that.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...