-
Notifications
You must be signed in to change notification settings - Fork 0
/
plots.py
390 lines (361 loc) · 11.9 KB
/
plots.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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
from datetime import timedelta
import plotly.graph_objects as go
import pandas as pd
#colors
blue_100_color = 'rgba(0,101,159,1)'
blue_75_color = 'rgba(0,101,159,0.75)'
blue_50_color = 'rgba(0,101,159,0.5)'
blue_25_color = 'rgba(0,101,159,0.25)'
red_100_color = 'rgba(181,44,56,1)'
yellow_100_color = 'rgba(255,149,43,1)'
axis_color = 'rgba(204,204,204,1)'
axis_grid_color = 'rgba(204,204,204,0.5)'
text_color = 'rgba(119,119,119,1)'
background_color = 'rgba(255,255,255,1)'
def CreateBoxChart(df, output_name, data_name, category, area, start_date, end_date):
"""Creates a categorized box chart with data filtered to specifications.
Args:
df (dataframe): a pandas dataframe that contains the data to use
output_name (str): the name of the file to be output along with it's path inside the images folder
data_name (str): the header for the dataframe column to pull data points from
category (str): the header for the dataframe column to categorize data points on
"""
# Sets the order of the x axis
if category == "Day":
cat_order = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']
elif category == "Hour":
cat_order = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23]
elif category == "Window":
cat_order = ["0-6","6-18","18-24"]
# Generate Title
title_data = {'TEMP F': 'Temperature (F)', 'RH %': 'Relative Humidity (%)'}
title_cat = {'Day': 'Weekday', 'Hour': 'Hour', 'Window': 'Time Window'}
title = f"Distribution of {title_data[data_name]} by {title_cat[category]} ({start_date} - {end_date}) -- {area}"
# Create Chart
fig = go.Figure()
fig.add_trace(
go.Box(
x = df[category],
y = df[data_name],
boxmean = True,
boxpoints = 'outliers',
fillcolor = blue_25_color,
line = dict(
color = blue_100_color,
width = 2,
),
marker = dict(
color = blue_50_color,
line = dict(
color = blue_100_color,
width = 0.2,
),
size = 2,
),
notched = False,
)
)
fig.update_xaxes(
categoryorder = 'array',
categoryarray = cat_order,
linecolor = axis_color,
linewidth = 2,
tickfont = dict(
family = 'Open Sans, Arial, Helvetica, sans-serif',
size = 14,
color = text_color,
),
tickmode = 'linear',
)
fig.update_yaxes(
gridcolor = axis_grid_color,
gridwidth = 1,
linecolor = axis_color,
linewidth = 2,
tickfont = dict(
family = 'Open Sans, Arial, Helvetica, sans-serif',
size = 14,
color = text_color,
),
title = dict(
font = dict(
family = 'Open Sans, Arial, Helvetica, sans-serif',
size = 21,
color = text_color,
),
standoff = 4,
text = title_data[data_name],
),
)
fig.update_layout(
autosize = False,
height = 1080,
paper_bgcolor = background_color,
plot_bgcolor = background_color,
title = dict(
font = dict(
family = 'Open Sans, Arial, Helvetica, sans-serif',
size = 28,
color = text_color,
),
text = title,
x = 0.5,
xanchor = 'center',
),
width = 1920,
)
# Save the image
fig.write_image(output_name + ".png")
def createControlChart(df, output_name, data_name, categories, area, start_date, end_date):
"""Creates a chart showing the change in measured data overtime as well as the control limits for the data set.
Args:
df (dataframe): a pandas dataframe containing rh/temp data
output_name (string): the name of the file to be output
data_name (string): the category of data to be charted
categories (list): the categories by which the dataframe is filtered
"""
# Generate Title
title_data = {'TEMP F': 'Temperature (F)', 'RH %': 'Relative Humidity (%)'}
title_cat = {'Day': 'weekday', 'DATE': 'date', 'Hour': 'hour', 'Window': 'time window'}
title = f"Control Chart of {title_data[data_name]} by {', '.join([title_cat[cat] for cat in categories])} ({start_date} - {end_date}) -- {area}"
# Create Chart
fig = go.Figure()
fig.add_trace(
go.Scatter(
line = dict(
color = red_100_color,
dash = 'dash',
width = 2,
),
mode = 'lines',
name = 'X-UCL',
x = df['idx'],
y = df['UCL'],
)
)
fig.add_trace(
go.Scatter(
line = dict(
color = red_100_color,
dash = 'dash',
width = 2,
),
mode = 'lines',
name = 'X-LCL',
x = df['idx'],
y = df['LCL']
)
)
fig.add_trace(
go.Scatter(
line = dict(
color = yellow_100_color,
dash = 'dot',
width = 2,
),
mode = 'lines',
name = 'X-Bar',
x = df['idx'],
y = df['x-bar']
)
)
fig.add_trace(
go.Scatter(
line = dict(
color = blue_75_color,
width = 3,
),
marker = dict(
color = blue_100_color,
size = 6,
),
mode = 'lines+markers',
name = 'Data',
x = df['idx'],
y = df['avg'],
)
)
fig.update_xaxes(
linecolor = axis_color,
linewidth = 2,
tickfont = dict(
family = 'Open Sans, Arial, Helvetica, sans-serif',
size = 14,
color = text_color,
),
)
fig.update_yaxes(
gridcolor = axis_grid_color,
gridwidth = 1,
linecolor = axis_color,
linewidth = 2,
tickfont = dict(
family = 'Open Sans, Arial, Helvetica, sans-serif',
size = 14,
color = text_color,
),
title = dict(
font = dict(
family = 'Open Sans, Arial, Helvetica, sans-serif',
size = 21,
color = text_color,
),
standoff = 4,
text = title_data[data_name]
),
)
fig.update_layout(
autosize = False,
height = 540,
paper_bgcolor = background_color,
plot_bgcolor = background_color,
title = dict(
font = dict(
family = 'Open Sans, Arial, Helvetica, sans-serif',
size = 28,
color = text_color,
),
text = title,
x = 0.5,
xanchor = 'center',
),
width = 1920,
)
# Save the image
fig.write_image(output_name + "-ControlChart.png")
def createRangeChart(df, output_name, data_name, categories, area, start_date, end_date):
"""Creates a chart showing the magnitude of the changes in measured data for consecutive points over time.
Args:
df (dataframe): a pandas dataframe containing rh/temp data
output_name (string): the name of the file to be output
data_name (string): the category of data to be charted
categories (list): the categories by which the dataframe is filtered
"""
# Generate Title
title_data = {'TEMP F': 'Temperature (F)', 'RH %': 'Relative Humidity (%)'}
title_cat = {'Day': 'weekday', 'DATE': 'date', 'Hour': 'hour', 'Window': 'time window'}
title = f"Range Chart of {title_data[data_name]} by {', '.join([title_cat[cat] for cat in categories])} ({start_date} - {end_date}) -- {area}"
# Create Chart
fig = go.Figure()
fig.add_trace(
go.Scatter(
line = dict(
color = red_100_color,
dash = 'dash',
width = 2,
),
mode = 'lines',
name = 'R-UCL',
x = df['idx'],
y = df['R UCL'],
)
)
fig.add_trace(
go.Scatter(
line = dict(
color = yellow_100_color,
dash = 'dot',
width = 2,
),
mode = 'lines',
name = 'R-Bar',
x = df['idx'],
y = df['r-bar']
)
)
fig.add_trace(
go.Scatter(
line = dict(
color = blue_75_color,
width = 3,
),
marker = dict(
color = blue_100_color,
size = 6,
),
mode = 'lines+markers',
name = 'Data',
x = df['idx'],
y = df['diff'],
)
)
fig.update_xaxes(
linecolor = axis_color,
linewidth = 2,
tickfont = dict(
family = 'Open Sans, Arial, Helvetica, sans-serif',
size = 14,
color = text_color,
),
)
fig.update_yaxes(
gridcolor = axis_grid_color,
gridwidth = 1,
linecolor = axis_color,
linewidth = 2,
tickfont = dict(
family = 'Open Sans, Arial, Helvetica, sans-serif',
size = 14,
color = text_color,
),
title = dict(
font = dict(
family = 'Open Sans, Arial, Helvetica, sans-serif',
size = 21,
color = text_color,
),
standoff = 4,
text = title_data[data_name]
),
)
fig.update_layout(
autosize = False,
height = 540,
paper_bgcolor = background_color,
plot_bgcolor = background_color,
title = dict(
font = dict(
family = 'Open Sans, Arial, Helvetica, sans-serif',
size = 28,
color = text_color,
),
text = title,
x = 0.5,
xanchor = 'center',
),
width = 1920,
)
#Save the image
fig.write_image(output_name + "-RangeChart.png")
def CreateControlRangeCharts(df, output_name, data_name, categories, area, start_date, end_date):#, days=0):
"""Modifies the data frame in order to create control and range charts for given data
Args:
df (dataframe): a pandas dataframe containing rh/temp data
output_name (string): the name of the file to be output
data_name (string): the category of data to be charted
categories (list): the categories by which the dataframe is filtered
"""
# Filter df to include only most recent 'days' days
#if days > 0:
#in_days = df['DATE'] >= df['DATE'].max()-timedelta(days)
#df = df[in_days]
df = pd.DataFrame({'avg' : df.groupby(categories)[data_name].mean()}).reset_index()
df['diff'] = abs(df['avg'].diff(1))
# Variables used for control chart columns
x_bar = df['avg'].mean()
r_bar = df['diff'].mean()
ucl = x_bar + 2.66*r_bar
lcl = max(0, x_bar - 2.66*r_bar)
r_ucl = 3.268*r_bar
# Create cols needed for control charts
df['idx'] = pd.Series([i for i in range(len(df.index))])
df['x-bar'] = pd.Series([x_bar for _ in range(len(df.index))])
df['r-bar'] = pd.Series([r_bar for _ in range(len(df.index))])
df['UCL'] = pd.Series([ucl for _ in range(len(df.index))])
df['LCL'] = pd.Series([lcl for _ in range(len(df.index))])
df['R UCL'] = pd.Series([r_ucl for _ in range(len(df.index))])
# Create Control and Range Chart with modified data frame
createControlChart(df, output_name, data_name, categories, area, start_date, end_date)#, days)
createRangeChart(df, output_name, data_name, categories, area, start_date, end_date)#, days)
if __name__ == "__main__":
pass