/
sc_plot.py
82 lines (76 loc) · 2.92 KB
/
sc_plot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
'''
Meiyi Ma, Timothy Davison, Eli Lifland
9/11/18
Two methods for plotting a SC graph using the library geoplotlib.
Includes a sample plot.
'''
import osmnx as ox
import pandas as pd
import geoplotlib
import random
import matplotlib.pyplot as plt
from collections import defaultdict
'''
Loads in nodes according to their coordinates, edges connecting those coordinates
to a pandas dataframe. That dataframe is passed to/ plotted with geoplotlib.
Green is used to represent nodes (using the tf_satisfied attribute of the node)
which are satisfied, and red to represent those unsatisfied.
'''
def plot(graph,tag_to_color,directed=True):
color_to_nodes_data = defaultdict(list)
color_to_nodes_pois = defaultdict(list)
plot_edges = list()
for node in graph.nodes:
node_info = {'lon':node.coordinates[1], 'lat':node.coordinates[0]}
color = (0,0,0)
if len(node.tags):
if node.tags[0] in tag_to_color:
color = tag_to_color[node.tags[0]]
else:
continue
if node.data_node:
color_to_nodes_data[color].append(node_info)
else:
color_to_nodes_pois[color].append(node_info)
neighbors = node.successors
if not directed:
neighbors = neighbors.union(node.predecessors)
for successor in neighbors:
#if node.coordinates[1] == successor.coordinates[1] and node.coordinates[0] == successor.coordinates[0]:
# print(node)
# print(successor)
plot_edges.append({'start_lon':node.coordinates[1], 'end_lon':successor.coordinates[1],
'start_lat':node.coordinates[0], 'end_lat':successor.coordinates[0]})
df_edges = pd.DataFrame(plot_edges)
'''
if not df_edges.empty:
geoplotlib.graph(df_edges,
src_lat='start_lat',
src_lon='start_lon',
dest_lat='end_lat',
dest_lon='end_lon',
color='Dark2',
alpha=30,
linewidth=3)
'''
for color, nodes_list in color_to_nodes_pois.items():
nodes_df = pd.DataFrame(nodes_list)
color = list(color)
color.append(255)
geoplotlib.dot(nodes_df,color=color)
for color, nodes_list in color_to_nodes_data.items():
nodes_df = pd.DataFrame(nodes_list)
color = list(color)
color.append(255)
geoplotlib.dot(nodes_df,color=color)
geoplotlib.show()
def plot_param(city,day,param):
df = pd.read_csv('data/{c}/{d}/{p}'.format(c=city,d=day,p=param),index_col=0)
node_id = df.columns[1]
plt.title('{p} on {d}'.format(p=param,d=day))
plt.plot(df.index,df[node_id])
plt.show()
def plotTwoGroups(coords1,coords2):
geoplotlib.dot(coords1,color=(255,0,0,255))
geoplotlib.dot(coords2,color=(0,255,0,255))
geoplotlib.show()