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

2024/11/18 12:33:22

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 = nameself.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), 
)
Answer

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.

https://en.xdnf.cn/q/120077.html

Related Q&A

How to iterate through each line of a text file and get the sentiment of those lines using python?

Currently, Im working on Sentiment Analysis part. For this I have preferred to use Standford Core NLP library using python. Im able to get the sentiment for each sentence using the following code : fro…

RECURSIVE function that will sum digits of input

Trying to write a piece of code that will sum the digits of a number. Also I should add that I want the program to keep summing the digits until the sum is only 1 digit. For example, if you start with …

Make sure matrix row took from text file are same length(python3) [duplicate]

This question already has answers here:Making sure length of matrix row is all the same (python3)(3 answers)Closed 10 years ago.so I have this code to input a matrix from a text file:import ospath = in…

how to randomize order of questions in a quiz in python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 9 years ago.Improve…

How transform days to hours, minutes and seconds in Python

I have value 1 day, 14:44:00 which I would like transform into this: 38:44:00. Ive tried the following code: myTime = ((myTime.days*24+myTime.hours), myTime.minutes, myTime.seconds) But it doesnt work.…

Brute Force in python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 10 years ago.Improv…

Choosing only non-zeros from a long list of numbers in text file

I have a text file with a long list of numbers. I would like to choose only the non-zeros and make another text file. This is a portion of the input file:0.00000E+00 0.00000E+00 0.00000E+00 0.00000…

Why can I not plot using Python on repl.it

For practical reasons, I want to test a small piece of Pyton code on repl.it (webbased, so I do not need to install Python).The codeimport numpy as np import matplotlib.pyplot as plttime = np.array([0,…

Pull Data from web link to Dataframe [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 5…

Python program that rolls a fair die counts the number of rolls before a 6 shows up

import randomsample_size = int(input("Enter the number of times you want me to roll the die: "))if (sample_size <=0):print("Please enter a positive number!")else:counter1 = 0coun…