예제 #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)
예제 #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)
예제 #3
0
def _explained_variance_plot(model, ax: mpl.axes.Axes, cutoff: float = 0.9):
    n = len(model.explained_variance_ratio_)
    _x = np.arange(1, n + 1)
    _ycum = np.cumsum(model.explained_variance_ratio_)
    best_index = np.where(_ycum > cutoff)[0]
    # modify in case we dont have one
    best_index = best_index[0] if best_index.shape[0] > 0 else n - 1
    # calculate AUC
    auc = np.trapz(_ycum, _x / n)
    # plot
    ax.plot(_x, _ycum, "x-")
    # plot best point
    ax.scatter(
        [_x[best_index]],
        [_ycum[best_index]],
        facecolors="None",
        edgecolors="red",
        s=100,
        label="n=%d, auc=%.3f" % (_x[best_index], auc),
    )
    # plot 0 to 1 line
    ax.plot([1, n], [0, 1], "k--")
    ax.set_xlabel("N\n(Best proportion: %.3f)" % (_x[best_index] / (n + 1)))
    ax.set_ylabel("Explained variance (ratio)\n(cutoff=%.2f)" % cutoff)
    ax.grid()
    ax.legend()
예제 #4
0
def _generate_commute_days_plot(commute_data: pd.DataFrame, ax: mpl.axes.Axes,
                                colours: dict):
    """
    Generate a line plot of commute days per year.

    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 day
    data = commute_data.groupby(commute_data.index.to_period('D')).agg(
        {'distance': 'mean'})

    # Generate and format the line plot
    sns.lineplot(x=data.index.year.value_counts().index,
                 y=data.index.year.value_counts(),
                 color=colours['commute_days'],
                 marker='o',
                 ax=ax)
    ax.set(ylabel='Commute days', xlabel='Year')
    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)
예제 #5
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)
예제 #6
0
def _plot_pca_scatter(model, ax: mpl.axes.Axes, dist_col: bool):
    # calculate the magnitude away from origin (0, 0)
    mag = np.linalg.norm(model.components_[:, :2], axis=1)
    _x, _y = model.components_[:, 0], model.components_[:, 1]
    _xl, _yl = [
        "PC%d (%.2f)" % (i + 1, model.explained_variance_ratio_[i] * 100)
        for i in range(2)
    ]
    # plot
    if dist_col:
        scatter(
            _x,
            _y,
            c=mag,
            cmap="RdBu_r",
            x_label=_xl,
            y_label=_yl,
            colorbar=False,
            ax=ax,
        )
    else:
        scatter(_x, _y, x_label=_xl, y_label=_yl, colorbar=False, ax=ax)
    # add pca lines
    ax.hlines(0, xmin=_x.min(), xmax=_x.max(), linestyle="--")
    ax.vlines(0, ymin=_y.min(), ymax=_y.max(), linestyle="--")
    ax.grid()
예제 #7
0
def plot_loss(ax: matplotlib.axes.Axes, training_losses: list, validation_losses: list):
    sz = len(training_losses)
    ax.plot(range(sz), training_losses, c='r',
            label='t--' + str(training_losses[-1]))
    ax.plot(range(sz), validation_losses, c='g',
            label='v--' + str(validation_losses[-1]))
    # ax.legend(fontsize=16)
    ax.grid(alpha=0.25)
예제 #8
0
def pretty_axes(ax: matplotlib.axes.Axes) -> matplotlib.axes.Axes:
    """Better looking matplotlib axes object."""
    ax.spines["top"].set_visible(False)
    ax.spines["right"].set_visible(False)
    ax.spines["left"].set_visible(False)
    ax.get_xaxis().tick_bottom()
    ax.get_yaxis().set_visible(False)
    ax.grid(axis="y", alpha=0.75)
    return ax
예제 #9
0
def _best_eigenvector_plot(
    x, y, labels: pd.Index, ax: mpl.axes.Axes, nk: Tuple[int, int] = (6, 5)
):
    n_samples, n_pcs = nk

    ax.scatter(x, y)
    ax.hlines(0, -0.5, n_pcs - 0.5, linestyle="--")
    annotate(x, y, labels, ax=ax, word_shorten=15)
    ax.set_ylabel("Eigenvector")
    ax.grid()
예제 #10
0
def set_axes_common_properties(axe: mp.axes.Axes,
                               no_grid: bool = False,
                               border: bool = False) -> bool:
    rv = False
    try:
        axe.spines['top'].set_visible(border)
        axe.spines['left'].set_visible(border)
        axe.spines['right'].set_visible(border)
        axe.spines['bottom'].set_visible(border)
        if no_grid == False:
            axe.grid(color='#636262', linestyle='-.', linewidth=0.2)
        rv = True
    except Exception as ex:
        print("Errore - {e}".format(e=str(ex)))

    return rv
예제 #11
0
파일: plot.py 프로젝트: andreasfelix/apace
def draw_sub_lattices(
    ax: mpl.axes.Axes,
    lattice: Lattice,
    *,
    labels: bool = True,
    location: str = "top",
):
    x_min, x_max = ax.get_xlim()
    length_gen = [0.0, *(obj.length for obj in lattice.children)]
    position_list = np.add.accumulate(length_gen)
    i_min = np.searchsorted(position_list, x_min)
    i_max = np.searchsorted(position_list, x_max, side="right")
    ticks = position_list[i_min:i_max]
    ax.set_xticks(ticks)
    ax.grid(color=Color.LIGHT_GRAY, linestyle="--", linewidth=1)

    if labels:
        y_min, y_max = ax.get_ylim()
        height = 0.08 * (y_max - y_min)
        if location == "top":
            y0 = y_max - height
        else:
            y0, y_min = y_min - height / 3, y_min - height

        ax.set_ylim(y_min, y_max)
        start = end = 0
        for obj in lattice.children:
            end += obj.length
            if not isinstance(obj, Lattice) or start >= x_max or end <= x_min:
                continue

            x0 = 0.5 * (max(start, x_min) + min(end, x_max))
            ax.annotate(
                obj.name,
                xy=(x0, y0),
                fontsize=FONT_SIZE + 2,
                fontstyle="oblique",
                va="center",
                ha="center",
                clip_on=True,
                zorder=102,
            )
            start = end
예제 #12
0
파일: __init__.py 프로젝트: f-koehler/mlxtk
    def apply(self, axes: matplotlib.axes.Axes,
              figure: matplotlib.figure.Figure):

        axes.grid(self.grid)
        if self.logx:
            axes.set_xscale("log")
        if self.logy:
            axes.set_yscale("log")

        xmin, xmax = axes.get_xlim()
        ymin, ymax = axes.get_ylim()
        xmin = xmin if self.xmin is None else self.xmin
        xmax = xmax if self.xmax is None else self.xmax
        ymin = ymin if self.ymin is None else self.ymin
        ymax = ymax if self.ymax is None else self.ymax
        axes.set_xlim(xmin=xmin, xmax=xmax)
        axes.set_ylim(ymin=ymin, ymax=ymax)

        if self.dpi and (figure is not None):
            figure.set_dpi(self.dpi)
예제 #13
0
def _generate_commute_distance_plot(commute_data: pd.DataFrame,
                                    ax: mpl.axes.Axes, colours: dict):
    """
    Generate a line plot of total and mean commute distance per year.

    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 year
    data = commute_data.resample('Y').agg({'distance': ['sum', 'mean']})

    # Generate and format the total distance line plot
    sns.lineplot(x=data.index.year,
                 y=data['distance', 'sum'],
                 color=colours['commute_distance_sum'],
                 marker='o',
                 ax=ax)
    ax.set_xlabel('Year')
    ax.set_ylabel('Total commute distance (km)',
                  color=colours['commute_distance_sum'])
    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)

    # Generate and format the mean distance line plot
    ax_mean = ax.twinx()
    sns.lineplot(x=data.index.year,
                 y=data['distance', 'mean'],
                 color=colours['commute_distance_mean'],
                 marker='o',
                 ax=ax_mean)
    ax_mean.set_ylabel('Average commute distance (km)',
                       color=colours['commute_distance_mean'])