Reputation: 155
Im trying to set text in different vertexs and draw them, but I don't know how to make it. I searched graph_tool documentaction but I can't find out how to make it because examples there are so confused...
My code is:
from graph_tool.all import *
g = Graph()
g.add_vertex()
// How to something like: g.vertex(0).text = "A"
g.add_vertex()
// How to something like: g.vertex(1).text = "B"
g.add_edge(g.vertex(0),g.vertex(1))
// And how to use it instead of vertex_index
graph_draw(g, vertex_text=g.vertex_index, vertex_font_size=18, output_size=(200, 200), output="test.png")
Waiting for any clues
Upvotes: 4
Views: 8370
Reputation: 6737
Please try the following code:
from graph_tool.all import *
#create your graph object
g = Graph()
#add the property to vertex object
vprop = g.new_vertex_property("string")
#add vertex
vertex_1 = g.add_vertex() #here you create a vertex
vertex_2 = g.add_vertex() #here you create a vertex
#set the value to the vertex property
vprop[vertex_1] = "A" #here you set text to vertex_1 property
vprop[vertex_2] = "B" #here you set text to vertex_2 property
#assign properties as a dic value
g.vertex_properties["name"]=vprop
#add edge
g.add_edge(vertex_1,vertex_2) #add an edge
#draw you graph
graph_draw(
g,
vertex_text=g.vertex_properties["name"],
vertex_font_size=18,
output_size=(200, 200),
output="test.png"
)
This code works for me (on OS X 10.10.4 & Ubuntu 14.04)
Upvotes: 5
Reputation: 9798
Actually it is written but not very clear. If you look at the notes section, you can see all the node and edge properties that can be set.
Individual properties can be set like this:
vertex_* : PropertyMap or arbitrary types (optional, default: None)
edge_* : PropertyMap or arbitrary types (optional, default: None)
Where the * can be: text, size, shape, and so on with all the properties listed in the notes section.
You can also supply dictionaries to draw methods:
vprops : dict (optional, default: None)
Dictionary with the vertex properties. Individual properties may also be given via the vertex_ parameters, where is the name of the property.
eprops : dict (optional, default: None)
Dictionary with the edge properties. Individual properties may also be given via the edge_ parameters, where is the name of the property.
In your example, the draw method should be something like this:
graph_draw(g, vertex_text=g.vertex_properties["name"], ...)
Upvotes: 1
Reputation: 641
#!/usr/bin/python3
from graph_tool.all import *
g = Graph()
v1 = g.add_vertex()
v2 = g.add_vertex()
e = g.add_edge(v1, v2)
v_prop = g.new_vertex_property("string")
v_prop[v1] = 'foo'
v_prop[v2] = 'bar'
e_prop = g.new_edge_property("string")
e_prop[e] = 'e1'
graph_draw(g, vertex_text=v_prop,edge_text=e_prop, vertex_font_size=18, output_size=(200, 200), output="two-nodes.png")
This should really be on the graph-tools website intro.
If you can find out how to set an edge length I'd like to know.
#!/usr/bin/python3
from graph_tool.all import *
g = Graph()
v1 = g.add_vertex()
v2 = g.add_vertex()
v3 = g.add_vertex()
e2 = g.add_edge(v1, v2)
e1 = g.add_edge(v1, v3)
v_prop = g.new_vertex_property("string")
v_prop[v1] = 'foo'
v_prop[v2] = 'bar'
v_prop[v3] = 'baz'
e_prop = g.new_edge_property("string")
e_prop[e1] = 'edge 1'
e_prop[e2] = 'edge 2'
e_len = g.new_edge_property("double")
e_len[e1] = 10
e_len[e2] = 20
graph_draw(g, vertex_text=v_prop, edge_text=e_prop, edge_pen_width = e_len, vertex_font_size=18, output_size=(800, 800), output="two-nodes.png")
This is what I'm using instead, because I can manipulate the widths... It complains if I try to set the length.
Upvotes: 8