netgraph interactive graph not working when changing shape in jupyter notebook - python-3.x

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)

Related

Why will Seaborn function 'regplot' not run in Jupyter?

I am having trouble with code Seaborn regplot function in Jupyter notebooks using Watson-Studio.
Using Python 3.6, the code appears to get stuck whilst processing, and this happens until I stop the code.
When I run this using IDLE on my Mac, the code runs perfectly and the plot shows.
Seems to happen with plots lmplot and regplot, however boxplots etc do show as normal.
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
df = pd.read_csv(csv.csv)
sns.regplot(x = 'independent', y = 'dependent', data = df)
The expected results should be a graph of the linear relationship between the two variables, however I am just getting a loading bar.
When I stop running the kernel, the graph exists as a scatterplot with no line of best fit. Of course this has the error in notebook as 'Keyboard Interrupted'.
Could this possibly be a bug? Thanks for your help.
Set ci parameter to none and it will solve your problem.
sns.regplot(x = 'independent', y = 'dependent', data = df, ci = None)

Why do edges not show up correctly in networkx drawing

I am using the latest networkx on Python3 with Linux Mint 17. I am using the pcalg method (https://github.com/keiichishima/pcalg) to build directed acyclic graphs from data. I checked to be sure that the graph has edges using the number_of_edges() function.
My code looks like this:
skel_1, sep_1 = alg.estimate_skeleton(ci.ci_test_dis, Mat_1, 0.1)
dag_1 = alg.estimate_cpdag(skel_1, sep_1)
num_edge1 = dag_1.number_of_edges()
print(num_edge1)
nx.draw_networkx(dag_1, arrows=True, edge_color='b')
plt.savefig("Target1.png")
plt.close()
When I execute this code, the nodes show up, but not the edges. Is this because I did not include the pos parameter?

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?

NetworkX node attribute drawing

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()

Resources