I want to make it so I can put a $ on the weight number and set it as the edge text. Can someone smart tell me the trick to do this? For example if the weight of an edge is 20, I want the edge text to be "$20"
Here is my code.
import json
import networkx as nx
import matplotlib.pyplot as plt
import os
import random
from networkx import graphviz_layout
G=nx.Graph()
for fn in os.listdir(os.getcwd()):
with open(fn) as 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)))
for 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 )
for 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 for donations to " + name + " from agriculture industry for " + str(cycle) )
plt.show()
Also if someone could tell me how I can make text appear to be in front of the plot, I.E the text is not being visually sliced by the edges, the edge text is on top of the edge if it should pass it. Lastly, for some reason the title of my plot isn't showing up. If someone knew a solution for that also it would be awesome. Thanks guys. Always a big help.
The documentation outlines that you have to use the
edge_labels
argument to specify custom labels. By default it the string representation of the edge data is used. In the example below such a dictionary is created: It has the edge tuples as keys and the formatted strings as values.To make the node labels stand out more you can add a bounding box to the corresponding text elements. You can do that after
draw_networkx_labels
has created them:EDIT:
You can't really remove the axes as they are the container for the entire graph. So what people usually do is to make the spines invisible:
Setting the title should be straightforward:
Result: