Example #1
0
def plot_aligned(
    x: np.ndarray,
    indices: Union[np.ndarray, list],
    ax: plt.axis,
    mode: str,
    window: int = 120,
    mean_kwargs: dict = None,
    **kwargs,
):
    """
        Given a 1d array and a series of indices it plots the values of 
        the array aligned to the timestamps.
    """
    pre, aft = int(window / 2), int(window / 2)
    mean_kwargs = mean_kwargs or dict(lw=4, zorder=100, color=pink_dark)

    # get plotting params
    if mode == "pre":
        pre_c, pre_lw = kwargs.pop("color", "salmon"), kwargs.pop("lw", 2)
        aft_c, aft_lw = blue_grey, 1
        ax.axvspan(0, pre, fc=blue, alpha=0.25, zorder=-20)
    else:
        aft_c, aft_lw = kwargs.pop("color", "salmon"), kwargs.pop("lw", 2)
        pre_c, pre_lw = blue_grey, 1
        ax.axvspan(aft, window, fc=blue, alpha=0.25, zorder=-20)

    # plot each trace
    X = []  # collect data to plot mean
    for idx in indices:
        x_pre = x[idx - pre:idx]
        x_aft = x[idx - 1:idx + aft]

        if len(x_pre) != pre or len(x_aft) != aft + 1:
            logger.warning(f"Could not plot data aligned to index: {idx}")
            continue
        X.append(x[idx - pre:idx + aft])

        ax.plot(x_pre, color=pre_c, lw=pre_lw, **kwargs)
        ax.plot(np.arange(aft + 1) + aft,
                x_aft,
                color=aft_c,
                lw=aft_lw,
                **kwargs)

    # plot mean and line
    X = np.vstack(X)
    plot_mean_and_error(np.mean(X, axis=0), np.std(X, axis=0), ax,
                        **mean_kwargs)
    ax.axvline(pre, lw=2, color=blue_grey_dark, zorder=-1)

    ax.set(**get_window_ticks(window, shifted=False))
Example #2
0
def add_cut_to_axis(ax: plt.axis,
                    cut_left: Optional[float] = None,
                    cut_right: Optional[float] = None,
                    cut_window: Optional[Tuple[float, float]] = None,
                    keep_window: Optional[Tuple[float, float]] = None,
                    color: str = 'white'):
    """
    Adds a "cut" to a given axis. The cut is shown as shaded area with the color
    given in the parameter color.

    :param ax: Axis to plot on.
    :param cut_left: Upper x value of the cut. If set, the area with
    x < cut_left is indicated to be cut away. Default is None
    :param cut_right: Lower x value of the cut. If set, the area with
    x > cut_right is indicated to be cut away. Default is None
    :param cut_window:
    :param keep_window:
    :param color: Color of the overlay of the area which is indicated to be
    cut away. Default is 'white'.
    """
    x_lim_low, x_lim_high = ax.get_xlim()

    if cut_left is not None:
        ax.axvspan(x_lim_low, cut_left, facecolor=color, alpha=0.7)
        ax.axvline(cut_left, color='black', linestyle='dashed', lw=1.5, label='Cut')
    elif cut_right is not None:
        ax.axvspan(cut_right, x_lim_high, facecolor=color, alpha=0.7)
        ax.axvline(cut_right, color='black', linestyle='dashed', lw=1.5, label='Cut')
    elif cut_window is not None:
        ax.axvspan(cut_window[0], cut_window[1], facecolor=color, alpha=0.7)
        ax.axvline(cut_window[0], color='black', linestyle='dashed', lw=1.5, label='Cut')
        ax.axvline(cut_window[1], color='black', linestyle='dashed', lw=1.5)
    elif keep_window is not None:
        ax.axvspan(x_lim_low, keep_window[0], facecolor=color, alpha=0.7)
        ax.axvline(keep_window[0], color='black', linestyle='dashed', lw=1.5, label='Cut')
        ax.axvspan(keep_window[1], x_lim_high, facecolor=color, alpha=0.7)
        ax.axvline(keep_window[1], color='black', linestyle='dashed', lw=1.5, label='Cut')

    ax.legend(frameon=False, bbox_to_anchor=(1, 1))