root / globecomm / custom_plot.py @ fac6e5a4
History  View  Annotate  Download (3.25 KB)
1 
import numpy as np 

2 
import matplotlib.pyplot as plt 
3  
4 
def plot_time_diff(time_diff, scatter=False, title='', xlabel='', ylabel='', ylim=None, out_filepath=''): 
5 
"""Helper function for percentage_overlap()

6 
"""

7 
tw_keys = sorted(time_diff.keys())

8  
9 
arr = np.linspace(0, 1, len(tw_keys)) 
10 
colors = get_colors_for_array(arr, 'hsv')

11  
12 
for index, k in enumerate(tw_keys): 
13 
values = time_diff[k] 
14 
if scatter:

15 
plt.scatter(range(len(values)), values, color=colors[index], s=2, label=k) 
16 
else:

17 
plt.plot(range(len(values)), values, label=k) 
18  
19 
plt.ylabel(ylabel) 
20 
plt.xlabel(xlabel) 
21 
if ylim:

22 
plt.ylim(ylim) 
23 
plt.legend() 
24 
plt.title(title) 
25  
26 
if out_filepath:

27 
plt.savefig(out_filepath) 
28 
plt.close() 
29 
else:

30 
plt.show() 
31  
32 
def plot_single_time_diff(time_diff, scatter=False, title='', xlabel='', ylabel='', ylim=None, out_filepath=''): 
33 
if scatter:

34 
plt.scatter(range(len(time_diff)), values, s=2) 
35 
else:

36 
plt.plot(range(len(time_diff)), time_diff) 
37  
38 
plt.ylabel(ylabel) 
39 
plt.xlabel(xlabel) 
40 
if ylim:

41 
plt.ylim(ylim) 
42 
plt.legend() 
43 
plt.title(title) 
44  
45 
if out_filepath:

46 
plt.savefig(out_filepath) 
47 
plt.close() 
48 
else:

49 
plt.show() 
50  
51  
52 
def plot_time_diff_window(time_diff, window=1, title='', xlabel='', ylabel='', ylim=None, out_filepath=''): 
53 
"""Plots only one row of time_diff, coressponding to window = 1.

54 

55 
It shows the fluctation with times

56 
"""

57 
data = time_diff[window] 
58 
x_range = range(len(data)) 
59 
plt.scatter(x_range, data) 
60 
if ylim:

61 
plt.ylim(ylim) 
62  
63 
if out_filepath:

64 
plt.savefig(out_filepath) 
65 
plt.close() 
66 
else:

67 
plt.show() 
68  
69 
def plot_time_diff_evolvement(time_diff, window=1, title='', xlabel='', ylabel='', ylim=None, out_filepath=''): 
70 
"""Plot shows the percentage_overlap of between the first snapshot and other subsequent snapshots

71 
"""

72 
plot_data = [0 for i in range(len(time_diff.keys()))] 
73 
for k, values in time_diff.iteritems(): 
74 
plot_data[k1] = values[window1] 
75  
76 
x_range = range(1, len(plot_data) + 1) 
77 
plt.scatter(x_range, plot_data) 
78  
79 
if ylim:

80 
plt.ylim(ylim) 
81  
82 
if out_filepath:

83 
plt.savefig(out_filepath) 
84 
plt.close() 
85 
else:

86 
plt.show() 
87  
88  
89 
def scatter_histogram(values, out_filepath): 
90 
"""Shows (or saves the figure) of the scater plot of the BC scores

91 
for each node.

92 
Xaxis the is node

93 
Each dot on the graph is the BC score of one snapshots.

94 
"""

95 
num_of_rows = len(values)

96 
num_of_cols = len(values[0]) 
97  
98 
bc_min = np.nanmin(values, axis=0)

99 
bc_max = np.nanmax(values, axis=0)

100 
bc_mean = np.nanmean(values, axis=0)

101  
102 
sorted_indices = sorted(range(num_of_cols), key = lambda k : bc_max[k]) 
103  
104 
x_range = range(num_of_cols)

105 
for i in range(num_of_rows): 
106 
plt.scatter(x_range, [values[i][j] for j in sorted_indices], s=1) 
107  
108 
# plt.plot(x_range, [bc_min[j] for j in sorted_indices])

109 
plt.plot(x_range, [bc_mean[j] for j in sorted_indices]) 
110 
# plt.plot(x_range, [bc_max[j] for j in sorted_indices])

111 
if out_filepath:

112 
plt.savefig(out_filepath) 
113 
else:

114 
plt.show() 
115  
116 
def get_colors_for_array(arr, color_map_name): 
117 
return plt.get_cmap(color_map_name)(arr)
