def autorange_plot(t, sig, gwin=<NUM_LIT:7>, swin=None, win=<NUM_LIT:30>,<EOL>on_mult=(<NUM_LIT>, <NUM_LIT:1.>), off_mult=(<NUM_LIT:1.>, <NUM_LIT>),<EOL>nbin=<NUM_LIT:10>, thresh=None):
if swin is None:<EOL><INDENT>swin = gwin // <NUM_LIT:2><EOL><DEDENT>sigs = fastsmooth(sig, swin)<EOL>bins = sig.size // nbin<EOL>kde_x = np.linspace(sig.min(), sig.max(), bins)<EOL>kde = gaussian_kde(sigs)<EOL>yd = kde.pdf(kde_x)<EOL>mins = findmins(kde_x, yd)  <EOL>if thresh is not None:<EOL><INDENT>mins = [thresh]<EOL><DEDENT>if len(mins) > <NUM_LIT:0>:<EOL><INDENT>bkg = sigs < (mins[<NUM_LIT:0>])  <EOL><DEDENT>else:<EOL><INDENT>bkg = np.ones(sig.size, dtype=bool)<EOL><DEDENT>fbkg = bkg<EOL>fsig = ~bkg<EOL>g = abs(fastgrad(sigs, gwin))  <EOL>zeros = bool_2_indices(fsig)<EOL>if zeros is not None:<EOL><INDENT>zeros = zeros.flatten()<EOL>lohi = []<EOL>pgs = []<EOL>excl = []<EOL>tps = []<EOL>failed = []<EOL>for z in zeros:  <EOL><INDENT>if z - win < <NUM_LIT:0>:<EOL><INDENT>lo = gwin // <NUM_LIT:2><EOL>hi = int(z + win)<EOL><DEDENT>elif z + win > (len(sig) - gwin // <NUM_LIT:2>):<EOL><INDENT>lo = int(z - win)<EOL>hi = len(sig) - gwin // <NUM_LIT:2><EOL><DEDENT>else:<EOL><INDENT>lo = int(z - win)<EOL>hi = int(z + win)<EOL><DEDENT>xs = t[lo:hi]<EOL>ys = g[lo:hi]<EOL>lohi.append([lo, hi])<EOL>mid = (hi + lo) // <NUM_LIT:2><EOL>tp = sigs[mid + <NUM_LIT:3>] > sigs[mid - <NUM_LIT:3>]  <EOL>tps.append(tp)<EOL>c = t[z]  <EOL>width = (t[<NUM_LIT:1>] - t[<NUM_LIT:0>]) * <NUM_LIT:2>  <EOL>try:<EOL><INDENT>pg, _ = curve_fit(gauss, xs, ys,<EOL>p0=(np.nanmax(ys),<EOL>c,<EOL>width),<EOL>sigma=(xs - c)**<NUM_LIT:2> + <NUM_LIT>)<EOL>pgs.append(pg)<EOL>fwhm = abs(<NUM_LIT:2> * pg[-<NUM_LIT:1>] * np.sqrt(<NUM_LIT:2> * np.log(<NUM_LIT:2>)))<EOL>if tp:<EOL><INDENT>lim = np.array([-fwhm, fwhm]) * on_mult + pg[<NUM_LIT:1>]<EOL><DEDENT>else:<EOL><INDENT>lim = np.array([-fwhm, fwhm]) * off_mult + pg[<NUM_LIT:1>]<EOL><DEDENT>excl.append(lim)<EOL>fbkg[(t > lim[<NUM_LIT:0>]) & (t < lim[<NUM_LIT:1>])] = False<EOL>fsig[(t > lim[<NUM_LIT:0>]) & (t < lim[<NUM_LIT:1>])] = False<EOL>failed.append(False)<EOL><DEDENT>except RuntimeError:<EOL><INDENT>failed.append(True)<EOL>lohi.append([np.nan, np.nan])<EOL>pgs.append([np.nan, np.nan, np.nan])<EOL>excl.append([np.nan, np.nan])<EOL>tps.append(tp)<EOL>pass<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>zeros = []<EOL><DEDENT>nrows = <NUM_LIT:2> + len(zeros) // <NUM_LIT:2> + len(zeros) % <NUM_LIT:2><EOL>fig, axs = plt.subplots(nrows, <NUM_LIT:2>, figsize=(<NUM_LIT:6>, <NUM_LIT:4> + <NUM_LIT> * nrows))<EOL>ax1, ax2, ax3, ax4 = axs.flat[:<NUM_LIT:4>]<EOL>ax4.set_visible(False)<EOL>for ax in [ax1, ax3]:<EOL><INDENT>p = ax.axes.get_position()<EOL>p2 = [p.x0, p.y0, p.width * <NUM_LIT>, p.height]<EOL>ax.axes.set_position(p2)<EOL><DEDENT>p = ax3.axes.get_position()<EOL>p2 = [p.x0, p.y0 + <NUM_LIT> * p.height, p.width, p.height]<EOL>ax3.axes.set_position(p2)<EOL>p = ax2.axes.get_position()<EOL>p2 = [p.x0 + p.width * <NUM_LIT>, p.y0, p.width * <NUM_LIT>, p.height]<EOL>ax2.axes.set_position(p2)<EOL>ax1.plot(t, sig, color='<STR_LIT:k>', lw=<NUM_LIT:1>)<EOL>ax1.set_xticklabels([])<EOL>ax1.set_ylabel('<STR_LIT>')<EOL>ax3.plot(t, g, color='<STR_LIT:k>', lw=<NUM_LIT:1>)<EOL>ax3.set_xlabel('<STR_LIT>')<EOL>ax3.set_ylabel('<STR_LIT>')<EOL>ax2.fill_betweenx(kde_x, yd, color=(<NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT>))<EOL>ax2.plot(yd, kde_x, color='<STR_LIT:k>')<EOL>ax2.set_ylim(ax1.get_ylim())<EOL>ax2.set_yticklabels([])<EOL>ax2.set_xlabel('<STR_LIT>')<EOL>for ax in [ax1, ax2]:<EOL><INDENT>ax.axhline(mins[<NUM_LIT:0>], color='<STR_LIT:k>', ls='<STR_LIT>', alpha=<NUM_LIT>)<EOL><DEDENT>if len(zeros) > <NUM_LIT:0>:<EOL><INDENT>for z in zeros:<EOL><INDENT>ax1.axvline(t[z], color='<STR_LIT:r>', alpha=<NUM_LIT:0.5>)<EOL>ax3.axvline(t[z], color='<STR_LIT:r>', alpha=<NUM_LIT:0.5>)<EOL><DEDENT>n = <NUM_LIT:1><EOL>for (lo, hi), lim, tp, pg, fail, ax in zip(lohi, excl, tps, pgs, failed, axs.flat[<NUM_LIT:4>:]):<EOL><INDENT>ax3.axvspan(t[lo], t[hi], color='<STR_LIT:r>', alpha=<NUM_LIT:0.1>, zorder=-<NUM_LIT:2>)<EOL>x = t[lo:hi]<EOL>y = g[lo:hi]<EOL>ys = sig[lo:hi]<EOL>ax.scatter(x, y, color='<STR_LIT:k>', marker='<STR_LIT:x>', zorder=-<NUM_LIT:1>, s=<NUM_LIT:10>)<EOL>ax.set_yticklabels([])<EOL>ax.set_ylim(rangecalc(y))<EOL>tax = ax.twinx()<EOL>tax.plot(x, ys, color='<STR_LIT:k>', alpha=<NUM_LIT>, zorder=-<NUM_LIT:5>)<EOL>tax.set_yticklabels([])<EOL>tax.set_ylim(rangecalc(ys))<EOL>xn = np.linspace(x.min(), x.max(), <NUM_LIT:100>)<EOL>ax.plot(xn, gauss(xn, *pg), color='<STR_LIT:r>', alpha=<NUM_LIT:0.5>)<EOL>ax.axvline(pg[<NUM_LIT:1>], color='<STR_LIT:b>', alpha=<NUM_LIT:0.5>)<EOL>ax.axvspan(*lim, color='<STR_LIT:b>', alpha=<NUM_LIT:0.1>, zorder=-<NUM_LIT:2>)<EOL>ax1.axvspan(*lim, color='<STR_LIT:b>', alpha=<NUM_LIT:0.1>, zorder=-<NUM_LIT:2>)<EOL>if tp:<EOL><INDENT>ax.text(<NUM_LIT>, <NUM_LIT>, '<STR_LIT>'.format(n), ha='<STR_LIT:left>',<EOL>va='<STR_LIT>', transform=ax.transAxes)<EOL><DEDENT>else:<EOL><INDENT>ax.text(<NUM_LIT>, <NUM_LIT>, '<STR_LIT>'.format(n), ha='<STR_LIT:right>',<EOL>va='<STR_LIT>', transform=ax.transAxes)<EOL><DEDENT>if ax.is_last_row():<EOL><INDENT>ax.set_xlabel('<STR_LIT>')<EOL><DEDENT>if ax.is_first_col():<EOL><INDENT>ax.set_ylabel('<STR_LIT>')<EOL><DEDENT>if ax.is_last_col():<EOL><INDENT>tax.set_ylabel('<STR_LIT>')<EOL><DEDENT>if fail:<EOL><INDENT>ax.axes.set_facecolor((<NUM_LIT:1>, <NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT>))<EOL>ax.text(<NUM_LIT>, <NUM_LIT>, '<STR_LIT>', ha='<STR_LIT>', va='<STR_LIT>',<EOL>fontsize=<NUM_LIT:16>, color=(<NUM_LIT:1>, <NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:0.5>), transform=ax.transAxes)<EOL><DEDENT>n += <NUM_LIT:1><EOL><DEDENT>if len(zeros) % <NUM_LIT:2> == <NUM_LIT:1>:<EOL><INDENT>axs.flat[-<NUM_LIT:1>].set_visible = False<EOL><DEDENT><DEDENT>return fig, axs<EOL>
Function for visualising the autorange mechanism.

Parameters
----------
t : array-like
    Independent variable (usually time).
sig : array-like
    Dependent signal, with distinctive 'on' and 'off' regions.
gwin : int
    The window used for calculating first derivative.
    Defaults to 7.
swin : int
    The window ised for signal smoothing. If None, gwin // 2.
win : int
    The width (c +/- win) of the transition data subsets.
    Defaults to 20.
on_mult and off_mult : tuple, len=2
    Control the width of the excluded transition regions, which is defined
    relative to the peak full-width-half-maximum (FWHM) of the transition
    gradient. The region n * FHWM below the transition, and m * FWHM above
    the tranision will be excluded, where (n, m) are specified in `on_mult`
    and `off_mult`.
    `on_mult` and `off_mult` apply to the off-on and on-off transitions,
    respectively.
    Defaults to (1.5, 1) and (1, 1.5).
nbin : ind
    Used to calculate the number of bins in the data histogram.
    bins = len(sig) // nbin

Returns
-------
fig, axes