In [1]:
import matplotlib
%matplotlib inline
In [2]:
import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt
We are adding the adjacency matrices for each of the confidence levels so we can get a weight for the connections bewtween the petitions. For example, a value of 4 in the adjaceny matrix means that it is at the 40% confidence level.
In [3]:
petition50 = pd.read_csv('../data/adjacency_matrix_50_percent_confidence.csv')
petition40 = pd.read_csv('../data/adjacency_matrix_40_percent_confidence.csv')
petition30 = pd.read_csv('../data/adjacency_matrix_30_percent_confidence.csv')
petition20 = pd.read_csv('../data/adjacency_matrix_20_percent_confidence.csv')
petition10 = pd.read_csv('../data/adjacency_matrix_10_percent_confidence.csv')
petitions = petition50 + petition40 + petition30 + petition20 + petition10
adjacency_matrix = petitions.T.values[1:]
petitions['Unnamed: 0'] = petition10['Unnamed: 0']
petition_ids = petitions['Unnamed: 0']
petition_labels = {i:x for i,x in petition_ids.iteritems()}
In [4]:
print petitions
In [5]:
print petition10
In [6]:
graph = nx.from_numpy_matrix(adjacency_matrix)
In [7]:
categories_colors = pd.read_csv('../data/petitions_groups_colors.csv')
In [8]:
color_table = [ [row[0],row[1],row[2]] for i,row in categories_colors.iterrows()]
In [9]:
petitions_categories_table = pd.read_csv('../data/petitions_grouping.csv')
petitions_categories = { row[0]:row[1] for i,row in petitions_categories_table.iterrows() }
In [10]:
node_colors_array = [ color_table[petitions_categories[pid]] for pid in petition_ids ]
In [11]:
edge_weight = [adjacency_matrix[int(line.split()[0])][int(line.split()[1])] for line in nx.generate_edgelist(graph, data = False)]
Nodes in this graph represent petitions. The edges represent the co-signing relationship. The color of the edges represents the strength of the relationship. The darker the edge, the more co-signatures there are:
In [12]:
plt.figure(num=2, figsize=(25,25))
positions = nx.spring_layout(graph,k=0.7,scale=10)
nodes = nx.draw_networkx_nodes(graph,positions,node_color = node_colors_array, node_size=1000)
labels = nx.draw_networkx_labels(graph,positions,labels = petition_labels)
edges = nx.draw_networkx_edges(graph,positions,edge_cmap = plt.get_cmap('binary'),edge_vmin = 0,edge_vmax = 5,edge_color = edge_weight, width = 3)
plt.axis('off')
plt.sci(nodes)
plt.sci(edges)
plt.show()
In [13]:
posframe = pd.DataFrame(positions)
posframe.T.to_csv('../data/graph_positions.csv')
In [14]:
posframe2 = pd.read_csv('../data/graph_positions.csv')
positions2 = {i:array([row[0],row[1]]) for i,row in posframe2.iterrows()}
print positions2
print positions
print posframe2
In [15]:
plt.figure(num=3, figsize=(25,25))
#positions = nx.spring_layout(graph,k=0.7,scale=10)
nodes = nx.draw_networkx_nodes(graph,positions2,node_color = node_colors_array, node_size=1000)
labels = nx.draw_networkx_labels(graph,positions2,labels = petition_labels)
edges = nx.draw_networkx_edges(graph,positions2,edge_cmap = plt.get_cmap('binary'),edge_vmin = 0,edge_vmax = 5,edge_color = edge_weight, width = 3)
plt.axis('off')
plt.sci(nodes)
plt.sci(edges)
plt.show()
In [16]:
for line in nx.generate_edgelist(graph, data = False):
print(line)
In [17]:
graph.number_of_edges()
Out[17]:
In [18]:
edge_weight = [adjacency_matrix[int(line.split()[0])][int(line.split()[1])] for line in nx.generate_edgelist(graph, data = False)]
In [19]:
print adjacency_matrix[0][2]
In [20]:
print edge_weight
In [20]:
In [20]: