In [ ]:
import matplotlib.pyplot as plt #for vizualizations
import numpy as np # for arrays , maybe not necessary for this class day
import pandas as pd # for dataframes
import networkx as nx # for networks
In [ ]:
#read the data
df=pd.read_csv('social_media.csv')
df.head()
Out[ ]:
user_name follows_who days
0 Anne Carmen 200
1 Betty Carmen 20
2 Carmen Dillan 30
3 Jessica Carmen 100
4 Dillan Carmen 50
In [ ]:
#create an empty (initialize the network)
G2= nx.DiGraph()
In [ ]:
#assign nodes and edges to G2 from the dataframe df
G2 = nx.from_pandas_edgelist(df, source='user_name', target='follows_who',
                             edge_attr='days')
In [ ]:
G2.nodes()
Out[ ]:
NodeView(('Anne', 'Carmen', 'Betty', 'Dillan', 'Jessica', 'Keth'))
In [ ]:
#print the edge weights based on G2
weights = list(nx.get_edge_attributes(G2, 'days').values())
weights = [i/30 for i in weights]
weights
Out[ ]:
[6.666666666666667,
 1.0,
 0.6666666666666666,
 1.6666666666666667,
 3.3333333333333335,
 3.3333333333333335]
In [ ]:
#degree
degree_info= nx.degree(G2)
degree_info
Out[ ]:
DegreeView({'Anne': 2, 'Carmen': 4, 'Betty': 1, 'Dillan': 3, 'Jessica': 1, 'Keth': 1})
In [ ]:
degree_values= [number*300 for user, number in degree_info]
degree_values
Out[ ]:
[600, 1200, 300, 900, 300, 300]
In [ ]:
#create the visualization
plt.figure(figsize=(6,4))
pos_G2 = nx.spring_layout(G2)
nx.draw_networkx_nodes(G2, pos_G2, node_size= degree_values, node_color= degree_values,
                       cmap='Wistia_r', alpha=0.5)
nx.draw_networkx_edges(G2, pos_G2, edge_color=weights, width=weights,arrows=True,
                       connectionstyle='arc3, rad=0.2', min_source_margin=0, min_target_margin=20)
nx.draw_networkx_labels(G2, pos_G2, font_size=10)
plt.show()
No description has been provided for this image
In [ ]: