-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplot_generator.py
1164 lines (854 loc) · 42.5 KB
/
plot_generator.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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 9 15:35:26 2020
@author: giamm
"""
from pathlib import Path
import numpy as np
import matplotlib.pyplot as plt
import random
import math
import datareader
###############################################################################
# This script is used to plot the results from the simulation
###############################################################################
# The basepath of the file is stored in a variable
basepath = Path(__file__).parent
# An /Output/Figures folder is created in order to store the graphs as .png files
dirname = 'Output'
subdirname = 'Figures'
try: Path.mkdir(basepath / dirname / subdirname)
except Exception: pass
## Scale settings
# This is done in order to convert the data (time, powers, energies) from the units of measures
# used in the simulation (min, W, Wh respectively) to the ones used for the post-processing of the results
ts_dict = {'min':1/1,'h':1/60}
ps_dict = {'W':1/1,'kW':1/1e3}
es_dict = {'Wh':1/1,'kWh':1/1e3,'MWh':1/1e6}
## Building seasons and week dictionaries
# This is done in order to explore all the seasons and, for each season, both
# types of days (weekday and weekend)
seasons = dict({'winter':(0,'w'),'spring':(1,'ap'),'summer':(2,'s'),'autumn':(3,'ap')})
days = dict({'week-day':(0,'wd'),'weekend-day':(1,'we')})
## Creating a list of different colors
colors = [(230, 25, 75),
(60, 180, 75),
(255, 225, 25),
(0, 130, 200),
(245, 130, 48),
(145, 30, 180),
(70, 240, 240),
(240, 50, 230),
(210, 245, 60),
(250, 190, 212),
(0, 128, 128),
(220, 190, 255),
(170, 110, 40),
(255, 250, 200),
(128, 0, 0),
(170, 255, 195),
(128, 128, 0),
(255, 215, 180),
(0, 0, 128),
(128, 128, 128)]
# Transforming into rgb triplets
colors_rgb = []
for color in colors:
color_rgb = []
for value in color:
color_rgb.append(float(value)/255)
colors_rgb.append(tuple(color_rgb))
## Seasonal energy consumptions
# A method for plotting the energy consumption by season (total or average) from appliances or classes of appliances
# is created.
def seasonal_energy(labels_dict, energies, fig_specs, appliances_data, **params):
''' The method returns a figure-handle where total energy consumption for appliances or class of appliances
is plotted, divided by season.
Inputs:
labels_dict - dict, containing for each label (keys) a unique corresponding index (value)
energies - 2d array, energy consumption for each appliance/appliance type (axis = 0), by season (axis = 1)
season - str, containing the name of the season
plot_specs - dict, for each type of load profile, the type of plot (str, bar plot or plot) and the legend (str)
fig_specs - dict, containing specific indications such suptitle, etc.
**params(if not specified, default values are used)
- 'time_scale': str, 's', 'h'
- 'power_scale': str,'W', 'kW'
- 'energy_scale': str, 'Wh', 'kWh', MWh'
- 'figsize': tup, height and width of the figure
- 'orientation': str, 'horizontal', 'vertical'
- 'font_small': float, size of the small fonts (ticks, ...)
- 'font_medium': float, size of the medium fonts (legend, labels, ...)
- 'font_large': float, size of the large fonts (titles, ...)
- all the simulation parameters (n_hh, en_class, etc.)
Outputs:
fig - figure handle
'''
## Input data for the appliances
# Appliances' attributes, energy consumptions and user's coefficients
# # apps is a 2d-array in which, for each appliance (rows) and attribute value is given (columns)
# apps_ID = appliances_data['apps_ID']
# # apps_attr is a dictionary in which the name of each attribute (value) is linked to its columns number in apps (key)
# apps_attr = appliances_data['apps_attr']
## Parameters
# Default parameters
def_params = {
'time_scale': 'h',
'power_scale': 'kW',
'energy_scale': 'MWh',
'figsize': (297/25.4 , 420/25.4),
'orientation': 'horizontal',
'font_small': 14,
'font_medium': 16,
'font_large': 18,
}
# Setting the parameters that are not specified when the function is called to the default value
for param in def_params:
if param not in params: params[param] = def_params[param]
# Scales setup: factors needed to turn the values of time, power and energy in the correct scale
# time_scale = params['time_scale']
# power_scale = params['power_scale']
energy_scale = params['energy_scale']
# ts = ts_dict[time_scale]
# ps = ps_dict[power_scale]
es = es_dict[energy_scale]
# Adjusting energies to the proper scales
energies = energies*es
# # Making sure that each value for the energy consumption correspond to the correct label
# id_list = [labels_dict[label][0] for label in labels_dict]
# energies = energies[id_list,:]
##
# Figure setup: figure size and orientation, font-sizes
figsize = params['figsize']
orientation = params['orientation']
if orientation == 'horizontal': figsize = figsize[::-1]
fontsize_title = params['font_large']
fontsize_legend = params['font_medium']
fontsize_labels = params['font_medium']
fontsize_text = params['font_medium']
fontsize_ticks = params['font_small']
# fontsize_pielabels = params['font_small']
##
# Creating a figure
fig, ax = plt.subplots(figsize=figsize)
suptitle = fig_specs['suptitle']
fig.suptitle(suptitle, fontsize = fontsize_title, fontweight = 'bold')
fig.subplots_adjust(left = 0.1, bottom = 0.2, right = 0.9, top = 0.88, wspace = None, hspace = 0.3)
##
# Labels for the plot, not sorted
labels_notsort = [label.capitalize().replace('_',' ') for label in labels_dict]
# Sum of seasonal energy consumptions, for sorting the data
total_heights = np.sum(energies, axis = 1)
ymax = np.max(total_heights)
# Indices for slicing and sorting the labels and the energies in increasing order
heights_sortind = np.argsort(total_heights)
# Labels for the plot, sorted
# labels = labels_notsort[heights_sortind]
labels = [labels_notsort[ind] for ind in heights_sortind]
# Initializing the bottoms to zero, in order to make a stack bar plot
bottoms = np.zeros(len(labels))
# Initializing the list of seasons, for the legend
legend = []
# Initializing the text to add (total seasonal energy consumption),
text_to_add = 'Energy consumption by season'
##
# Running through the seasons
for season in seasons:
# Number corresponding to the season (0: winter, 1: summer, 2: spring, 3: autumn)
ss = seasons[season][0]
# Energies corresponding to the seasonal consumption, sorted
heights = energies[heights_sortind, ss]
# Plotting the energy consumption for each season and updating the bottom values
ax.bar(labels, heights, bottom = bottoms)
bottoms = bottoms + heights
# Adding the current season to the legend
legend.append(season.capitalize())
# Adding the total energy consumption for the current season to the text to be added
text_to_add = '\n\n'.join((text_to_add, '{0:s}: {1:.2f} {2:s}'.format(season.capitalize(), np.sum(heights), energy_scale)))
##
# Making the figure look properly
ax.set_ylim([0, 1.1*ymax])
ax.set_ylabel('Energy consumption ({}/year)'.format(energy_scale), fontsize = fontsize_labels)
ax.tick_params(axis ='both', labelsize = fontsize_ticks)
ax.tick_params(axis ='x', labelrotation = 45)
ax.grid(axis = 'y')
ax.legend(legend, loc = 'upper left', ncol = len(seasons), fontsize = fontsize_legend)
# Adding the text with the total energy consumptions by season
props = dict(boxstyle='square', facecolor = colors_rgb[2], pad = 0.3, alpha = 0.5)
ax.text(0.02, 0.9, text_to_add.rstrip(), fontsize = fontsize_text, ha = 'left', va = 'top', transform = ax.transAxes , bbox = props)
##
return(fig)
## Yearly energy
# A method for plotting the yearly energy consumption (total/average) for appliances or classes of appliances
# is created.
def yearly_energy(labels_dict, energies, fig_specs, appliances_data, **params):
''' The method returns a figure-handle where the yearly energy consumption (total/average)
for appliances or class of appliances is plotted.
Inputs:
labels_dict - dict, containing for each label (keys) a unique corresponding index (value)
energies - 1d array, energy consumption for each appliance/appliance type (axis = 0) for one year
plot_specs - dict, for each load profile, the type of plot (str, bar plot or plot) and the legend (str)
season - str, containing the name of the season
**params(if not specified, default values are used)
- 'time_scale': str, 's', 'h'
- 'power_scale': str,'W', 'kW'
- 'energy_scale': str, 'Wh', 'kWh', MWh'
- 'figsize': tup, height and width of the figure
- 'orientation': str, 'horizontal', 'vertical'
- 'font_small': float, size of the small fonts (ticks, ...)
- 'font_medium': float, size of the medium fonts (legend, labels, ...)
- 'font_large': float, size of the large fonts (titles, ...)
Outputs:
fig - figure handle
'''
## Input data for the appliances
# Appliances' attributes, energy consumptions and user's coefficients
# # apps is a 2d-array in which, for each appliance (rows) and attribute value is given (columns)
# apps_ID = appliances_data['apps_ID']
# # apps_attr is a dictionary in which the name of each attribute (value) is linked to its columns number in apps (key)
# apps_attr = appliances_data['apps_attr']
## Parameters
# Default parameters
def_params = {
'time_scale': 'h',
'power_scale': 'kW',
'energy_scale': 'MWh',
'figsize': (297/25.4 , 420/25.4),
'orientation': 'horizontal',
'font_small': 14,
'font_medium': 16,
'font_large': 18,
}
# Setting the parameters that are not specified when the function is called to the default value
for param in def_params:
if param not in params: params[param] = def_params[param]
# Scales setup: factors needed to turn the values of time, power and energy in the correct scale
# time_scale = params['time_scale']
# power_scale = params['power_scale']
energy_scale = params['energy_scale']
# ts = ts_dict[time_scale]
# ps = ps_dict[power_scale]
es = es_dict[energy_scale]
# Adjusting energies to the proper scales
energies = energies*es
# # Making sure that each value for the energy consumption correspond to the correct label
# id_list = [labels_dict[label][0] for label in labels_dict]
# energies = energies[id_list,:]
##
# Figure setup: figure size and orientatio, font-sizes
figsize = params['figsize']
orientation = params['orientation']
if orientation == 'horizontal': figsize = figsize[::-1]
fontsize_title = params['font_large']
# fontsize_legend = params['font_medium']
fontsize_labels = params['font_medium']
fontsize_text = params['font_medium']
fontsize_ticks = params['font_small']
# fontsize_pielabels = params['font_small']
##
# Creating a figure
fig, ax = plt.subplots(figsize = figsize)
suptitle = fig_specs['suptitle']
fig.suptitle(suptitle, fontsize = fontsize_title, fontweight = 'bold')
fig.subplots_adjust(left = 0.1, bottom = 0.2, right = 0.9, top = 0.88, wspace = None, hspace = 0.3)
##
# Labels for the plot, not sorted
labels_notsort = [label.capitalize().replace('_',' ') for label in labels_dict]
# Indices for slicing and sorting the labels and the energies in increasing order
heights_sortind = np.argsort(energies)
# Heights for the plot, sorted
heights = energies[heights_sortind]
ymax = np.max(heights)
# Labels for the plot, sorted
# labels = labels_notsort[heights_sortind]
labels = [labels_notsort[ind] for ind in heights_sortind]
##
# Plotting the yearly energy consumption
ax.bar(labels, heights)
# Showing the value of the energy consumption above each bar
for index, value in enumerate(heights):
plt.text(index, 1.01*value, '%.f' %(value), ha = 'center', va = 'bottom', rotation = 0, fontsize = fontsize_text)
##
# Evaluating the percentage of the total energy consumption
total_energy = np.sum(energies)
energy_perc = energies/total_energy*100
heights = np.sort(energy_perc)
# Creating a twin y-axis and plotting the percentage energy consumption
ax_tw = ax.twinx()
ax_tw.plot(labels, heights, 'rs')
if fig_specs['text'] == 'on':
# Adding the text showing the total yearly energy consumption
text_toadd = '\n'.join(('Total energy consumption','{} {}/year'.format(total_energy, energy_scale)))
props = dict(boxstyle = 'square', facecolor = colors_rgb[2], pad = 0.3, alpha = 0.5)
ax.text(0.02, 0.95, text_toadd, fontsize = fontsize_text, ha ='left', va ='top', transform = ax.transAxes, bbox = props)
##
# Making the figure look properly
ax.set_ylabel('Energy consumption ({}/year)'.format(energy_scale), fontsize=fontsize_labels)
ax.set_ylim([0, 1.1*ymax])
ax.tick_params(axis = 'both', labelsize = fontsize_ticks)
ax.tick_params(axis = 'x', rotation = 70)
ax.grid(axis = 'y')
ax_tw.set_ylabel('Energy consumption (%)', fontsize = fontsize_labels)
ax_tw.yaxis.label.set_color('r')
ax_tw.set_ylim([0, 1.1*ymax/total_energy*100])
ax_tw.spines['right'].set_color('r')
ax_tw.tick_params(axis = 'y', colors = 'r', labelsize = fontsize_ticks)
##
return(fig)
## Seasonal energy pie
# A method for plotting the yearly energy consumption for appliances or classes of appliances (percentage over total)
# as a pie plot is created.
def seasonal_energy_pie(labels_dict, energies, fig_specs, appliances_data, **params):
''' The method returns a figure-handle where the percentage over the total energy consumption
for appliances or class of appliances is plotted, divided by season.
Inputs:
labels_dict - dict, containing for each label (keys) a unique corresponding index (value)
energies - 2d array, energy consumption for each appliance/appliance type (axis = 0), by season (axis = 1)
# plot_specs - dict, for each load profile, the type of plot (str, bar plot or plot) and the legend (str)
**params(if not specified, default values are used)
- 'time_scale': str, 's', 'h'
- 'power_scale': str,'W', 'kW'
- 'energy_scale': str, 'Wh', 'kWh', MWh'
- 'figsize': tup, height and width of the figure
- 'orientation': str, 'horizontal', 'vertical'
- 'font_small': float, size of the small fonts (ticks, ...)
- 'font_medium': float, size of the medium fonts (legend, labels, ...)
- 'font_large': float, size of the large fonts (titles, ...)
Outputs:
fig - figure handle
'''
## Input data for the appliances
# Appliances' attributes, energy consumptions and user's coefficients
# # apps is a 2d-array in which, for each appliance (rows) and attribute value is given (columns)
# apps_ID = appliances_data['apps_ID']
# # apps_attr is a dictionary in which the name of each attribute (value) is linked to its columns number in apps (key)
# apps_attr = appliances_data['apps_attr']
## Parameters
# Default parameters
def_params = {
'time_scale': 'h',
'power_scale': 'kW',
'energy_scale': 'MWh',
'figsize': (297/25.4 , 420/25.4),
'orientation': 'horizontal',
'font_small': 14,
'font_medium': 16,
'font_large': 18,
}
##
# Setting the parameters that are not specified when the function is called to the default value
for param in def_params:
if param not in params: params[param] = def_params[param]
# Scales setup: factors needed to turn the values of time, power and energy in the correct scale
# time_scale = params['time_scale']
# power_scale = params['power_scale']
# energy_scale = params['energy_scale']
# ts = ts_dict[time_scale]
# ps = ps_dict[power_scale]
# es = es_dict[energy_scale]
# Figure setup: figure size and orientatio, font-sizes
figsize = params['figsize']
orientation = params['orientation']
if orientation == 'horizontal': figsize = figsize[::-1]
fontsize_title = params['font_large']
fontsize_legend = params['font_medium']
# fontsize_labels = params['font_medium']
# fontsize_text = params['font_medium']
# fontsize_ticks = params['font_small']
fontsize_pielabels = params['font_small']
##
# Creating a new figure, with multiple subplots (one for each season)
fig, ax = plt.subplots(2, 2, figsize=figsize)
suptitle = fig_specs['suptitle']
fig.suptitle(suptitle, fontsize =fontsize_title , fontweight = 'bold')
fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.88, wspace=None, hspace=0.1)
# Indices that run through the row and columns of ax
subpl_row = 0
subpl_col = 0
##
# RUnning through the seasons
for season in seasons:
# Number corresponding to the season (0: winter, 1: summer, 2: spring, 3: autumn)
ss = seasons[season][0]
# Updating the row and columns indeces
if subpl_col > 1:
subpl_row = 1
subpl_col = 0
# Initializing lists where to store the labels, sizes and colors for the pie plot
labels = []
sizes = []
colors = []
# Selecting the data to plot (only if the seasonal energy consumption is larger than zero)
for label in labels_dict:
labels_index = labels_dict[label][0]
if energies[labels_index, ss] > 0:
labels.append(label.capitalize().replace('_', ' '))
sizes.append(energies[labels_index, ss])
colors.append(colors_rgb[labels_index])
##
# Plotting the data and making the figure look properly
ax[subpl_row,subpl_col].pie(sizes, autopct='%1.1f%%', pctdistance=0.6, radius=0.8, frame=True, colors = colors , startangle=60, textprops = {'fontsize':fontsize_pielabels})
ax[subpl_row,subpl_col].legend(labels)
ax[subpl_row,subpl_col].axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle
ax[subpl_row,subpl_col].set_title(season.capitalize(), loc='left', pad = 0.5, fontsize = fontsize_legend, fontweight = 'bold')
ax[subpl_row,subpl_col].set_xticks([])
ax[subpl_row,subpl_col].set_yticks([])
subpl_col += 1
##
return(fig)
## Parametric analysis
# A method for plotting the results of a parametric analysis for different sizes of PV/battery
def parametric_analysis(main_size_range, lead_size_range, data, plot_specs, fig_specs, **params):
''' The method returns a figure-handle where the percentage over the total energy consumption
for appliances or class of appliances is plotted, divided by season.
Input:
main_size_range - list, containing the different sizes along which to plot on the x-axis
lead_size_range - list, containing the different sizes; for each of them a different subplot will be created
data - 3d-array, containing the different data (axis = 2) to be plotted along the y-axes for the different main sizes on
the x-axis (axis = 0), for the different subplots (axis = 1)
plot_specs - dict, containing for each type of data some plotting information
fig_specs -dict, containing plot information for the figure
**params(if not specified, default values are used)
- 'figsize': tup, height and width of the figure
- 'orientation': str, 'horizontal', 'vertical'
- 'font_small': float, size of the small fonts (ticks, ...)
- 'font_medium': float, size of the medium fonts (legend, labels, ...)
- 'font_large': float, size of the large fonts (titles, ...)
Output:
fig - figure handle
'''
## Parameters
# Default parameters
def_params = {
'figsize': (297/25.4 , 420/25.4),
'orientation': 'horizontal',
'font_small': 14,
'font_medium': 16,
'font_large': 18,
}
##
# Setting the parameters that are not specified when the function is called to the default value
for param in def_params:
if param not in params: params[param] = def_params[param]
# Figure setup: figure size and orientatio, font-sizes
figsize = params['figsize']
orientation = params['orientation']
if orientation == 'horizontal': figsize = figsize[::-1]
fontsize_title = params['font_medium']
fontsize_legend = params['font_small']
fontsize_labels = params['font_small']
# fontsize_text = params['font_medium']
fontsize_ticks = params['font_small']
# fontsize_pielabels = params['font_small']
##
# For each size in the lead_size_range list, a subplot will be created. The data will be plotted
# on the y-axis, while on x-axis there will be the main_size_range
n_sizes_main = len(main_size_range)
n_sizes_lead = len(lead_size_range)
n_subplots = n_sizes_lead
n_cols = 2
n_rows = int((n_subplots + 1)/n_cols)
n_plots = np.size(data, axis = 2)
# Creating a figure with multiple subplots, with two rows (one for each type of day)
fig, ax = plt.subplots(n_rows, n_cols, sharex = False, sharey = False, figsize = figsize)
ax = ax.reshape((n_rows, n_cols))
suptitle = fig_specs['suptitle']
fig.suptitle(suptitle, fontsize = fontsize_title, fontweight = 'bold')
fig.subplots_adjust(left = 0.1, bottom = 0.1, right = 0.9, top = 0.9, wspace = 0, hspace = 0.3)
# Needed to set the bars width in bar plots
d_main_size = (main_size_range[-1]-main_size_range[0])/n_sizes_main
# Running through the various subplots
for lead_size in lead_size_range:
lead_index = lead_size_range.index(lead_size)
i_row = int(lead_index/n_cols)
i_col = lead_index%n_cols
yaxis_right_flag = 1
# In case of uneven number of plots, the last subplot is expanded into two slots
if lead_index == n_sizes_lead - 1 and n_subplots%2 != 0:
ax[i_row, i_col] = plt.subplot(n_rows, 1, n_rows)
# Running throught he varous data to be plotted
for i_plot in range(n_plots):
plot_data = data[:, lead_index, i_plot]
plot_yaxis = plot_specs[i_plot]['yaxis']
plot_type = plot_specs[i_plot]['type']
plot_label = plot_specs[i_plot]['label']
# ax[i_row, i_col].plot(main_size_range, plot_data, 's-')
if plot_yaxis == 'left':
if plot_type == 'plot':
ax[i_row, i_col].plot(main_size_range, plot_data, color = colors_rgb[i_plot], linestyle = '--', marker = 's', label = plot_label)
elif plot_type == 'bar':
ax[i_row, i_col].bar(main_size_range, plot_data, width = d_main_size, color = colors_rgb[i_plot], label = plot_label, alpha = 0.5)
elif plot_yaxis == 'right':
if yaxis_right_flag == 1:
axtw = ax[i_row, i_col].twinx()
yaxis_right_flag = 0
if plot_type == 'plot':
axtw.plot(main_size_range, plot_data, color = colors_rgb[i_plot], linestyle = '--', marker = 's', label = plot_label)
elif plot_type == 'bar':
axtw.bar(main_size_range, plot_data, width = d_main_size, color = colors_rgb[i_plot], fill = False, label = plot_label, alpha = 0.5)
# Making the subplot looking nice
ax[i_row, i_col].set_xlabel(fig_specs['xaxis_label'], fontsize = fontsize_labels)
# Set one tick each size
ax[i_row, i_col].set_xticks(main_size_range[: : 1])
if i_col == 0:
ax[i_row, i_col].set_ylabel(fig_specs['yaxis_left_label'], fontsize = fontsize_labels)
else:
ax[i_row, i_col].tick_params(
axis = 'y',
which = 'both',
left = False,
labelleft = False,
)
ax[i_row, i_col].set_ylim(fig_specs['yaxis_left_ylim'])
ax[i_row, i_col].tick_params(axis ='both', labelsize = fontsize_ticks)
# ax[i_row, i_col].grid(axis = 'y')
ax[i_row, i_col].legend(loc = 'upper left', fontsize = fontsize_legend)
title = '{} size: {} {}'.format(fig_specs['lead_size_name'], lead_size_range[lead_index], fig_specs['lead_size_uom'])
ax[i_row, i_col].set_title(title, fontsize = fontsize_title)
# If something has been plotted on the right y-axis:
if yaxis_right_flag == 0:
if i_col%2 == 1 or (lead_index == n_sizes_lead - 1 and n_subplots%2 != 0):
axtw.set_ylabel(fig_specs['yaxis_right_label'], fontsize = fontsize_labels)
else:
axtw.tick_params(
axis = 'y',
which = 'both',
right = False,
labelright = False,
)
axtw.grid(axis = 'y')
axtw.set_ylim(fig_specs['yaxis_right_ylim'])
axtw.legend(loc = 'upper right', fontsize = fontsize_legend)
return fig
## Parametric chart
# A method is created for plotting pairs of sizes in correspondence of two indicators' values
def parametric_chart(plot_specs, fig_specs, **params):
''' The method returns a figure-handle where the percentage over the total energy consumption
for appliances or class of appliances is plotted, divided by season.
Input:
plot_specs - dict, containing for each plot, the data (x_values, y_values) and other information
fig_specs -dict, containing plot information for the figure
**params(if not specified, default values are used)
- 'figsize': tup, height and width of the figure
- 'orientation': str, 'horizontal', 'vertical'
- 'font_small': float, size of the small fonts (ticks, ...)
- 'font_medium': float, size of the medium fonts (legend, labels, ...)
- 'font_large': float, size of the large fonts (titles, ...)
Output:
fig - figure handle
'''
## Parameters
# Default parameters
def_params = {
'figsize': (297/25.4 , 420/25.4),
'orientation': 'horizontal',
'font_small': 14,
'font_medium': 16,
'font_large': 18,
}
##
# Setting the parameters that are not specified when the function is called to the default value
for param in def_params:
if param not in params: params[param] = def_params[param]
# Figure setup: figure size and orientatio, font-sizes
figsize = params['figsize']
orientation = params['orientation']
if orientation == 'horizontal': figsize = figsize[::-1]
fontsize_title = params['font_medium']
fontsize_legend = params['font_small']
fontsize_labels = params['font_small']
# fontsize_text = params['font_medium']
fontsize_ticks = params['font_small']
# fontsize_pielabels = params['font_small']
##
# Creating a figure with multiple subplots, with two rows (one for each type of day)
fig, ax = plt.subplots(figsize = figsize)
suptitle = fig_specs['suptitle']
fig.suptitle(suptitle, fontsize = fontsize_title, fontweight = 'bold')
fig.subplots_adjust(left = 0.1, bottom = 0.1, right = 0.9, top = 0.9, wspace = 0, hspace = 0.3)
# If there is something to be plotted on the yaxis right, the flag is deactivated
yaxis_right_flag = 1
# The number of plots on each yaxis are taken into account (to set the number of cols in the legend)
n_plot_left = 0
n_plot_right = 0
for i_plot in plot_specs:
x_data = plot_specs[i_plot]['plot_xvalues']
y_data = plot_specs[i_plot]['plot_yvalues']
try: plot_yaxis = plot_specs[i_plot]['plot_yaxis']
except: plot_yaxis = 'left'
try: plot_color = plot_specs[i_plot]['plot_color']
except: plot_color = colors_rgb[i_plot]
try: plot_label = plot_specs[i_plot]['plot_label']
except: plot_label = ''
try: plot_linestyle = plot_specs[i_plot]['plot_linestyle']
except: plot_linestyle = '-'
try: plot_marker = plot_specs[i_plot]['plot_marker']
except: plot_marker = 's'
if plot_yaxis == 'right':
if yaxis_right_flag == 1:
axtw = ax.twinx()
yaxis_right_flag = 0
axtw.plot(x_data, y_data, color = plot_color, linestyle = plot_linestyle, marker = plot_marker, label = plot_label)
n_plot_right += 1
else:
ax.plot(x_data, y_data, color = plot_color, linestyle = plot_linestyle, marker = plot_marker, label = plot_label)
n_plot_left += 1
# Making the subplot looking nice
ax.set_xlabel(fig_specs['xaxis_label'], fontsize = fontsize_labels)
ax.set_xlim(fig_specs['xaxis_lim'])
ax.xaxis.set_major_locator(plt.MaxNLocator(10))
ax.set_ylabel(fig_specs['yaxis_label'], fontsize = fontsize_labels)
ax.set_ylim(fig_specs['yaxis_lim'])
ax.yaxis.set_major_locator(plt.MaxNLocator(10))
ax.tick_params(axis ='both', labelsize = fontsize_ticks)
max_cols = 2; ncol = min(max_cols, n_plot_left)
ax.legend(loc = 'upper left', ncol = ncol, fontsize = fontsize_legend)
ax.grid()
if yaxis_right_flag == 0:
axtw.axis('off')
axtw.set_ylim(fig_specs['yaxis_lim'])
max_cols = 3; ncol = min(max_cols, n_plot_right)
axtw.legend(loc = 'upper right', ncol = ncol, fontsize = fontsize_legend)
return fig
## Daily profiles
# A method for plotting various profiles (power/energy) during two typical days (week-day and weekend)
def daily_profiles(time, powers, plot_specs, fig_specs, **params):
''' The method returns a figure-handle where seasonal load profiles are plotted, for
both day types.
Inputs:
time - 1d array, vector of time
powers - 3d array, different types (axis = 0) of time-dependent profiles (axis = 1) to be plotted for each day-type (axis = 2)
plot_specs - dict, for each type of profile, the type of plot (str, bar plot or plot), the legend (str), yaxis (str, eft or right), etc.
fig_specs - dict, containing specific indications such suptitle, etc.
**params(if not specified, default values are used)
- 'time_scale': str, 's', 'h'
- 'power_scale': str,'W', 'kW'
- 'energy_scale': str, 'Wh', 'kWh', MWh'
- 'figsize': tup, height and width of the figure
- 'orientation': str, 'horizontal', 'vertical'
- 'font_small': float, size of the small fonts (ticks, ...)
- 'font_medium': float, size of the medium fonts (legend, labels, ...)
- 'font_large': float, size of the large fonts (titles, ...)
- all the simulation parameters (n_hh, en_class, etc.)
Outputs:
fig - figure handle
'''
## Parameters
# Default parameters
def_params = {
'figsize': (297/25.4 , 420/25.4),
'orientation': 'horizontal',
'font_small': 12,
'font_medium': 14,
'font_large': 16,
}
# The parameters that are not specified when the function is called are set to the default value
for param in def_params:
if param not in params: params[param] = def_params[param]
## Updating parameters
# Figure setup: figure size and orientation, font-sizes
figsize = params['figsize']
orientation = params['orientation']
if orientation == 'horizontal': figsize = figsize[::-1]
fontsize_title = params['font_large']
fontsize_legend = params['font_medium']
fontsize_labels = params['font_medium']
# fontsize_text = params['font_medium']
fontsize_ticks = params['font_small']
# fontsize_pielabels = params['font_small']
##
# Creating a figure with multiple subplots, with two rows (one for each type of day)
fig, ax = plt.subplots(2, 1, sharex = False, sharey = False, figsize = figsize)
suptitle = fig_specs['suptitle']
fig.suptitle(suptitle, fontsize = fontsize_title, fontweight = 'bold')
fig.subplots_adjust(left = 0.1, bottom = 0.1, right = 0.9, top = 0.85, wspace = None, hspace = 0.3)
##
# Evaluating the time-step of the time-vector in order to set the bars' width
dt = float((time[-1] - time[0])/(np.size(time) - 1))
# Evaluating the number of profiles passed to the function for each day-type
# It is given for ganted that only two day-types are considered
n_profiles = np.size(powers, axis = 0)
# Initializing minimum and maximum value for the two yaxis
ymin_left, ymax_left = 0, 0
ymin_right, ymax_right = 0, 0
##
#Running through the day-types (week-day and weekend-day)
for day in days:
# Number corresponding to the type of day (0: week-day, 1: week-end -day)
dd = days[day][0]
# Number of plots along left and right yaxis
n_plot_left = 0
n_plot_right = 0
# Running through the types of load profiles to be plotted for each day-type
for i_profile in range(n_profiles):
# Selecting the correct power-data to plot and the plot specifications
plot_data = powers[i_profile, :, dd]
try: plot_type = plot_specs[i_profile]['plot_type']
except: plot_type = 'plot'
try: plot_yaxis = plot_specs[i_profile]['plot_yaxis']
except: plot_yaxis = 'left'
try: plot_label = plot_specs[i_profile]['plot_label']
except: plot_label = ''
try: plot_linestyle = plot_specs[i_profile]['plot_linestyle']
except: plot_linestyle = '-'
try: plot_marker = plot_specs[i_profile]['plot_marker']
except: plot_marker = ''
try: plot_alpha = plot_specs[i_profile]['plot_alpha']
except: plot_alpha = 0.5
try: plot_color = plot_specs[i_profile]['plot_color']
except: plot_color = colors_rgb[i_profile]
if plot_yaxis == 'right':
axtw = ax[dd].twinx()
n_plot_right += 1
if plot_type == 'bar':
axtw.bar(time, plot_data, width = dt, color = plot_color, align = 'edge', fill = False, label = plot_label, alpha = plot_alpha)
else:
axtw.plot(time + dt/2, plot_data, color = plot_color, linestyle = plot_linestyle, marker = plot_marker, label = plot_label)
if np.max(plot_data) > ymax_right: ymax_right = np.max(plot_data)
if np.min(plot_data) < ymin_right: ymin_right = np.min(plot_data)
else:
n_plot_left += 1
if plot_type == 'bar':
ax[dd].bar(time, plot_data, width = dt, color = plot_color, align = 'edge', label = plot_label, alpha = plot_alpha)
else:
ax[dd].plot(time + dt/2, plot_data, color = plot_color, linestyle = plot_linestyle, marker = plot_marker, label = plot_label)
if np.max(plot_data) > ymax_left: ymax_left = np.max(plot_data)
if np.min(plot_data) < ymin_left: ymin_left = np.min(plot_data)
# Making the subplot looking nice
# x-axis
ax[dd].set_xlabel(fig_specs['xaxis_label'], fontsize = fontsize_labels)
ax[dd].set_xlim([time[0], time[-1] + dt])
# Set one tick each hour on the x-axis
ax[dd].xaxis.set_major_locator(plt.MaxNLocator(24))
# ax[dd].set_xticks(list(time[: : int(60*ts/dt)]))
ax[dd].tick_params(axis ='both', labelsize = fontsize_ticks)
# y-axis left
ax[dd].set_ylabel(fig_specs['yaxis_left_label'], fontsize = fontsize_labels)
ax[dd].set_ylim([0.9*ymin_left, 1.1*ymax_left])
ax[dd].grid(axis = 'y')
max_col = 2; ncol = min(max_col, n_plot_left)
ax[dd].legend(loc = 'upper left', ncol = ncol, fontsize = fontsize_legend)
title = '{}, {}'.format(fig_specs['title'].capitalize(), day)
ax[dd].set_title(title, fontsize = fontsize_title)
# If something has been plotted on the right y-axis:
if n_plot_right > 0: