@@ -608,110 +608,110 @@ def calc_sensitivity(self, salib_method='sobol', method_kwargs=None):
608608
609609
610610 def plot_distribution (self , metric_list = None , figsize = None , log = False ):
611- """
612- Plot the distribution of values of the risk metrics over the sampled
613- input parameters (i.e. plot the "uncertainty" distributions).
614-
615- Parameters
616- ----------
617- metric_list: list, optional
618- List of metrics to plot the distribution.
619- The default is None.
620- figsize: tuple(int or float, int or float), optional
621- The figsize argument of matplotlib.pyplot.subplots()
622- The default is derived from the total number of plots (nplots) as:
623- nrows, ncols = int(np.ceil(nplots / 3)), min(nplots, 3)
624- figsize = (ncols * FIG_W, nrows * FIG_H)
625- log: boolean
626- Use log10 scale for x axis. Default is False
627-
628- Raises
629- ------
630- ValueError
631- If no metric distribution was computed the plot cannot be made.
632-
633- Returns
634- -------
635- axes: matplotlib.pyplot.axes
636- The axes handle of the plot.
637-
638- """
639- fontsize = 18 #default label fontsize
640-
641- if not self .metrics :
642- raise ValueError ("No uncertainty data present for these metrics. " +
643- "Please run an uncertainty analysis first." )
644-
645- if metric_list is None :
646- metric_list = self .metric_names
647-
648- df_values = pd .DataFrame ()
649- for metric in metric_list :
650- if metric not in METRICS_2D :
651- df_values = df_values .append (self .metrics [metric ])
652-
653- if log :
654- df_values_plt = df_values .apply (np .log10 ).copy ()
655- df_values_plt = df_values_plt .replace ([np .inf , - np .inf ], np .nan )
656- else :
657- df_values_plt = df_values .copy ()
658-
659- cols = df_values_plt .columns
660- nplots = len (cols )
661- nrows , ncols = int (np .ceil (nplots / 2 )), min (nplots , 2 )
662- if not figsize :
663- figsize = (ncols * FIG_W , nrows * FIG_H )
664- _fig , axes = plt .subplots (nrows = nrows ,
665- ncols = ncols ,
666- figsize = figsize )
667- if nplots > 1 :
668- flat_axes = axes .flatten ()
669- else :
670- flat_axes = np .array ([axes ])
671-
672- for ax , col in zip_longest (flat_axes , cols , fillvalue = None ):
673- if col is None :
674- ax .remove ()
675- continue
676- data = df_values_plt [col ]
677- if data .empty :
678- ax .remove ()
679- continue
680- data .hist (ax = ax , bins = 30 , density = True , histtype = 'bar' ,
681- color = 'lightsteelblue' , edgecolor = 'black' )
682- try :
683- data .plot .kde (ax = ax , color = 'darkblue' , linewidth = 4 , label = '' )
684- except np .linalg .LinAlgError :
685- pass
686- avg , std = df_values [col ].mean (), df_values [col ].std ()
687- _ , ymax = ax .get_ylim ()
688- if log :
689- avg_plot = np .log10 (avg )
690- else :
691- avg_plot = avg
692- ax .axvline (avg_plot , color = 'darkorange' , linestyle = 'dashed' , linewidth = 2 ,
693- label = "avg=%.2f%s" % u_vtm (avg ))
694- if log :
695- std_m , std_p = np .log10 (avg - std ), np .log10 (avg + std )
696- else :
697- std_m , std_p = avg - std , avg + std
698- ax .plot ([std_m , std_p ],
699- [0.3 * ymax , 0.3 * ymax ], color = 'black' ,
700- label = "std=%.2f%s" % u_vtm (std ))
701- ax .set_title (col )
702- if log :
703- ax .set_xlabel ('value [log10]' )
704- else :
705- ax .set_xlabel ('value' )
706- ax .set_ylabel ('density of events' )
707- ax .legend (fontsize = fontsize - 2 )
708-
709- ax .tick_params (labelsize = fontsize )
710- for item in [ax .title , ax .xaxis .label , ax .yaxis .label ]:
711- item .set_fontsize (fontsize )
712- plt .tight_layout ()
713-
714- return axes
611+ """
612+ Plot the distribution of values of the risk metrics over the sampled
613+ input parameters (i.e. plot the "uncertainty" distributions).
614+
615+ Parameters
616+ ----------
617+ metric_list: list, optional
618+ List of metrics to plot the distribution.
619+ The default is None.
620+ figsize: tuple(int or float, int or float), optional
621+ The figsize argument of matplotlib.pyplot.subplots()
622+ The default is derived from the total number of plots (nplots) as:
623+ nrows, ncols = int(np.ceil(nplots / 3)), min(nplots, 3)
624+ figsize = (ncols * FIG_W, nrows * FIG_H)
625+ log: boolean
626+ Use log10 scale for x axis. Default is False
627+
628+ Raises
629+ ------
630+ ValueError
631+ If no metric distribution was computed the plot cannot be made.
632+
633+ Returns
634+ -------
635+ axes: matplotlib.pyplot.axes
636+ The axes handle of the plot.
637+
638+ """
639+ fontsize = 18 #default label fontsize
640+
641+ if not self .metrics :
642+ raise ValueError ("No uncertainty data present for these metrics. " +
643+ "Please run an uncertainty analysis first." )
644+
645+ if metric_list is None :
646+ metric_list = self .metric_names
647+
648+ df_values = pd .DataFrame ()
649+ for metric in metric_list :
650+ if metric not in METRICS_2D :
651+ df_values = df_values .append (self .metrics [metric ])
652+
653+ if log :
654+ df_values_plt = df_values .apply (np .log10 ).copy ()
655+ df_values_plt = df_values_plt .replace ([np .inf , - np .inf ], np .nan )
656+ else :
657+ df_values_plt = df_values .copy ()
658+
659+ cols = df_values_plt .columns
660+ nplots = len (cols )
661+ nrows , ncols = int (np .ceil (nplots / 2 )), min (nplots , 2 )
662+ if not figsize :
663+ figsize = (ncols * FIG_W , nrows * FIG_H )
664+ _fig , axes = plt .subplots (nrows = nrows ,
665+ ncols = ncols ,
666+ figsize = figsize )
667+ if nplots > 1 :
668+ flat_axes = axes .flatten ()
669+ else :
670+ flat_axes = np .array ([axes ])
671+
672+ for ax , col in zip_longest (flat_axes , cols , fillvalue = None ):
673+ if col is None :
674+ ax .remove ()
675+ continue
676+ data = df_values_plt [col ]
677+ if data .empty :
678+ ax .remove ()
679+ continue
680+ data .hist (ax = ax , bins = 30 , density = True , histtype = 'bar' ,
681+ color = 'lightsteelblue' , edgecolor = 'black' )
682+ try :
683+ data .plot .kde (ax = ax , color = 'darkblue' , linewidth = 4 , label = '' )
684+ except np .linalg .LinAlgError :
685+ pass
686+ avg , std = df_values [col ].mean (), df_values [col ].std ()
687+ _ , ymax = ax .get_ylim ()
688+ if log :
689+ avg_plot = np .log10 (avg )
690+ else :
691+ avg_plot = avg
692+ ax .axvline (avg_plot , color = 'darkorange' , linestyle = 'dashed' , linewidth = 2 ,
693+ label = "avg=%.2f%s" % u_vtm (avg ))
694+ if log :
695+ std_m , std_p = np .log10 (avg - std ), np .log10 (avg + std )
696+ else :
697+ std_m , std_p = avg - std , avg + std
698+ ax .plot ([std_m , std_p ],
699+ [0.3 * ymax , 0.3 * ymax ], color = 'black' ,
700+ label = "std=%.2f%s" % u_vtm (std ))
701+ ax .set_title (col )
702+ if log :
703+ ax .set_xlabel ('value [log10]' )
704+ else :
705+ ax .set_xlabel ('value' )
706+ ax .set_ylabel ('density of events' )
707+ ax .legend (fontsize = fontsize - 2 )
708+
709+ ax .tick_params (labelsize = fontsize )
710+ for item in [ax .title , ax .xaxis .label , ax .yaxis .label ]:
711+ item .set_fontsize (fontsize )
712+ plt .tight_layout ()
713+
714+ return axes
715715
716716
717717 def plot_sample (self , figsize = None ):
0 commit comments