python - How to represent networkx graphs with edge weight using nxpd like outptut -
recently asked question how represent graphs ipython. answer looking for, today i'm looking way show edge valuation on final picture.
the edge valuation added :
import networkx nx nxpd import draw # if library same or same # output of nxpd , answer question, that's # not issue import random g = nx.graph() g.add_nodes_from([1,2]) g.add_edge(1, 2, weight=random.randint(1, 10)) draw(g, show='ipynb')
and result here.
i read help
of nxpd.draw
(didn't see web documentation), didn't find anything. there way print edge value ?
edit : also, if there's way give formating function, good. example :
def edge_formater(graph, edge): return "my edge %s" % graph.get_edge_value(edge[0], edge[1], "weight")
edit2 : if there's library nxpd doing same output, it's not issue
edit3 : has work nx.{graph|digraph|multigraph|multidigraph}
if @ source nxpd.draw
(function draw_pydot
) calls to_pydot
filters graph attributes like:
if attr_type == 'edge': accepted = pydot.edge_attributes elif attr_type == 'graph': accepted = pydot.graph_attributes elif attr_type == 'node': accepted = pydot.node_attributes else: raise exception("invalid attr_type.") d = dict( [(k,v) (k,v) in attrs.items() if k in accepted] )
if pydot
find pydot.edge_attributes
contains valid graphviz-attributes. weight
strictly refers edge-weight graphviz if recall, , label
attribute need. try:
g = nx.graph() g.add_nodes_from([1,2]) weight=random.randint(1, 10) g.add_edge(1, 2, weight=weight, label=str(weight))
note haven't been able test if works, downvote if doesn't.
Comments
Post a Comment