-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.py
More file actions
140 lines (107 loc) · 4.64 KB
/
plot.py
File metadata and controls
140 lines (107 loc) · 4.64 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import matplotlib.pyplot as plt
import numpy as np
from io import BytesIO
from datetime import datetime
from dateutil import relativedelta
import pytz
sites_color = {
'playstation': '#ffcc00',
'wikipedia': '#ab00ff',
'bing': '#ff27b8',
'google': '#ff6964',
'github': '#ce0000',
'aparat': '#15fdef',
'filimo': '#2785ff',
'digikala': '#08cf29'
}
class RadarPlot:
def __init__(self, data, start_time=None):
self._start_time = start_time
if not self._start_time:
self._start_time = datetime.now(tz=pytz.timezone('Asia/Tehran')) - relativedelta.relativedelta(hours=6)
self.datacenter_names = []
self.site_values = []
for site in data:
self.datacenter_names.append(site[0].replace('_', ' '))
self.site_values.extend([list(site[1].values())])
self.site_names = [[values for values in data[0][1].keys()]] * len(self.datacenter_names)
print(self.datacenter_names)
print(self.site_names)
print(self.site_values)
def make_plot_1(self):
"""
heavy and detailful plot
"""
num_plots = len(self.site_names)
with plt.style.context('dark_background'):
fig, axes = plt.subplots(num_plots, 1, figsize=(15, 2 * num_plots), squeeze=False)
for i in range(num_plots):
ax = axes[i, 0]
ax.set_ylabel(self.datacenter_names[i])
max_length = max(len(arr) for arr in self.site_values[i])
bottom = np.zeros(max_length)
colors = [sites_color[color] for color in self.site_names[0]]
for j, dc in enumerate(self.site_names[i]):
current_arr = self.site_values[i][j]
current_arr += [0] * (max_length - len(current_arr))
ax.bar(range(max_length), current_arr, bottom=bottom, label=dc, color=colors[j])
bottom += current_arr
if i == num_plots - 1:
np_range = np.arange(0, max_length, 20)
minute = int(30 / len(np_range))
column_date = [self._start_time]
for state in range(len(np_range) - 1):
column_date.append((column_date[-1] + relativedelta.relativedelta(minutes=minute)))
ax.set_xticks(np_range)
ax.set_xticklabels([column.strftime('%H:%M') for column in column_date])
ax.legend(loc='upper right', bbox_to_anchor=(1.05, 1))
else:
ax.set_xticks([])
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.tight_layout()
byte = BytesIO()
plt.savefig('o.png')
fig.savefig(byte, format='png')
plt.close(fig)
byte.seek(0)
# plt.show()
return byte.getvalue()
def make_plot_2(self):
"""light"""
ziped_data = [list(zip(*values)) for values in self.site_values]
avg_data = [[round(sum(group) / len(group), 2) for group in ziped] for ziped in ziped_data]
max_length = max(len(data) for data in avg_data)
padded_data = [np.pad(data, (0, max_length - len(data)), 'constant', constant_values=np.nan) for data in
avg_data]
x_values = list(range(max_length))
num_plots = len(padded_data)
num_cols = 2
num_rows = (num_plots + num_cols - 1) // num_cols
fig, axs = plt.subplots(num_rows, num_cols, figsize=(12, 2 * num_rows))
for i, y in enumerate(padded_data):
row = i // num_cols
col = i % num_cols
ax = axs[row, col]
ax.plot(x_values, y)
ax.set_title(self.datacenter_names[i])
ax.set_ylim([0, 1])
if i == num_plots - 2 or i == num_plots - 1:
np_range = np.arange(0, max_length, 20)
minute = int(380 / len(np_range))
column_date = [self._start_time]
for state in range(len(np_range) - 1):
column_date.append((column_date[-1] + relativedelta.relativedelta(minutes=minute)))
ax.set_xticks(np_range)
ax.set_xticklabels([column.strftime('%H:%M') for column in column_date], rotation='vertical')
else:
ax.set_xticks([])
plt.subplots_adjust(hspace=0.5)
plt.tight_layout()
byte = BytesIO()
plt.savefig('o.png')
fig.savefig(byte, format='png')
plt.close(fig)
byte.seek(0)
# plt.show()
return byte.getvalue()