from graphviz import *
import networkx as nx
from networkx import *
import matplotlib.pyplot as plt
G = nx.DiGraph()
G.add_node(1)
G.add_node(2)
G.add_edge(1,2)
myLabels = {1: 'node1', 2: 'node2'}
nx.set_node_attributes(G, myLabels, 'label')
nx.draw(G,with_labels=True)
So current I use the latest networkx. When I use nx.draw(G, with_values=True), it uses the vertex indexes instead of labels.
How can I fix this? Thank you.
Change this command: nx.draw(G,with_labels=True) to nx.draw(G,with_labels=True, labels = myLabels)
Related
I'm wanting to make nodes different sizes depending on the number of connections. However, it seems like this functionality isn't present in pyvis? Maybe I'm missing something or it might not be supported entirely.
Here's my sample code:
import networkx as nx
import matplotlib.pyplot as plt
from pyvis.physics import Physics
from pyvis.network import Network
G = nx.Graph()
G.add_edges_from([(1,2), (2,3), (2,4), (3,4)])
d = dict(G.degree)
print(d.keys())
print([v * 100 for v in d.values()])
nx.draw(G, nodelist=d.keys(), node_size=[v * 100 for v in d.values()])
plt.show()
G2 = Network(height="1500px", width="75%",bgcolor="#222222",font_color="white",directed=True)
G2.from_nx(G)
G2.show("network_map.html")
You can do that in a few lines by using the set_node_attributes function from networkx. It allows you to add/modify node attributes (see more info here). See below where I used it to change the size of the nodes:
import networkx as nx
import matplotlib.pyplot as plt
from pyvis.physics import Physics
from pyvis.network import Network
G = nx.Graph()
G.add_edges_from([(1,2), (2,3), (2,4), (3,4)])
scale=10 # Scaling the size of the nodes by 10*degree
d = dict(G.degree)
#Updating dict
d.update((x, scale*y) for x, y in d.items())
#Setting up size attribute
nx.set_node_attributes(G,d,'size')
G2 = Network(height="1500px", width="75%",bgcolor="#222222",font_color="white",directed=True)
G2.from_nx(G)
G2.show("network_map.html")
And the output gives:
I wonder how I can add the edge weights to my Bokeh plot (from NetworkX), preferably for edge_renderer.hover_glyph, like in this picture:
A possible answer for this problem is using nx.get_edge_attributes and pass the values to draw_networkx_edge_labels as the labels for the edges.
Here's the so awaited code:
pos=nx.spring_layout(G)
nx.draw_networkx(G, pos)
labels = nx.get_edge_attributes(G,'weight')
nx.draw_networkx_edge_labels(G,pos,edge_labels=labels)
Here's a full working example:
import networkx as nx
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(4,4))
G = nx.Graph()
G.add_node('A', pos="1,3!", demand=-10)
G.add_node('B', pos="3,5!")
G.add_node('C', pos="3,1!")
G.add_node('D', pos="4,3!")
G.add_node('E', pos="5,5!")
G.add_node('F', pos="5,1!")
G.add_node('G', pos="7,3!", demand=10)
G.add_edge('A','B',weight=20,capacity=20,label='(20,20)')
G.add_edge('A','C',weight=7,capacity=15,label='(7,15)')
G.add_edge('B','E',weight=16,capacity=21,label='(16,21)')
G.add_edge('B','D',weight=4,capacity=4,label='(4,4)')
G.add_edge('C','F',weight=3,capacity=9,label='(3,9)')
G.add_edge('C','D',weight=4,capacity=4,label='(4,4)')
G.add_edge('D','E',weight=2,capacity=2,label='(2,2)')
G.add_edge('D','F',weight=6,capacity=10,label='(6,10)')
G.add_edge('E','G',weight=18,capacity=24,label='(18,24)')
G.add_edge('F','G',weight=9,capacity=10,label='(9,10)')
pos=nx.spring_layout(G)
nx.draw_networkx(G, pos)
labels = nx.get_edge_attributes(G,'weight')
nx.draw_networkx_edge_labels(G,pos,edge_labels=labels)
I try to save a graph in graph6 format in Python with networkx. The obvious way does not seem to work:
import networkx as nx
g = nx.Graph()
g.add_edge('1','2')
nx.write_graph6(g,"anedge.g6")
g1 = nx.read_graph6("anedge.g6")
import matplotlib.pyplot as plt
nx.draw(g1)
plt.savefig("anedge.pdf")
This makes a pdf file showing a graph with two isolated vertices instead of two connected vertices.
Using g.add_edge(0, 1) instead of g.add_edge('1','2') it should work:
import networkx as nx
import matplotlib.pyplot as plt
g = nx.Graph()
g.add_edge(0, 1)
nx.write_graph6(g, 'anedge.g6')
g1 = nx.read_graph6('anedge.g6')
nx.draw(g1)
plt.savefig("anedge.pdf")
This is actually exposing a bug in the networkx graph6 generator when the nodes are not consecutively ordered from zero. Bug fix is here https://github.com/networkx/networkx/pull/2739
The following code tries to place a label for each node apart from the one that is by default included by NetworkX/Matplotlib. The original positions of the nodes are obtained through the call to "nx.spring_layout(g)".
The problem is that, when it comes to draw with Matplotlib the labels, the latter are misplaced, as it can be seen in the attached graph.
Should I be doing something differently?
import logging
import networkx as nx
import matplotlib.pyplot as plt
__log = logging.getLogger(__name__)
g = nx.Graph()
nodes = ['shield', 'pcb-top', 'pcb-config', 'chassis']
for k in nodes:
g.add_node(k)
plt.figure(figsize=(8, 11), dpi=150)
nx.draw(g, with_labels=True)
node_cfg = nx.spring_layout(g)
for k, node in node_cfg.items():
__log.debug('node = %s #(%.6f, %.6f)', k, node[0], node[1])
plt.text(node[0], node[1], k, bbox={'color': 'grey'})
plt.savefig('test.png')
Use the same position information for the network drawing as for the labels.
node_cfg = nx.spring_layout(g)
plt.figure(figsize=(8, 11), dpi=150)
nx.draw(g, pos=node_cfg, with_labels=True)
I want to build a directed graph and subscribe edges.
import os
import scipy as sc
import pylab
import networkx
import matplotlib.pyplot as plt
from networkx import *
from numpy import *
G=networkx.DiGraph()
R=[('S0','S1'),('S1','S2'),('S1','S7'),('S2','S3'),('S2','S6'),('S3','S4'),('S3','S6'),('S4','S5'),('S5','S6'),('S6','S7'),('S7','S8'),('S7','S5'),('S8','Sk') ]
G.add_edges_from([ (2,3,) ])
G.add_edges_from(R)
networkx.draw_circular(G)
plt.show()
plt.savefig("path.png");
Now I have done this. I built a graph, but I cannot think up how to subscribe edges. For example I want to mark S0 and S1 edge like "565", etc. It will make it more visual and demostrative.
Thanks in advance!
Instead of layouting and drawing in one single step (networkx.draw_circular(G)), you can layout and draw nodes, edges, node labels and edge labels separately. Here's a small example:
import networkx as nx
import matplotlib.pyplot as plt
G=nx.DiGraph()
R=[('S0','S1'),('S1','S2'),('S1','S7'),('S0','S7')]
G.add_edges_from(R)
# Calculate layout and get all positions
pos = nx.circular_layout(G)
# Draw everything
nx.draw_networkx_nodes(G, pos)
nx.draw_networkx_edges(G, pos)
nx.draw_networkx_labels(G, pos)
nx.draw_networkx_edge_labels(G, pos,
{
('S0', 'S1'): 'edge1',
('S1', 'S2'): 'edge2',
('S1', 'S7'): 'edge3',
('S0', 'S7'): 'edge4'
}
)
plt.axis('off')
plt.savefig("path.png");
plt.show()
For more information about what parameters can be passed to the different drawing functions, check the documentation.
You can draw nodes and egdes selectively with:
# nodes
networkx.draw_networkx_nodes(graph, pos, nodelist=[list of the nodes])
# edges
networkx.draw_networkx_edges(graph, pos, edgelist=[list of edges])
There are more options at http://networkx.lanl.gov/reference/drawing.html#module-networkx.drawing.nx_pylab
well, I wanted to do this:
and I did it. i.e. I wanted to mark edges. It seems simple, but it wasn't so. Really.
full image is here http://s019.radikal.ru/i603/1204/2a/921bc6badfae.png
import os
import scipy as sc
import pylab
import networkx
import matplotlib.pyplot as plt
from networkx import *
from numpy import *
G=networkx.DiGraph()
R=[('S0','S1'),('S1','S2'),('S1','S7'),('S2','S3'),('S2','S6'),('S3','S4'),('S3','S6'),('S4','S5'),('S5','S6'),('S6','S7'),('S7','S8'),('S7','S5'),('S8','Sk') ]
G.add_edges_from(R)
label={R[1]:'a',R[2]:'b',R[3]:'c'}
networkx.draw_networkx_edge_labels(G,pos=networkx.graphviz_layout(G),edge_labels=label)
networkx.draw_graphviz(G)
plt.show()
plt.savefig("path.png");