Esempio n. 1
0
def plot_period(trj: TrajaDataFrame, col="x", dark=(7, 19), **kwargs):
    time_col = traja._get_time_col(trj)
    _trj = trj.set_index(time_col)
    if not col in _trj:
        raise ValueError(f"{col} not a column in dataframe")
    series = _trj[col]
    fig, ax = plt.subplots()
    series.plot(ax=ax)

    dates = np.unique(series.index.date)

    nights = []
    nights.append([(date, date + timedelta(hours=dark[0])) for date in dates])
    nights.append([(date + timedelta(hours=dark[1]), date + timedelta(days=1))
                   for date in dates])
    for interval in nights:
        t0, t1 = interval
        ax.axvspan(t0, t1, color="gray", alpha=0.2)

    # Format date displayed on the x axis
    xfmt = md.DateFormatter("%H:%M\n%m-%d-%y")
    ax.xaxis.set_major_formatter(xfmt)

    if kwargs.get("interactive"):
        plt.show()
Esempio n. 2
0
def polar_bar(
    trj: TrajaDataFrame,
    feature: str = "turn_angle",
    bin_size: int = 2,
    threshold: float = 0.001,
    overlap: bool = True,
    ax: Optional[matplotlib.axes.Axes] = None,
    **plot_kws: str,
) -> Axes:
    """Plot polar bar chart.

    Args:
        trj (:class:`traja.TrajaDataFrame`): trajectory
        feature (str): Options: 'turn_angle', 'heading'
        bin_size (int): width of bins
        threshold (float): filter for step distance
        overlap (bool): Overlapping shows all values, if set to false is a histogram

    Returns:
        ax (:class:`~matplotlib.collections.PathCollection`): Axes of plot

    """
    # Get displacement
    displacement = traja.trajectory.calc_displacement(trj)
    trj["displacement"] = displacement
    trj = trj.loc[trj.displacement > threshold]
    if feature == "turn_angle":
        feature_series = traja.trajectory.calc_turn_angle(trj)
        trj["turn_angle"] = feature_series
        trj.turn_angle = trj.turn_angle.shift(-1)
    elif feature == "heading":
        feature_series = traja.trajectory.calc_heading(trj)
        trj[feature] = feature_series

    trj = trj[pd.notnull(trj[feature])]
    trj = trj[pd.notnull(trj.displacement)]

    assert (
        len(trj) > 0
    ), f"Dataframe is empty after filtering for step distance threshold {threshold}"

    ax = _polar_bar(
        trj.displacement,
        trj[feature],
        bin_size=bin_size,
        overlap=overlap,
        ax=ax,
        **plot_kws,
    )
    return ax