def plot_signals()

in getting_started/utils/lookout_equipment_utils.py [0:0]


    def plot_signals(self, nb_cols=3, max_plots=12):
        """
        Once the histograms are computed, we can plot the top N signals by 
        decreasing ranking distance. By default, this will plot the signals for 
        the top 12 signals, with 3 plots per line. For each signal, this method
        will plot the normal values in green and the anomalies in red.
        
        PARAMS
        ======
            nb_cols: integer (default: 3)
                Number of plots to assemble on a given row
                
            max_plots: integer (default: 12)
                Number of signal to consider
                
        RETURNS
        =======
            fig: matplotlib.pyplot.figure
                A figure where the plots are drawn
                
            axes: list of matplotlib.pyplot.Axis
                An axis for each plot drawn here
        """
        # Prepare the figure:
        nb_rows = max_plots // nb_cols + 1
        plt.style.use('Solarize_Light2')
        prop_cycle = plt.rcParams['axes.prop_cycle']
        colors = prop_cycle.by_key()['color']
        fig = plt.figure(figsize=(28, int(nb_rows * 4)))
        gs = gridspec.GridSpec(nb_rows, nb_cols, hspace=0.5, wspace=0.25)
        axes = []
        for i in range(max_plots):
            axes.append(fig.add_subplot(gs[i]))
        
        # Loops through each signal by decreasing distance order:
        i = 0
        for tag, current_rank in self.rank.items():
            # We stop after reaching the number of plots we are interested in:
            if i > max_plots - 1:
                break

            # Get the anomaly and the normal values from the current signal:
            current_signal_evaluation = self.df_list[tag].loc[self.ts_label_evaluation, tag]
            current_signal_training = self.df_list[tag].loc[self.ts_normal_training, tag]
            current_signal_known = self.df_list[tag].loc[self.ts_known_anomalies, tag]

            # Plot both time series with a line plot
            # axes.append(plt.subplot(gs[i]))
            axes[i].plot(current_signal_training, 
                         linewidth=0.5, 
                         alpha=0.8, 
                         color=colors[1])
            axes[i].plot(current_signal_evaluation, 
                         linewidth=0.5, 
                         alpha=0.8, 
                         color=colors[5])
            axes[i].plot(current_signal_known, 
                         linewidth=0.5, 
                         alpha=0.8, 
                         color='#AAAAAA')

            # Title will be the tag name followed by the score:
            title = tag
            title += f' (score: {current_rank:.02f})'
            axes[i].set_title(title, fontsize=10)
            start = min(
                min(self.ts_label_evaluation),
                min(self.ts_normal_training), 
                min(self.ts_known_anomalies)
            )
            end = max(
                max(self.ts_label_evaluation),
                max(self.ts_normal_training), 
                max(self.ts_known_anomalies)
            )
            axes[i].set_xlim(start, end)

            i += 1
            
        return fig, axes