python - Making networkx plot where edges only display edited numeric value, not field name -


the labels fields displayed. can see metric , weight

i want make can put $ on weight number , set edge text. can smart tell me trick this? example if weight of edge 20, want edge text "$20"

here code.

import json import networkx nx import matplotlib.pyplot plt import os import random networkx import graphviz_layout  g=nx.graph()   fn in os.listdir(os.getcwd()):     open(fn) data_file:             data = json.load(data_file)         name=data["name"]         name=name.split(',')         name = name[1] +  " " + name[0]         cycle=data["cycle"]         contributions=data["contributions"]         contributionlistforindustry=[]         colorlist=[]         colorlist.append((random.uniform(0,1),random.uniform(0,1),random.uniform(0,1)))          contibution in contributions:             amount=contibution["amount"]             industryname=contibution["name"]             metric=contibution["metric"]             colorlist.append((random.uniform(0,1),random.uniform(0,1),random.uniform(0,1)))             contributionlistforindustry.append((industryname,amount))             g.add_edge(name,industryname,weight=amount, metricval=metric)         position=nx.graphviz_layout(g,prog='twopi',args='')         nx.draw(g,position,with_labels=false,node_color=colorlist )           p in position:  # raise text positions                 t= list(position[p])                 t[1]=t[1]+10                 position[p]=tuple(t)         nx.draw_networkx_edge_labels(g,position)         nx.draw_networkx_labels(g, position)         plt.title("break down donations " + name + " agriculture industry " +  str(cycle)  )         plt.show() 

also if tell me how can make text appear in front of plot, i.e text not being visually sliced edges, edge text on top of edge if should pass it. lastly, reason title of plot isn't showing up. if knew solution awesome. guys. big help.

the documentation outlines have use edge_labels argument specify custom labels. default string representation of edge data used. in example below such dictionary created: has edge tuples keys , formatted strings values.

to make node labels stand out more can add bounding box corresponding text elements. can after draw_networkx_labels has created them:

import matplotlib.pyplot plt import networkx nx  # define graph g = nx.graph() g.add_edges_from([(1,2,{'weight':10, 'val':0.1}),                   (1,4,{'weight':30, 'val':0.3}),                   (2,3,{'weight':50, 'val':0.5}),                   (2,4,{'weight':60, 'val':0.6}),                   (3,4,{'weight':80, 'val':0.8})]) # generate positions nodes pos = nx.spring_layout(g, weight=none)  # create dictionary formatted labels edge_labels = {i[0:2]:'${}'.format(i[2]['weight']) in g.edges(data=true)}  # create longer node labels node_labels = {n:"this node {}".format(n) n in range(1,5)}   # draw graph nx.draw_networkx(g, pos=pos, with_labels=false)  # draw custom node labels shifted_pos = {k:[v[0],v[1]+.04] k,v in pos.iteritems()} node_label_handles = nx.draw_networkx_labels(g, pos=shifted_pos,         labels=node_labels)  # add white bounding box behind node labels [label.set_bbox(dict(facecolor='white', edgecolor='none')) label in         node_label_handles.values()]  # add custom egde labels nx.draw_networkx_edge_labels(g, pos=pos, edge_labels=edge_labels)  plt.show() 

edit:

you can't remove axes container entire graph. people make spines invisible:

# axes settings (make spines invisible, remove ticks , set title) ax = plt.gca() [sp.set_visible(false) sp in ax.spines.values()] ax.set_xticks([]) ax.set_yticks([]) 

setting title should straightforward:

ax.set_title('this nice figure') # or  plt.title('this nice figure') 

result: enter image description here


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -