forked from sbradnam/JADE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotter.py
1149 lines (959 loc) · 39 KB
/
plotter.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 Tue Jan 14 16:44:45 2020
# @author: Davide Laghi
# Copyright 2021, the JADE Development Team. All rights reserved.
# This file is part of JADE.
# JADE is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# JADE is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with JADE. If not, see <http://www.gnu.org/licenses/>.
import os
import math
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from matplotlib.ticker import (LogLocator, AutoMinorLocator, MultipleLocator,
AutoLocator)
from matplotlib.markers import CARETUPBASE
from matplotlib.markers import CARETDOWNBASE
from matplotlib.patches import Patch
from matplotlib.lines import Line2D
from scipy.interpolate import interp1d
from matplotlib.patches import Rectangle
# ============================================================================
# Specify parameters for plots
# ============================================================================
DEFAULT_EXTENSION = '.png'
SMALL_SIZE = 22
MEDIUM_SIZE = 26
BIGGER_SIZE = 30
plt.rc('font', size=SMALL_SIZE) # controls default text sizes
plt.rc('axes', titlesize=BIGGER_SIZE) # fontsize of the axes title
plt.rc('axes', labelsize=MEDIUM_SIZE) # fontsize of the x and y labels
plt.rc('xtick', labelsize=SMALL_SIZE) # fontsize of the tick labels
plt.rc('ytick', labelsize=SMALL_SIZE) # fontsize of the tick labels
plt.rc('legend', fontsize=SMALL_SIZE) # legend fontsize
plt.rc('figure', titlesize=BIGGER_SIZE) # fontsize of the figure title
plt.rc('lines', markersize=12) # Marker default size
# ============================================================================
# Specific data for benchmarks plots
# ============================================================================
# --- TBM HCPB ---
TBM_HCPB_RECT = [['Void', 'White', 840, 850.3],
['Eurofer97', '#377eb8', 850.3, 850.6],
['Water cooled Eurofer97', '#ff7f00', 850.6, 851.3],
['Eurofer97', '#377eb8', 851.3, 853.3],
['Breeding Area pt1', '#4daf4a', 853.3, 855.4],
['Breeding Area pt2', '#f781bf', 855.4, 859.9],
['Breeding Area pt3', '#a65628', 859.9, 893.3],
['Breeding Unit Pipework', '#984ea3', 893.3, 918.8],
['Gap', '#999999', 918.8, 946.3],
['SS316L(N)-IG/Water', '#e41a1c', 946.3, 1084.2]]
TBM_HCPB_RECT = pd.DataFrame(TBM_HCPB_RECT)
TBM_HCPB_RECT.columns = ['name', 'color', 'xmin', 'xmax']
XLIM_HCPB = (830, 1090)
# --- TBM WCLL ---
TBM_WCLL_RECT = [['Void', 'White', 840, 850.3],
['Eurofer97', '#377eb8', 850.3, 850.6],
['Water cooled Eurofer97', '#ff7f00', 850.6, 851.3],
['Eurofer97', '#377eb8', 851.3, 853.3],
['Breeding Area pt1', '#4daf4a', 853.3, 854.3],
['Breeding Area pt2', '#f781bf', 854.3, 862.5],
['Breeding Area pt3', '#a65628', 862.5, 903.4],
['Breeding Unit Pipework', '#984ea3', 903.4, 918.8],
['Gap', '#999999', 918.8, 946.3],
['SS316L(N)-IG/Water', '#e41a1c', 946.3, 1084.2]]
TBM_WCLL_RECT = pd.DataFrame(TBM_WCLL_RECT)
TBM_WCLL_RECT.columns = ['name', 'color', 'xmin', 'xmax']
XLIM_WCLL = (830, 1090)
# --- ITER 1D ---
ADD_LABELS_ITER1D = {'major': [('INBOARD', 0.21), ('PLASMA', 0.45),
('OUTBOARD', 0.70)],
'minor': [('TF Coil', 0.1), ('VV', 0.26),
('FW/B/S', 0.37), ('FW/B/S', 0.55),
('VV', 0.70), ('TF Coil', 0.87)]}
VERT_LINES_ITER1D = {'major': [49, 53], 'minor': [23, 32, 70, 84]}
# --- ITER CYLINDER SDDR ---
CYL_SDDR_XTICKS = {"'22-1'": 'Hole Front',
"'22-2'": 'Cyl. Front',
"'22-3'": 'Gap Front',
"'22-4'": 'SS Front',
"'23-1'": 'Hole Rear',
"'23-2'": 'Cyl. Rear',
"'23-3'": 'Gap Rear',
"'23-4'": 'SS Rear',
"'24-1'": 'P. Front (Hole)',
"'24-2'": 'P. Front (Cyl.)',
"'24-3'": 'P. Front (Gap)',
"'24-4'": 'SS Back Front',
"'25-1'": 'P. Rear (Hole)',
"'25-2'": 'P. Rear (Cyl.)',
"'25-3'": 'P. Rear (Gap)',
"'25-4'": 'SS Back Rear'}
# ============================================================================
# Plotter Class
# ============================================================================
class Plotter:
def __init__(self, data, title, outpath, outname, quantity, unit, xlabel,
testname, ext=DEFAULT_EXTENSION):
"""
Object Handling plots
Parameters
----------
data : list
data = [data1, data2, ...]
data1 = {'x': x data, 'y': y data, 'err': error data,
'ylabel': data label}
title : str
plot title
outpath : str/path
path to save image
outname : str
name of the image file
quantity : str
quantity of the y axis
unit : str
unit of the y axis
xlabel : str
name of the x axis
testname : str
name of the benchmark
ext : str
extension of the image to save. Default is '.png'
Returns
-------
None.
"""
self.data = data
self.title = title
self.outpath = os.path.join(outpath, outname+ext)
self.xlabel = xlabel
self.unit = unit
self.quantity = quantity
self.testname = testname
# --- Useful plots parameters ---
# May be improved in the future with additional markers and colors
# plot decorators
self.markers = ['o', 's', 'D', '^', 'X', 'p', 'd', '*']*50
# Color-blind saver palette
self.colors = ['#377eb8', '#ff7f00', '#4daf4a', '#f781bf', '#a65628',
'#984ea3', '#999999', '#e41a1c', '#dede00']*50
def plot(self, plot_type):
"""
Function to be called to actually perform the plot
Parameters
----------
plot_type : str
plot type. The current available ones are ['Binned graph',
'Ratio graph', 'Experimental points',
'Discreet Experimental points', 'Grouped bars', 'Waves'].
Raises
------
ValueError
if plot type is not among the available ones.
Returns
-------
outp : path like object
path to the saved image.
"""
# --- Binned Plot ---
if plot_type == 'Binned graph':
outp = self._binned_plot()
# --- Ratio Plot ---
elif plot_type == 'Ratio graph':
if self.testname == 'ITER_1D': # Special actions for ITER 1D
outp = self._ratio_plot(additional_labels=ADD_LABELS_ITER1D,
v_lines=VERT_LINES_ITER1D)
elif self.testname == 'HCPB_TBM_1D':
outp = self._ratio_plot(recs=TBM_HCPB_RECT, xlimits=XLIM_HCPB,
markers=True, figsize=(24, 13.5))
elif self.testname == 'WCLL_TBM_1D':
outp = self._ratio_plot(recs=TBM_WCLL_RECT, xlimits=XLIM_WCLL,
markers=True, figsize=(24, 13.5))
else:
outp = self._ratio_plot()
# --- Experimental Points Plot ---
elif plot_type == 'Experimental points':
outp = self._exp_points_plot()
# --- Experimental Points Plot ---
elif plot_type == 'Discreet Experimental points':
outp = self._exp_points_discreet_plot()
# --- Grouped bars chart ---
elif plot_type == 'Grouped bars':
if self.testname == 'C_Model':
log = True
xlegend = None
elif self.testname == 'ITER_Cyl_SDDR':
log = True
xlegend = CYL_SDDR_XTICKS
else:
log = False
xlegend = None
outp = self._grouped_bar(log=log, xlegend=xlegend)
# --- Waves plot ---
elif plot_type == 'Waves':
outp = self._waves()
# --- Deafault ---
else:
raise ValueError(plot_type+' is not an admissible plot type')
return outp
def _waves(self, upperlimit=1.5, lowerlimit=0.5):
"""
Built a multirow ratio that correlates different results on the same
x bin
Parameters
----------
upperlimit: float
set the y upper limit for the ratio plot
lowerlimit: float
set the y lower limit for the ratio plot
Returns
-------
outpath
path to the saved image.
"""
nrows = len(self.quantity)
fig, axes = plt.subplots(figsize=(18, 13.5), nrows=nrows, sharex=True)
fig.suptitle(self.title, weight='bold')
# common to all axes
for i, ax in enumerate(axes):
# Plot
refy = np.array(self.data[0]['y'][i])
for j, libdata in enumerate(self.data[1:]):
tary = np.array(libdata['y'][i])
y = tary/refy
# Compute the plot limits
norm, upper, lower = _get_limits(lowerlimit, upperlimit,
y, libdata['x'])
# This Should ensure that the x labels order is kept fixed
axes[i].scatter(libdata['x'], np.ones(len(libdata['x'])),
alpha=0)
# Plot everything
axes[i].scatter(norm[0], norm[1], color=self.colors[j],
marker=self.markers[i],
label=libdata['ylabel'])
axes[i].scatter(upper[0], upper[1], marker=CARETUPBASE,
c=self.colors[j])
axes[i].scatter(lower[0], lower[1], marker=CARETDOWNBASE,
c=self.colors[j])
# Write title
ax.set_title('{}'.format(self.quantity[i]))
# Draw the ratio line
ax.axhline(1, color='black', linestyle='--')
# Get minor ticks on the y axis
ax.yaxis.set_minor_locator(AutoMinorLocator())
# Ticks style
ax.tick_params(which='major', width=1.00, length=5)
ax.tick_params(which='minor', width=0.75, length=2.50)
# Grid stylying
ax.grid('True', which='major', linewidth=0.75, axis='y')
ax.grid('True', which='minor', linewidth=0.30, axis='y')
# limits
toadd = (upperlimit-lowerlimit)/8
ax.set_ylim(lowerlimit-toadd, upperlimit+toadd)
ax.axhline(lowerlimit, color='red', linewidth=0.5)
ax.axhline(upperlimit, color='red', linewidth=0.5)
# Add the legend
axes[0].legend(loc='upper center', bbox_to_anchor=(0.88, 1.5),
fancybox=True, shadow=True)
# Handle x and y global axes
plt.setp(axes[-1].get_xticklabels(), rotation=45, ha="right",
rotation_mode="anchor")
axes[-1].set_xlabel(self.xlabel)
return self._save()
def _grouped_bar(self, log=False, maxgroups=35, xlegend=None,
minspread=2):
"""
Plot a grouped bar chart on a "categorical" x axis.
Parameters
----------
log : Bool, optional
if True the y-axis is set to be logaritimic. The default is False.
maxgroups : int, optional
indicated the maximum number of grouped bars to plot in a single
axis. In case the data to plot is higher, new axis are created
vertically. The default is 30.
xlegend : dic, optional
allows to change the x ticks labels for better plot clarity.
The default is None.
minspread: float
minimum spread necessary to keep the log option on the y axis.
expressed in order of magnitudes. The default is 2.
Returns
-------
outpath : str/path
path to the saved image
"""
# Override log parameter if variation is low on y axis
if log:
spread = checkYspread(self.data)
if spread <= minspread:
log = False
# Override x ticks labels if requested
if xlegend is None:
labels = self.data[0]['x']
else:
labels = []
for lab in self.data[0]['x']:
lab = repr(lab)
try:
labels.append(xlegend[lab])
except KeyError:
labels.append(lab)
single_width = 0.35 # the width of the bars
tot_width = single_width*len(self.data)
# Check if the data is higher than max
if len(labels) > maxgroups:
nrows = int(len(labels)/maxgroups)+1 # rows of the plot
nlabels = maxgroups # number of labels in first row
else:
nlabels = len(labels) # number of labels in first row
nrows = 1
# Compute the position of the labels in the different rows
# and the datasets
x_array = []
datasets = []
label_chunks = []
added_labels = 0
for i in range(nrows):
x = np.arange(nlabels) # the label locations
x_array.append(x)
lab_chunk = labels[added_labels: added_labels+nlabels]
label_chunks.append(lab_chunk)
# Select the correspondent dataset
data = []
for libdata in self.data:
chunks = {}
for key, item in libdata.items():
if key == 'ylabel':
chunks[key] = item
else:
chunks[key] = item[added_labels: added_labels+nlabels]
data.append(chunks)
datasets.append(data)
# Adjourn nlabels
added_labels += nlabels
if len(labels)-added_labels > maxgroups:
nlabels = maxgroups
else:
nlabels = len(labels)-added_labels
# Initialize plot
fig, axes = plt.subplots(figsize=(18, 13.5), nrows=nrows)
# Always want axes as a list even if it is only one
try:
iterator = iter(axes)
except TypeError:
# not iterable
axes = [axes]
# --- Plotting ---
# Set the title only in the top ax
axes[0].set_title(self.title)
# Plot everything
for ax, datachunk, x, labels in zip(axes, datasets, x_array,
label_chunks):
pos = -tot_width/2
for dataset in datachunk:
ax.bar(x + pos, dataset['y'], single_width,
label=dataset['ylabel'],
yerr=dataset['err']*dataset['y'],
align='edge')
pos = pos+single_width # Adourn relative position
# log scale optional
if log:
ax.set_yscale('log')
ax.yaxis.set_major_locator(LogLocator())
else:
ax.yaxis.set_major_locator(AutoLocator())
# --- Plot details ---
# Legend and ticks
ax.legend(loc='best')
ax.tick_params(which='major', width=1.00, length=5)
ax.tick_params(which='minor', width=0.75, length=2.50)
# title and labels
ylabel = self.quantity+' ['+self.unit+']'
ax.set_ylabel(ylabel)
ax.set_xlabel(self.xlabel)
# Special for x labels
ax.set_xticks(x)
ax.set_xticklabels(labels, rotation=60)
# Grid
ax.grid('True', which='major', linewidth=0.75, axis='y')
ax.grid('True', which='minor', linewidth=0.30, axis='y')
return self._save()
def _exp_points_plot(self, y_scale='log', markersize=6):
"""
Plot a simple plot that compares experimental data points with
computational calculation.
Also a C/E plot is added
Parameters
----------
y_scale: str
acceppted values are the ones of matplotlib.axes.Axes.set_yscale
e.g. "linear", "log", "symlog", "logit", ... The default is 'log'.
markersize: float
size of the markers for experimental plots.
Returns
-------
outpath : str/path
path to the saved image
"""
data = self.data
ref = data[0]
# Adjounrn ylabel
ylabel = self.quantity+' ['+self.unit+']'
# Grid info
gridspec_kw = {'height_ratios': [3, 1], 'hspace': 0.13}
figsize = (18, 13.5)
# Initialize plot
fig, axes = plt.subplots(nrows=2, ncols=1, sharex=True,
figsize=figsize,
gridspec_kw=gridspec_kw)
ax1 = axes[0]
ax2 = axes[1]
# Plot referece
ax1.plot(ref['x'], ref['y'], 's', color=self.colors[0],
label=ref['ylabel'], markersize=markersize)
# Get the linear interpolation for C/E
interpolate = interp1d(ref['x'], ref['y'], fill_value=0,
bounds_error=False)
# Plot all data
try:
for i, dic in enumerate(data[1:]):
# Plot the flux
ax1.plot(dic['x'], dic['y'], color=self.colors[i+1],
drawstyle='steps-pre', label=dic['ylabel'])
# plot the C/E
interp_ref = interpolate(dic['x'])
ax2.plot(dic['x'], dic['y']/interp_ref, color=self.colors[i+1],
drawstyle='steps-pre', label=dic['ylabel'])
except KeyError:
# it is a single pp
return self._save()
# --- Plot details ---
# ax 1 details
ax1.set_yscale(y_scale)
ax1.set_title(self.title)
ax1.set_ylabel(ylabel)
ax1.legend(loc='best')
# limit the ax 2 to [0, 2]
ax2.set_ylim(bottom=0, top=2)
ax2.set_ylabel('C/E')
yticks = np.arange(0, 2.5, 0.5)
ax2.set_yticks(yticks)
ax2.set_xlabel(self.xlabel)
ax2.axhline(y=1, linestyle='--', color='black')
# # Draw the exp error
# ax2.fill_between(ref['x'], 1+ref['err'], 1-ref['err'], alpha=0.2)
# Common for all axes
for ax in axes:
ax.set_xscale('log')
# # Tiks positioning and dimensions
# ax.xaxis.set_major_locator(AutoLocator())
# ax.yaxis.set_major_locator(AutoLocator())
# ax.xaxis.set_minor_locator(AutoMinorLocator())
# ax.yaxis.set_minor_locator(AutoMinorLocator())
ax.tick_params(which='major', width=1.00, length=5)
ax.tick_params(which='minor', width=0.75, length=2.50)
# Grid
ax.grid('True', which='major', linewidth=0.50)
ax.grid('True', which='minor', linewidth=0.20)
return self._save()
def _exp_points_discreet_plot(self, y_scale='log', lowerlimit=0.5,
upperlimit=1.5):
"""
Plot a simple plot that compares experimental data points with
computational calculation. Differently from _exp_points_plot here
the computational results are reported in a descreet format.
Also a C/E plot is added
Parameters
----------
y_scale: str
acceppted values are the ones of matplotlib.axes.Axes.set_yscale
e.g. "linear", "log", "symlog", "logit", ... The default is 'log'.
upperlimit: float
set the y upper limit for the ratio plot
lowerlimit: float
set the y lower limit for the ratio plot
Returns
-------
outpath : str/path
path to the saved image
"""
data = self.data
# Adjourn ylabel
ylabel = self.quantity+' ['+self.unit+']'
# Grid info
gridspec_kw = {'height_ratios': [3, 1], 'hspace': 0.13}
figsize = (18, 13.5)
# Initialize plot
fig, axes = plt.subplots(nrows=2, ncols=1, sharex=True,
figsize=figsize,
gridspec_kw=gridspec_kw)
ax1 = axes[0]
ax2 = axes[1]
ref_x = data[0]['x']
ref_y = data[0]['y']
# -- Main plot --
for i, libdata in enumerate(data):
x = libdata['x']
y = libdata['y']
err = libdata['err']
label = libdata['ylabel']
ax1.errorbar(x, y, yerr=err, marker=self.markers[i], linestyle='',
capsize=10, color=self.colors[i], label=label)
# -- C/E --
for i, libdata in enumerate(data[1:]):
x = libdata['x']
y = libdata['y']
label = libdata['ylabel']
try:
assert (x == ref_x).all()
except AttributeError:
# it may be directly a boolean
assert (x == ref_x)
# Compute the plot limits
norm, upper, lower = _get_limits(lowerlimit, upperlimit,
y/ref_y, x)
# This Should ensure that the x labels order is kept fixed
ax2.scatter(x, np.ones(len(x)), alpha=0)
# Plot everything
ax2.scatter(norm[0], norm[1], color=self.colors[i+1],
marker=self.markers[i+1], label=label)
ax2.scatter(upper[0], upper[1], marker=CARETUPBASE,
c=self.colors[i+1])
ax2.scatter(lower[0], lower[1], marker=CARETDOWNBASE,
c=self.colors[i+1])
# --- Plot details ---
# ax 1 details
ax1.set_yscale(y_scale)
ax1.set_title(self.title)
ax1.set_ylabel(ylabel)
ax1.legend(loc='best')
# ax 2 details
toadd = (upperlimit-lowerlimit)/8
ax2.set_ylim(bottom=lowerlimit-toadd, top=upperlimit+toadd)
ax2.set_ylabel('C/E')
ax2.set_xlabel(self.xlabel)
ax2.axhline(y=1, linestyle='--', color='black')
yticks = np.arange(0, 2.5, 0.5)
ax2.set_yticks(yticks)
plt.setp(ax2.get_xticklabels(), rotation=45, ha="right",
rotation_mode="anchor")
ax2.axhline(lowerlimit, color='red', linewidth=0.5)
ax2.axhline(upperlimit, color='red', linewidth=0.5)
# Common for all axes
for ax in axes:
ax.tick_params(which='major', width=1.00, length=5)
ax.tick_params(which='minor', width=0.75, length=2.50)
# Grid
ax.grid('True', which='major', linewidth=0.50)
ax.grid('True', which='minor', linewidth=0.20)
return self._save()
def _ratio_plot(self, additional_labels=None, v_lines=None, recs=None,
xlimits=None, markers=False, figsize=(16, 9)):
"""
Plot a ratio plot where all data dictionaries are plotted against the
first one which is used as reference
Parameters
----------
additional_labels : dic, optional
contains additional tags to print in the plot.
{'major': [(label, xpos), ...], 'minor': [(label, xpos), ...]}.
The default is None.
v_lines : dic, optional
contains additional vertical lines to plot.
{'major': [xpos, ...], 'minor': [xpos, ...]}.
The default is None.
recs : pd.DataFrame, optional
contains the data to draw rectangles on the plot. Columns values
are ['name', 'color', 'xmin', 'xmax']. The default is None
xlimits : tuple
(xmin, xmax). The default is None.
markers : bool
if True markers are applied to the line plots.
The default is False.
figsize : (int, int)
determines the size of the plot. default is (16, 9)
Returns
-------
outpath : str/path
path to the saved image
"""
data = self.data
ref = data[0]
# Adjounrn ylabel
ylabel = 'Ratio of '+self.quantity+' (vs. '+ref['ylabel']+')'
# Initialize plot
fig, ax = plt.subplots(figsize=figsize)
# Plot all data
y_max = 0
y_min = 0
try:
for i, dic in enumerate(data[1:]):
y = dic['y']/ref['y']
# Adjourn y max and min
if i == 0:
y_max = max(y)
y_min = min(y)
else:
if max(y) > y_max:
y_max = max(y)
if min(y) < y_min:
y_min = min(y)
# Plot
if markers:
marker = self.markers[i]
else:
marker = None
ax.plot(dic['x'], y, color=self.colors[i],
drawstyle='steps-mid', label=dic['ylabel'],
marker=marker)
except KeyError:
# it is a single pp
return self._save()
# Plot details
ax.set_title(self.title)
ax.legend(loc='best')
ax.set_xlabel(self.xlabel)
ax.set_ylabel(ylabel)
# Tiks positioning and dimensions
ax.xaxis.set_major_locator(AutoLocator())
ax.yaxis.set_major_locator(AutoLocator())
ax.xaxis.set_minor_locator(AutoMinorLocator())
ax.yaxis.set_minor_locator(AutoMinorLocator())
ax.tick_params(which='major', width=1.00, length=5)
ax.tick_params(which='minor', width=0.75, length=2.50)
# Grid
ax.grid('True', which='major', linewidth=0.50)
ax.grid('True', which='minor', linewidth=0.20)
# Add additional labels if requested
if additional_labels is not None:
# major labels
labels = additional_labels['major']
for label, xpos in labels:
bbox_dic = {'boxstyle': 'round,pad=0.5', 'facecolor': 'white',
'alpha': 1}
ax.text(xpos, 0.95, label,
bbox=bbox_dic, transform=ax.transAxes)
# minor labels
labels = additional_labels['minor']
for label, xpos in labels:
ax.text(xpos, 0.87, label, transform=ax.transAxes)
# Add vertical lines if requested
if v_lines is not None:
# major lines
lines = v_lines['major']
for line in lines:
ax.axvline(line, color='black')
# minor lines
lines = v_lines['minor']
for line in lines:
ax.axvline(line, color='black', ymin=0.10, ymax=0.90,
linestyle='--', linewidth=1)
# Add Rectangles if requested
if recs is not None:
# Plot the rects
height = y_max-y_min
_add_recs(ax, recs, height, y_origin=y_min)
# Build the additional legend
# Drop duplicates
df = TBM_HCPB_RECT[['color', 'name']].drop_duplicates()
legend_elements = []
for key, row in df.iterrows():
patch = Patch(facecolor=row['color'], edgecolor='black',
label=row['name'], alpha=0.2)
legend_elements.append(patch)
additional_legend = ax.legend(handles=legend_elements,
loc='upper center',
bbox_to_anchor=(0.5, -0.1),
fancybox=True,
ncol=5,
shadow=True)
# Normal legend needs to be reprinted
ax.legend(loc='best')
# And now the custom one
ax.add_artist(additional_legend)
# Limit the x-axis if needed
if xlimits is not None:
ax.set_xlim(xlimits[0], xlimits[1])
return self._save()
def _binned_plot(self, normalize=False):
"""
PLot composed by three subplots.
Main plot -> binned values (e.g. a flux in energies)
Error plot -> statistical error
Ratio plot (Optional) -> ratio among reference and target values
Parameters
----------
Returns
-------
outpath : str/path
path to the saved image
"""
# General parameters
data = self.data
title = self.title
colors = self.colors
ylabel = self.quantity+' ['+self.unit+']'
if len(data) > 1:
nrows = 3
else:
nrows = 2
# Set properties for the plot spacing
if len(data) > 1:
gridspec_kw = {'height_ratios': [4, 1, 1], 'hspace': 0.13}
else:
gridspec_kw = {'height_ratios': [4, 1], 'hspace': 0.13}
# Initiate plot
fig, axes = plt.subplots(nrows=nrows, ncols=1, sharex=True,
figsize=(18, 13.5),
gridspec_kw=gridspec_kw)
# --- Main plot ---
ax1 = axes[0]
ax1.set_title(title)
# Labels
ax1.set_ylabel(ylabel)
# Ticks
subs = (0.2, 0.4, 0.6, 0.8)
ax1.set_xscale('log')
ax1.set_yscale('log')
ax1.xaxis.set_major_locator(LogLocator(base=10, numticks=15))
ax1.yaxis.set_major_locator(LogLocator(base=10, numticks=15))
ax1.xaxis.set_minor_locator(LogLocator(base=10.0, subs=subs,
numticks=12))
ax1.yaxis.set_minor_locator(LogLocator(base=10.0, subs=subs,
numticks=12))
# --- Error Plot ---
ax2 = axes[1]
ax2.axhline(y=10, linestyle='--', color='black')
ax2.set_ylabel('1σ [%]', labelpad=35)
ax2.set_yscale('log')
ax2.set_ylim(bottom=0, top=100)
ax2.yaxis.set_major_locator(LogLocator(base=10, numticks=15))
ax2.yaxis.set_minor_locator(LogLocator(base=10.0, subs=subs,
numticks=12))
# --- Comparison Plot ---
if len(data) > 1:
ax3 = axes[2]
ax3.axhline(y=1, linestyle='--', color='black')
ax3.set_ylabel('$T_i/R$', labelpad=30)
ax3.yaxis.set_major_locator(MultipleLocator(0.5))
ax3.yaxis.set_minor_locator(AutoMinorLocator(5))
ax3.axhline(y=2, linestyle='--', color='red', linewidth=0.5)
ax3.axhline(y=0.5, linestyle='--', color='red', linewidth=0.5)
ax3.set_ylim(bottom=0.3, top=2.2)
# Generate X axis for bin properties
oldX = np.array([0]+list(data[0]['x']))
base = np.log(oldX[:-1])
shifted = np.log(oldX[1:])
newX = np.exp((base+shifted)/2)
newX[0] = (oldX[1]+oldX[0])/2
# --- Plot Data ---
for idx, dic_data in enumerate(data):
x = np.array([0]+list(dic_data['x']))
y = np.array([0]+list(dic_data['y']))
if normalize:
# Find global area
hist_areas = np.diff(x)*y[1:]
tot_area = hist_areas.sum()
# Normalize values
y = [0]+list(np.diff(x)*y[1:]/tot_area)
err = np.array(dic_data['err'])
err_multi = np.array(y[1:])*np.abs(err)
# Main plot
if idx > 0:
tag = 'T'+str(idx)+': '
else:
tag = 'R: '
ax1.step(x, y, label=tag+dic_data['ylabel'], color=colors[idx])
ax1.errorbar(newX, y[1:], linewidth=0,
yerr=err_multi, elinewidth=0.5, color=colors[idx])
# Error Plot
ax2.plot(newX, np.array(dic_data['err'])*100, 'o',
label=dic_data['ylabel'], markersize=2,
color=colors[idx])
# Comparison
if len(data) > 1:
for idx, dic_data in enumerate(data[1:]):
ratio = np.array(dic_data['y'])/np.array(data[0]['y'])
# Uniform plots actions
norm, upper, lower = _get_limits(0.5, 2, ratio, newX)
ax3.plot(norm[0], norm[1], 'o', markersize=2,
color=colors[idx+1])
ax3.scatter(upper[0], upper[1], marker=CARETUPBASE, s=50,
c=colors[idx+1])
ax3.scatter(lower[0], lower[1], marker=CARETDOWNBASE, s=50,
c=colors[idx+1])
# Build ax3 legend
leg = [Line2D([0], [0], marker=CARETUPBASE, color='black',
label='> 2', markerfacecolor='black',
markersize=8, lw=0),
Line2D([0], [0], marker=CARETDOWNBASE,
color='black', label='< 0.5',
markerfacecolor='black', markersize=8, lw=0)]
ax3.legend(handles=leg, loc='best')
# Final operations
ax1.legend(loc='best')
axes[-1].set_xlabel(self.xlabel)
# --- Common Features ---
for ax in axes:
# Grid control
ax.grid()
ax.grid('True', which='minor', linewidth=0.25)
# Ticks
ax.tick_params(which='major', width=1.00, length=5)
ax.tick_params(which='minor', width=0.75, length=2.50)
return self._save()
def _contribution(self, yscale='linear', legend_outside='False'):
data = self.data
# Adjounrn ylabel
ylabel = self.quantity+' ['+self.unit+']'
# Grid info
# gridspec_kw = {'height_ratios': [3, 1], 'hspace': 0.13}
figsize = (22, 15)
# Initialize plot
fig, ax = plt.subplots(figsize=figsize)
# Plot all data
for i, libdata in enumerate(data):
ax.plot(libdata['x'], libdata['y'], color=self.colors[i],
marker=self.markers[i], label=libdata['ylabel'])
# --- Plot details ---
# ax details
ax.set_yscale(yscale)
ax.set_title(self.title)
ax.set_ylabel(ylabel)
ax.set_xlabel(self.xlabel)
if legend_outside:
ax.legend(bbox_to_anchor=(1, 1))
else:
ax.legend(loc='best')
ax.tick_params(which='major', width=1.00, length=5)
ax.tick_params(axis='y', which='minor', width=0.75, length=2.50)
plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
rotation_mode="anchor")
# Grid
ax.grid('True', axis='y', which='major', linewidth=0.50)
ax.grid('True', axis='y', which='minor', linewidth=0.20)