Ejemplo n.º 1
0
def _generate_activity_count_plot(activity_data: pd.DataFrame,
                                  ax: mpl.axes.Axes, colours: str):
    """
    Generate a bar plot of activity counts over time (by type).

    Arguments:
    activity data - A pandas DataFrame containing the activity data.
    ax - A set of matplotlib axes to generate the plot on.
    colours - A name of the colour palette to generate the plot with.
    """

    # Group the activity data by month and calculate the count of each activity type
    data = (activity_data.groupby([activity_data.index.to_period('M'), 'type'
                                   ]).size().to_frame('count').reset_index())

    # Generate and format the bar plot
    sns.barplot(x='start_date_local',
                y='count',
                hue='type',
                data=data,
                palette=colours,
                ax=ax)
    ax.set(title='Activities over time',
           ylabel='Number of activities',
           xlabel='Month')
    ax.set_xticklabels(ax.get_xticklabels(),
                       rotation=45,
                       horizontalalignment='right')
    ax.get_xaxis().set_minor_locator(mpl.ticker.AutoMinorLocator())
    ax.get_yaxis().set_minor_locator(mpl.ticker.AutoMinorLocator())
    ax.grid(b=True, which='major', linewidth=1.0)
    ax.yaxis.grid(b=True, which='minor', linewidth=0.5)
    ax.set_axisbelow(True)
Ejemplo n.º 2
0
def _generate_commute_count_plot(commute_data: pd.DataFrame, ax: mpl.axes.Axes,
                                 colours: dict):
    """
    Generate a bar plot of number of commutes per month.

    Arguments:
    commute_data - A pandas DataFrame containing the commute activity data.
    ax - A set of matplotlib axes to generate the plot on.
    colours - A dictionary of colours to generate the plot with.
    """

    # Group the commute data by month
    data = commute_data.resample('M').count()

    # Generate and format the bar plot
    sns.barplot(x=data.index.to_period('M'),
                y=data['distance'],
                color=colours['commute_count'],
                ax=ax)
    ax.set(ylabel='Number of commutes', xlabel='Month')
    ax.set_xticklabels(ax.get_xticklabels(),
                       rotation=45,
                       horizontalalignment='right')
    ax.get_xaxis().set_minor_locator(mpl.ticker.AutoMinorLocator())
    ax.get_yaxis().set_minor_locator(mpl.ticker.AutoMinorLocator())
    ax.grid(b=True, which='major', linewidth=1.0)
    ax.yaxis.grid(b=True, which='minor', linewidth=0.5)
    ax.set_axisbelow(True)
Ejemplo n.º 3
0
def _generate_mean_distance_plot(activity_data: pd.DataFrame,
                                 ax: mpl.axes.Axes, colour_palette: list):
    """
    Generate a bar plot of mean activity distance over time (by type).

    Arguments:
    activity data - A pandas DataFrame containing the activity data.
    ax - A set of matplotlib axes to generate the plot on.
    colour_palette - The colour palette to generate the plot with.
    """

    # Group the activity data by month and calculate the mean distance of each activity type
    data = activity_data.groupby([activity_data.index.to_period('Y'),
                                  'type']).mean().reset_index()

    # Generate and format the bar plot
    sns.barplot(x='start_date_local',
                y='distance',
                hue='type',
                data=data,
                palette=colour_palette,
                ax=ax)
    ax.set(title='Mean activity distance over time',
           ylabel='Mean distance (km)',
           xlabel='Year')
    ax.set_xticklabels(ax.get_xticklabels(),
                       rotation=45,
                       horizontalalignment='right')
    ax.get_xaxis().set_minor_locator(mpl.ticker.AutoMinorLocator())
    ax.get_yaxis().set_minor_locator(mpl.ticker.AutoMinorLocator())
    ax.grid(b=True, which='major', linewidth=1.0)
    ax.yaxis.grid(b=True, which='minor', linewidth=0.5)
    ax.set_axisbelow(True)