def _add_regression_line(self, ax: plt.subplot, x: np.array, y: np.array): """ Build linear regression of x y data and plot on included ax Parameters ---------- ax pyplot AxesSubplot instance x numpy array, 1d y numpy array, 1d Returns ------- str regression line slope rounded to 6 places as string """ # nan not allowed for regression line filter_nan = np.logical_or(np.isnan(x), np.isnan(y)) x = x[~filter_nan] y = y[~filter_nan] slopes, intercepts, stderrs, percent_deviation = linear_regression(x, y) slope_label = np.round(slopes, 6) ax.plot(x, slopes * x + intercepts, label='y = {}x + {}'.format(slope_label, np.round(intercepts, 6)), color='red') ax.legend() return slope_label
def plot_allowable_percent_deviation(self, subplot: plt.subplot = None): """ Plot the correlation plot between ping time and percent deviation in the ping slope linear regression. Percent deviation here is related to the standard error of the y in the regression. Include bounds for invalid data in the plot as a filled in red area. According to source paper, greater than 5% should be rejected. Need to include segment identification in final version for exluding greater than 5% Parameters ---------- subplot pyplot AxesSubplot instance to add to, if None will generate new instance """ if subplot is None: subplot = plt.subplots(1)[1] subplot.plot(self.times, self.slope_percent_deviation) subplot.set_ylim(self.slope_percent_deviation.min(), np.max([6, self.slope_percent_deviation.max() * 1.5])) subplot.axhline(5, c='red', linestyle='--') subplot.fill_between(self.times, 5, np.max( [6, self.slope_percent_deviation.max() * 1.5]), color='red', alpha=0.2) subplot.set_title('Allowable Percent Deviation (red = data unusable)') subplot.set_xlabel('Time (s)') subplot.set_ylabel('% deviation')