def plot_histograms()

in src/lookoutequipment/plot.py [0:0]


    def plot_histograms(self, freq='1min', prediction_index=0, top_n=8, fig_width=18, start=None, end=None):
        """
        Plot values distribution as histograms for the top contributing sensors.
        
        Parameters:
            freq (string):
                The datetime index frequence (defaults to '1min'). This must 
                be a string following this format: XXmin where XX is a number
                of minutes.
            prediction_index (integer):
                You can add several predicted ranges in your plot. Use this
                argument to specify for which one you wish to plot a histogram
                for (defaults to 0)
            top_n (integer):
                Number of top signals to plot (default: 8)
            fig_width (float):
                Width of the figure generated (default: 18)
            start (pandas.DatetTime):
                Start date of the range to build the values distribution for
                (default: None, use the evaluation period start)
            end (pandas.DatetTime):
                End date of the range to build the values distribution for
                (default: None, use the evaluation period end)
                
        Returns:
            matplotlib.pyplot.figure: a figure where the histograms are drawn
        """
        predicted_ranges_df = self._predictions_df[prediction_index]    
        abnormal_index = predicted_ranges_df[predicted_ranges_df['Label'] > 0.0].index
        normal_index = predicted_ranges_df[predicted_ranges_df['Label'] == 0.0].index
        expanded_results = self._build_feature_importance_dataframe(
            freq=freq, 
            prediction_index=prediction_index
        )
        
        if (start is not None) and (end is not None):
            abnormal_index = predicted_ranges_df[predicted_ranges_df['Label'] > 0.0]
            abnormal_index = abnormal_index.loc[start:end].index
            normal_index = predicted_ranges_df[predicted_ranges_df['Label'] == 0.0]
            normal_index = normal_index.loc[start:end].index
            expanded_results = expanded_results.loc[start:end]
        
        most_contributing_signals = list(expanded_results.sum().sort_values(ascending=False).head(top_n).index)
        most_contributing_signals = [tag.split('\\')[1] for tag in most_contributing_signals]
        
        num_tags = len(most_contributing_signals)
        num_rows = int(np.ceil(num_tags / 4))
        fig_height = 5.0 * num_rows * fig_width / 24.0
        fig = plt.figure(figsize=(fig_width,fig_height))

        for i, current_tag in enumerate(most_contributing_signals):
            ax = fig.add_subplot(num_rows, 4, i+1)
            current_df = self._data[current_tag]

            ts1 = current_df.reindex(normal_index).copy()
            ts2 = current_df.reindex(abnormal_index).copy()

            plot_histogram_comparison(ts2,
                                      ts1,
                                      ax=ax, 
                                      label_timeseries_1=f'Values during abnormal events',
                                      label_timeseries_2=f'Values during normal periods',
                                      num_bins=50)
            ax.set_title(current_tag)
            
        return fig