-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathplots.py
534 lines (436 loc) · 16.2 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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
import logging
import os
import numpy as np
import pandas as pd
from plotly.offline import iplot, init_notebook_mode
import plotly.graph_objs as go
import plotly.io as pio
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
class Plot(object):
"""Creates and saves plots to visualize and
correlate arrays, usually timeseries
Parameters:
title: str
Plot title
data_headers: list
A list of labels in the same order as the corresponding data
If None the labels will be the df column labels, or integer
indices if a list got provided
label_h: str
Horizontal axis label
label_v: str
Vertical axis label
legend: boolean
Plot the legend or not
save_image: boolean
If True saves the created image with either
a given or default path and filename. Supported
file types are 'png' and 'pdf', as specified in
the filename.
duration_curve: boolean
If True it sorts the columns (df or arrays)
and plots the duration_curve, returns a
duration_curve metric as a real
outpath: string or '' (for current directory)
Path to save the png image of the plot
boxmean: True, False, 'sd', 'Only Mean'
notebook_mode: boolean
Plot in the notebook if True
width: int
Image width
height: int
Image height
fontsize: int
Axis label font size
Returns:
fig: plotly figure if self.interactive
else True
"""
def __init__(
self,
title="",
label_h="Time [h]",
label_v="Component performance",
data_headers=None,
save_image=True,
legend=True,
outpath="",
duration_curve=False,
boxmode="group",
notebook_mode=False,
width=1200,
height=800,
fontsize=28,
legend_x=0.4,
legend_y=1.0,
margin_l=200.0,
margin_b=200.0,
):
self.data_headers = data_headers
self.save_image = save_image
self.outpath = outpath
self.interactive = notebook_mode
self.duration_curve = duration_curve
# plot formatting, see
# https://plot.ly/python/reference/#layout-titlefont
self.layout = go.Layout(
font=dict(size=fontsize, family="arial"),
title=title,
titlefont=dict(size=fontsize * 1.0, family="arial"),
xaxis=dict(
title=label_h,
titlefont=dict(
# family='Courier New, monospace',
size=fontsize,
color="#7f7f7f",
),
tickfont=dict(size=fontsize * 0.8),
),
yaxis=dict(
title=label_v,
titlefont=dict(
# family='Courier New, monospace',
size=fontsize,
color="#7f7f7f",
),
tickfont=dict(size=fontsize * 0.6),
),
showlegend=legend,
width=width,
height=height,
margin=dict(l=margin_l, b=margin_b),
legend=dict(
x=legend_x,
y=legend_y,
font=dict(family="arial", size=fontsize * 0.8),
),
)
self.boxlayout = go.Layout(
font=dict(size=fontsize, family="arial"),
title=title,
titlefont=dict(size=fontsize * 1.2, family="arial"),
xaxis=dict(
title=label_h,
titlefont=dict(
# family='Courier New, monospace',
size=fontsize,
color="#7f7f7f",
),
tickfont=dict(size=fontsize * 0.8),
),
yaxis=dict(
title=label_v,
titlefont=dict(
# family='Courier New, monospace',
size=fontsize,
color="#7f7f7f",
),
tickfont=dict(size=fontsize * 0.8),
),
showlegend=legend,
width=width,
height=height,
margin=dict(l=margin_l, b=margin_b),
legend=dict(x=legend_x, y=legend_y),
boxmode=boxmode,
)
def scatter(self, data, outfile="scatter.png", modes="lines+markers"):
"""Creates a scatter plot
Parameters:
data: array/list, pd series, list of arrays/lists, pd df
Provide a list or arrays/lists or a pandas dataframe.
The variables should be ordered in pairs such that
each odd variable in the list/first column in the df
gets assigned to the horizontal axis, each even
variable to the vertical axes. Each pair needs
to have the same length, but pairs can be of
a different length.
outfile: str
Filename, include .png, .png .pdf
modes: str or list of str
'markers', 'lines', 'lines + markers' or
a list of the above to assign to each plot
(one string in a list for each pair of data)
Returns:
fig: plotly figure if self.interactive
else True
"""
# some input format error handling, not exhaustive
if (isinstance(data, list)) and (len(data) < 2):
msg = (
"Provide at least two arrays or columns to"
"create a scatter plot. Or try Series plot "
"for a single column of data versus its index."
)
log.error(msg)
raise Exception
if (isinstance(data, pd.Series)) or (
(isinstance(data, pd.DataFrame)) and (data.shape[1] == 1)
):
msg = (
"Provide a dataframe with no less than two"
"columns. Series plot can plot a single column"
"against its index."
)
log.error(msg)
raise Exception
# rectangles the data if passed as a list of lists/arrays
# if some of the lists/arrays are shorter, the gaps are
# filled with np.nan
if isinstance(data, list):
df_data = pd.DataFrame(
data=np.empty(
(
len(max(data, key=len)),
len(data),
)
)
* np.nan
)
col_inx = 0
for i in data:
if isinstance(i, list):
i_list = i
else:
i_list = i.tolist()
df_data[col_inx] = (
i_list
+ (np.empty(df_data.shape[0] - len(i)) * np.nan).tolist()
)
col_inx += 1
data = df_data.copy()
if self.duration_curve:
for col_index in range(0, df_data.shape[1]):
data.iloc[:, col_index] = (
data.iloc[:, col_index].sort_values(ascending=False).values
)
if self.data_headers:
data.columns = self.data_headers
num_columns = data.shape[1]
if not isinstance(modes, list):
list_modes = [modes] * int(num_columns / 2)
else:
list_modes = modes
if num_columns % 2 != 0:
msg = (
"Provide an even number of columns,"
"e.g. [x1, y1, x2, y2, ...]"
)
log.error(msg)
raise Exception
plot_data = []
for col_index in range(0, num_columns, 2):
plot_data.append(
go.Scatter(
x=data.iloc[:, col_index],
y=data.iloc[:, col_index + 1],
mode=list_modes[int(col_index / 2.0)],
name=data.columns[col_index + 1],
)
)
fig = go.Figure(data=plot_data, layout=self.layout)
if self.save_image:
if self.outpath == None:
self.outpath = os.getcwd()
if not os.path.exists(self.outpath):
os.makedirs(self.outpath)
pio.write_image(fig, os.path.join(self.outpath, outfile))
if self.interactive:
try:
iplot(fig)
return fig
except:
log.error("Interactive mode failed.")
raise Exception
return True
def series(
self,
data,
index_in_a_column=None,
outfile="series.png",
modes="lines+markers",
):
"""Plots all series data against either the index or the first
provided series. It can sort the data and plot the duration_curve.
Parameters:
data: array/list, pd series, list of arrays/lists, pd df
Provide an array or a list if plotting a single
variable. If plotting multiple variables provide
a list of arrays or a pandas dataframe.
Horizontal axis corresponds to:
* if pd df: the index of the dataframe or the first columns of the dataframe
* if list or arrays/lists: a range of array length of the first array/list in the list
All arrays in the list need to have the same length.
index_in_a_column: boolean
Horizontal axis labels
If None, dataframe index is used, otherwise pass a
column label for a column (it will not be considered
as a series to plot)
outfile: str
Filename, include .png, .png .pdf
modes: str or list of str
'markers', 'lines', 'lines+markers' or
a list of the above to assign to each column
of data, excluding the first column if
index_in_a_column is not None
Returns:
fig: plotly figure if self.interactive
else True
"""
# rectangles the data if passed as a list of lists/arrays
# if some of the lists/arrays are shorter, the gaps are
# filled with np.nan
if isinstance(data, list):
df_data = pd.DataFrame(
data=np.empty(
(
len(max(data, key=len)),
len(data),
)
)
* np.nan
)
col_inx = 0
for i in data:
if isinstance(i, np.ndarray):
df_data[col_inx] = np.concatenate(
(
i,
(
np.empty((1, df_data.shape[0] - len(i)))
* np.nan
)[0],
)
)
if isinstance(i, list):
df_data[col_inx] = (
i
+ (np.empty((1, df_data.shape[0] - len(i))) * np.nan)[
0
].tolist()
)
col_inx += 1
data = df_data.copy()
if index_in_a_column is not None:
labels_h_axis = data.loc[:, index_in_a_column]
data = data.drop(columns=[index_in_a_column])
else:
labels_h_axis = data.index
if self.data_headers:
data.columns = self.data_headers
num_columns = data.shape[1]
if not isinstance(modes, list):
list_modes = [modes] * num_columns
else:
list_modes = modes
if self.duration_curve:
for col_index in range(0, num_columns):
data.iloc[:, col_index] = (
data.iloc[:, col_index].sort_values(ascending=False).values
)
plot_data = []
for col_index in range(num_columns):
plot_data.append(
go.Scatter(
x=labels_h_axis,
y=data.iloc[:, col_index],
mode=list_modes[col_index],
name=data.columns[col_index],
)
)
fig = go.Figure(data=plot_data, layout=self.layout)
if self.save_image:
if self.outpath == None:
self.outpath = os.getcwd()
if not os.path.exists(self.outpath):
os.makedirs(self.outpath)
pio.write_image(fig, os.path.join(self.outpath, outfile))
if self.interactive:
try:
iplot(fig)
return fig
except:
log.error("Interactive mode failed.")
raise Exception
return True
def box(
self,
dfs,
plot_cols=None,
groupby_cols=None,
df_cat=None,
outfile="box.png",
boxmean=False,
colors=["#3D9970", "#FF4136", "#FF851B"],
title="Energy Use",
boxpoints="outliers",
):
"""Creates box plots for the chosen `plot_col` and can
group plots by the `groupby_col`.
Parameters:
dfs: list of dfs
df_cat: list of str
Indicator of the category carried by the dfs
(E.g. the dfs differ by housing type)
plot_col: list of columns to plot, one from each df in
dfs. If multiple dfs are passed, the values will be
shown as groups on the plot
groupby_cols: list of cols to use as x axis, from each
df. Use the same column if it has the same elements.
Use None if x axis category not used
boxpoints: False, 'all', 'outliers', 'suspectedoutliers'
See https://plot.ly/python/reference/#box
Returns:
fig: plotly figure if self.interactive
else True
"""
# Extract y values
y = dict()
x = dict()
trace = dict()
df_ctg = dict()
data = list()
i = 0
for df in dfs:
y[i] = df[plot_cols[i]].values.tolist()
if (groupby_cols is not None) and (groupby_cols[i] is not None):
x[i] = df[groupby_cols[i]].values.tolist()
else:
x[i] = None
if df_cat[i] is not None:
df_ctg[i] = df_cat[i]
else:
df_ctg[i] = ""
trace[i] = go.Box(
y=y[i],
x=x[i],
name=plot_cols[i] + " - " + df_ctg[i],
boxpoints=boxpoints,
marker=dict(color=colors[i]),
boxmean=boxmean,
)
data.append(trace[i])
i += 1
fig = go.Figure(data=data, layout=self.boxlayout)
if self.save_image:
if self.outpath == None:
self.outpath = os.getcwd()
if not os.path.exists(self.outpath):
os.makedirs(self.outpath)
pio.write_image(fig, os.path.join(self.outpath, outfile))
if self.interactive:
try:
iplot(fig)
return fig
except:
log.error("Interactive mode failed.")
raise Exception
return True
# size and color modes:
# .........
# mode='markers',
# marker={'size': sz,
# 'color': colors,
# 'opacity': 0.6,
# 'colorscale': 'Viridis'