NetworkX node attribute drawing - attributes

Im using networkx for visualization. I see when I use the function
draw_networkx_edge_labels
I can retrieve the labels for edges.
I want to print the attribute on node ( instead of the label).. try everything almost . still stuck. If i have 5 attributes per node, is there anyway I can print a specific attribute on each node ? For example, if a car node has attributes: size, price, company, .. I want to print the size of the car on each node ?
Don't know whether can output this on graph.

You can do it by specifying the labels= keyword
import pylab
import networkx as nx
G=nx.Graph()
G.add_node('Golf',size='small')
G.add_node('Hummer',size='huge')
G.add_edge('Golf','Hummer')
labels = nx.get_node_attributes(G, 'size')
nx.draw(G,labels=labels,node_size=1000)
pylab.show()

Related

netgraph interactive graph not working when changing shape in jupyter notebook

I created a graph with networkx assigning properties to each node. Then, I used netgraph to display my graph in Jupiter Notebook (Python 3.5) as an Interactive graph (using netgraph.InteractiveGraph), the problem is that when I want to use a different shape for different nodes, the nodes aren't moving unless they all are the same figure.
If anyone knows how to solve this, please let me know!
Below is my code. Thank you!
#Graph creation:
G=nx.Graph(type="")
for i in range(6):
G.add_node(i,shape="o")
#Changing shape for two nodes
G.node[1]['shape'] = "v"
G.node[5]['shape'] = "v"
#Add edges
G.add_edge(1,2)
G.add_edge(4,5)
G.add_edge(0,4)
G.add_edge(2,3)
G.add_edge(2,4)
#Get node shapes
node_shapes = nx.get_node_attributes(G,"shape")
%matplotlib notebook
plot_instance = netgraph.InteractiveGraph(G, node_shape=node_shapes)

Networkx - exporting graphml with edge labels, height and width attributes, custom images

I want to automate a network topology diagram using python. I'm new to python so please bear with me. After doing some research I found out that I can use python to create graphml files which can be read by yEd.
I'm learning how to use Networkx to create the graphml files. So far I'm able to create nodes, connect them and add labels to the nodes (these labels would be the hostnames). Now I need to know how I can add labels to the edges (these labels would be the interfaces). For example:
Topology example
If possible I would like to know how to add a custom image for every node (by default the shape is a square but I would like to use a router png file).
If it is not possible then it would be helpful to know how to edit the height and width of the shape and also disabling arrows.
I've reviewed the docs on networkx website but I haven't found how to do these changes directly to the graph object. The only way I've seen it done is when drawing the graph, for example using the following function: nx.draw_networkx_labels(G, pos, labels, font_size=15, arrows=False), but this is not what I need because this is not saved to the graphml file.
If someone can guide me through this it would be really helpful, I'm attaching my code:
import networkx as nx
import matplotlib
import matplotlib.pyplot as plt
g = nx.DiGraph()
g.add_node('Hostname_A')
g.add_node('Hostname_B')
g.add_node('Hostname_C')
g.add_node('Hostname_D')
g.add_edge('Hostname_A','Hostname_B')
g.add_edge('Hostname_A','Hostname_C')
g.add_edge('Hostname_B','Hostname_D')
g.add_edge('Hostname_B','Hostname_C')
for node in g.nodes():
g.node[node]['label'] = node
nx.readwrite.write_graphml(g, "graph.graphml")
This is the solution:
for edge in g.edges():
g.edges[edge]['source'] = 'int gi0/0/0'
g.edges[edge]['destination'] = 'int gi0/0/1'

how can i hover network node attribute (using bokeh and networkx under python 3.5)

i used networkx and bokeh to generate a interactive network graph. I would like the attributes of nodes hover while the mouse on the node.
I used the code
layout = nx.spring_layout(network,
k=1.1/sqrt(network.number_of_nodes()),
iterations=100)
from bokeh.models import ColumnDataSource
nodes, nodes_coordinates = zip(*sorted(layout.items()))
nodes_xs, nodes_ys = list(zip(*nodes_coordinates))
nodes_source = ColumnDataSource(dict(x=nodes_xs,
y=nodes_ys,
name=nodes,
))
However, i also have some node attribute in the network data. i would like to hover those nodes attributes.
anyone can help?

Getting an object in Python Matplotlib

To make a plot, I have written my code in the following fashion:
from pylab import *
x = [1,2,3]
y = [1,2,3]
matplotlib.pyplot.scatter(x,y,label='Blah')
matplotlib.pyplot.legend(title='Title')
matplotlib.pyplot.show()
I want to change the font size of the legend title. The way to go about this is to get the legend object and then change the title that way (e.g., How to set font size of Matplotlib axis Legend?)
Instead of rewriting all my code using ax.XXX, figure.XXX, etc, is there any way to get at the legend object from the code I have written, and then go from there?
That is to say, how do I define
Legend
from my original piece of code, such that
Title = Legend.get_title()
Title.set_fontsize(30)
would get at the title object and then allow me to play with .get_title()?
I think I'm on the verge of a eureka moment regarding object-orientated languages. I have a feeling a good answer will give me that eureka moment!
cheers,
Ged
First, in your code you should stick to using either from pylab import * and then use the imported methods directly, or import matplotlib.pyplot as plt and then plt.* instead of matplotlib.pyplot.*. Both these are "conventions" when it comes to working with matplotlib. The latter (i.e. pyplot) is generally preferred for scripting, as pylab is mainly used for interactive plotting.
To better understand the difference between pylab and pyplot see the matplotlib FAQ.
Over to the problem at hand; to "get" an object in Python, simply assign the object to a variable.
from pylab import *
x = [1,2,3]
y = [1,2,3]
scatter(x,y,label='Blah')
# Assign the Legend object to a variable leg
leg = legend(title='Title')
leg_title = leg.get_title()
leg_title.set_fontsize(30)
# Optionally you can use the one-liner
#legend(title='Title').get_title().set_fontsize(30)
show()
Visual comparison (rightmost subplot produced with the above code):

Make a visual representation of a graph

I have a list of edges.
(1,2),(1,3),(1,4),(1,5),(1,6),(2,4),(2,7),(3,4),(3,7),(4,5),(4,7),(5,6),(6,7)
How can I get an image of this graph?
It should be automatic, because there are over 9000(not kidding) those lists.
I have always used graphviz for this sort of stuff.
You can draw it with Python and networkx.
import networkx
import pylab
edges = [(1,2),(1,3),(1,4),(1,5),(1,6),(2,4),(2,7),(3,4),(3,7),(4,5),(4,7),(5,6),(6,7)]
G = networkx.Graph(data=edges)
networkx.draw(G)
pylab.show()
You should read pylab's documentation on how to save the graph as an image without using the GUI. You can use ast.literal_eval to parse the original lists. For example, if it stored as one graph on a line in a file, you can do:
with open('edges.txt') as f:
for line in f:
edges = list(ast.literal_eval(line))
# drawing goes here

Resources