Wednesday, 15 February 2012

matplotlib - Drawing a custom diagram in python -



matplotlib - Drawing a custom diagram in python -

i wanna draw :

the closest thing find networkx border colormap:

http://networkx.github.io/documentation/latest/examples/drawing/edge_colormap.html

and here source code:

#!/usr/bin/env python """ draw graph matplotlib, color edges. must have matplotlib>=87.7 work. """ __author__ = """aric hagberg (hagberg@lanl.gov)""" try: import matplotlib.pyplot plt except: raise import networkx nx g=nx.star_graph(20) pos=nx.spring_layout(g) colors=range(20) nx.draw(g,pos,node_color='#a0cbe2',edge_color=colors,width=4,edge_cmap=plt.cm.blues,with_labels=false) plt.savefig("edge_colormap.png") # save png plt.show() # display

after playing around source code, i can't figure out how hardcode distance of border circles centre. right random.

also how label border circles , distance centre?

i know position comes pos=nx.spring_layout(g). looked @ spring_layout attribute , found position can specified using pos variable dictionary nodes keys , values list. (https://networkx.github.io/documentation/latest/reference/generated/networkx.drawing.layout.spring_layout.html)

but when next result random edges :

ap = {'uniwide':[55,34,1],'eduram':[34],'uniwide_webauth':[20,55,39],'uniwide_guest':[55,34],'tele9751_lab':[100],'homesdn':[100],'tp-link':[39]} pos=nx.spring_layout(g,pos=ap)

you can set node positions explicitly pos dictionary. example

import networkx nx import matplotlib.pyplot plt g = nx.graph() g.add_edge('center',1) g.add_edge('center',2) g.add_edge('center',3) g.add_edge('center',4) pos = {'center':(0,0), 1:(1,0), 2:(0,1), 3:(-1,0), 4:(0,-1) } nx.draw(g, pos=pos, with_labels=true) plt.show()

python matplotlib networkx

No comments:

Post a Comment