%matplotlib inline
import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_node('abc@gmail.com')
nx.draw(G, with_labels=True)
plt.show()
The output figure is
What I want is
I have thousands of email records from person@email.com to another@email.com in a csv file, I use G.add_node(email_address)
and G.add_edge(from, to)
to build G. I want keep the whole email address in Graph G but display it in a simplified string.
networkx
has a method called relabel_nodes
that takes a graph ( G
), a mapping
(the relabeling rules) and returns a new graph ( new_G
) with the nodes relabeled.
That said, in your case:
import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_node('abc@gmail.com')
mapping = {
'abc@gmail.com': 'abc'
}
relabeled_G = nx.relabel_nodes(G,mapping)
nx.draw(relabeled_G, with_labels=True)
plt.show()
That way you keep G
intact and haves simplified labels.
You can optionally modify the labels in place, without having a new copy, in which case you'd simply call G = nx.relabel_nodes(G, mapping, copy=False)
If you don't know the email addresses beforehand, you can pass relabel_nodes
a function, like so:
G = nx.relabel_nodes(G, lambda email: email.split("@")[0], copy=False)
import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_node('abc@gmail.com')
mapping = {'abc@gmail.com':'abc' }
G=nx.relabel_nodes(G, mapping)
nx.draw(G, with_labels=True)
plt.rcParams["figure.figsize"] = [10,10]
plt.axis('off')
plt.show()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.