I have two csv files. names.csv is containing name of person and its corresponding node and nodelinks.csv file is containing the link weight between nodes(persons). nodelinks.csv contains information about how many times a person calls other person(how many times is represented as weight column).
I want to create a network which is divided into sub-networks according to leaders, followers, marginals, outliers and bridges in the network.
I searched internet and I found out networkx library in python. So I tried networkx and it gave me an output of the whole network but it is very clustered i.e. nodes are drawn on top of each other in the output. I'd like to get an output of the network that can be easily understood and also i want to find out sub-networks, leaders, followers, marginals, outliers and bridges in that network.
What I've tried so far
import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt
df = pd.read_csv('Nodelinks.csv')
df.columns = ['Source', 'Destination', 'Link']
df.head()
graph = nx.from_pandas_edgelist(df, source = 'Source', target =
'Destination', edge_attr = 'Link',create_using = nx.DiGraph())
plt.figure(figsize = (10,9))
nx.draw(graph, node_size=1200, node_color='lightblue',
linewidths=0.25, font_size=10, font_weight='bold', with_labels=True,
dpi=1000)
plt.show()
Install networkx library using pip or conda.
I tried using pip but it was showing me error. I tried to install it using conda and it worked.
The dataset and jupyter notebook is uploaded on mega.
I don't know how I should proceed next to get what I want as the output. Also, is there any other way to go about this topic?(preferably easier way if there is one)
Related
I was working with cdlib and detecting my data communities. but I need clusters of communities. how can I pick data from this object?
from cdlib import algorithms
import networkx as nx
coms = algorithms.louvain( G )
print(coms.data)
this is result of print(coms)
<cdlib.classes.node_clustering.NodeClustering object at 0x7fe46ec70ac0>
I found it. but not sure it's good to do. but worked.
We can save it using
from cdlib import readwrite
readwrite.write_community_csv(coms, "communities.csv", ",")
then read again communities.csv dataset and use clusters data
import leidenalg as la
import igraph as ig
G = ig.Graph.Famous('Zachary')
partition = la.find_partition(G, la.ModularityVertexPartition)
ig.plot(partition,vertex_size = 30)
ig.save(G,'ttt.gml')
Everything works fine, however ig.save does not contain cluster information, just nodes and edges.
Need to add Cluster information to nodes in ttt.gml file
The graph itself doesn't contain any information of the partition. You should first add this information to the graph before saving it by doing G.vs['cluster'] = partition.membership.
I am trying to plot a tree from a Pandas dataframe but I don't know which is the correct data structure I must use and how can I solve it.
The dataset has 4 columns: source, destination, application and timemark.
For example, a row in the dataset could be:
192.168.1.1 | 192.168.1.200 | ping | 10:00AM
I would like to plot a tree graph generating a node for each of the sources, and showing the adjacency of each source with the destinations who has communicated with, and an adjacency of each destination with all the applications that this destination has used with the source and finally showing the adjacency of each (source, destination, application) leaf, with all the timemarks of the sessions that used this application between this destination and this source.
Could you please tell me how can I find a Python solution for this?
Thanks a lot!
You should look at NetworkX:
"NetworkX is a Python package for the creation, manipulation, and study of the structure, dynamics, and functions of complex networks."
You can feed your dataset to populate a graph and then plot the graph.
For example:
import networkx as nx
import matplotlib.pyplot as plt
G=nx.Graph()
G.add_node('192.168.1.1')
G.add_node('192.168.1.100')
G.add_node('192.168.1.200')
G.add_edge('192.168.1.1', '192.168.1.100', object='10')
G.add_edge('192.168.1.1', '192.168.1.200', object='11')
nx.draw_networkx(G, pos = nx.shell_layout(G))
nx.draw_networkx_edge_labels(G, pos = nx.shell_layout(G))
plt.show()
would give you:
My task is to generate a graph from a dot file (using pydot) and then convert the same as networkx graph. The problem that I faced was that the attributes of Graph (as I have given in the .dot file) is not present in the networkx graph.
I also used read_dot() function which is again an error. My code is successfully working to visualize graphs but not its attribs.
My code is:
import pydot
import networkx as nx
(graph,) = pydot.graph_from_dot_file('1.dot')
G = nx.nx_pydot.from_pydot(graph)
nx.get_node_attributes(G,'1')
My output is {}
Pls help me to fix the problem
Thanks from Mathan :)
I am using networkX to draw a network plot from a distance matrix(emoji_sim, a DataFrame). Here is the code:
G = nx.from_numpy_matrix(np.array(emoji_sim))
nx.draw(G, edge_color='silver', node_color='lightsalmon', with_labels=True)
plt.show()
I know there is a way to relabel the nodes as:
G = nx.relabel_nodes(G, dict(zip(range(len(G.nodes())), range(1, len(G.nodes())+1))))
But I want to substitute the nodes label with images(possibly read from files or using Python Emoji package). Is there any way to do that? Thanks a lot!
To clarify, I am trying to substitute the actual circle with images.
The idea behind it is not very difficult but in order to get it to be displayed (at least on ubunto) it gave me some hard time as not all fonts support emoji. I shall display the straight forward way then some links that helped me in the end (maybe you will not need those).
From emoji cheat sheet from the emoji python package I picked up three to be shown as an example and here is the code.
G = nx.Graph()
G.add_nodes_from([0,1,2])
n0 = emoji.emojize(':thumbsup:',use_aliases=True)
n1 = emoji.emojize(':sob:',use_aliases=True)
n2 = emoji.emojize(':joy:',use_aliases=True)
labels ={0:n0,1:n1,2:n2}
nx.draw_networkx(G,labels=labels, node_color = 'w', linewidths=0, with_labels=True, font_family = 'Symbola' ,font_size = 35)
plt.show()
Difficulties encountered:
1- My machine is on ubunto 14.04, I could not display any emoji they always appeared as rectangles
Installed needed font Symbola using the following command (mentioned here):
sudo apt-get install ttf-ancient-fonts
2- Maplotlib (which networkx calls to draw) is not using the installed font.
From several useful discussions 1 2 3 4 5 6 I copied and pasted the .tff font file of Symbola in the default matplotib directory (where it fetches for fonts to use).
cp /usr/share/fonts/truetype/ttf-ancient-scripts/Symbola605.ttf /usr/share/matplotlib/mpl-data/fonts/ttf
Then I had to delete fontList.cache file for the new font to be loaded.
rm ~/.cache/matplotlib/fontList.cache
Note
You can have different views by changing the input to the draw_networkx e.g. not sending the linewidths will show circular border for each node, also if you want a specific background color for nodes change the color_node from white to a color that you want ... for more details check the documentation.