コード例 #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 _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)
コード例 #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
ファイル: train_gcn.py プロジェクト: JohnFinn/gas
    def draw_tables(ax: matplotlib.axes.Axes, net: torch.nn.Module,
                    data: tg.data.DataLoader):
        table = np.full((13, 12), np.nan)
        for batch in data:
            predicted = net(batch)
            Y = batch.y[:, 0] - 2008
            M = batch.y[:, 1]
            table[Y, M] = cycle_dst2(M.float(),
                                     predicted.flatten().detach().numpy())**.5

        mshow = ax.matshow(table, vmin=0, vmax=6)
        ax.set(yticks=range(13), yticklabels=range(2008, 2021))
        return mshow
コード例 #6
0
ファイル: plot_fig.py プロジェクト: BvB93/SBU-Reporter
def post_process_plt(df: pd.DataFrame,
                     ax: plt.axes.Axes,
                     percent: bool = False) -> plt.figure.Figure:
    """Post-process the Matplotlib Axes instance produced by :func:`pre_process_plt`.

    The post-processing invovles further formatting of the legend, the x-axis and the y-axis.

    Parameters
    ----------
    df : :class:`pandas.DataFrame`
        A DataFrame holding the accumulated SBU usage.
        See :func:`pre_process_df` and :func:`.get_agregated_sbu`.

    ax : :class:`matplotlib.Axes<matplotlib.axes.Axes>`
        An Axes instance produced by :func:`pre_process_plt`.

    percent : class`bool`
        If ``True``, apply additional formatting for handling percentages.

    Returns
    -------
    :class:`matplotlib.figure.Figure`:
        A Matplotlib Figure constructed from **ax**.

    """
    df_max = df.max().max()
    decimals = 1 - len(str(int(df.values.max())))
    y_max = round(df_max, decimals) + 10**-decimals

    # Format the y-axis
    ax.yaxis.set_major_formatter(plt.ticker.StrMethodFormatter('{x:,.0f}'))
    ax.set(ylim=(0, y_max))

    # Format the x-axis
    i = len(df.index) // 6 or 1
    ax.set(xticks=df.index[0::i])

    today = date.today().strftime('%d %b %Y')
    if percent:
        ax.set_ylabel('SBUs (System Billing Units)  /  %')
        ax.set_title('Accumulated % SBU usage: {}'.format(today),
                     fontdict={'fontsize': 18})
        ax.legend_.set_title('Project (PI): % SBU')
    else:
        ax.set_ylabel('SBUs (System Billing Units)  /  hours')
        ax.set_title('Accumulated SBU usage: {}'.format(today),
                     fontdict={'fontsize': 18})
        ax.legend_.set_title('Project (PI): SBU')
    return ax.get_figure()
コード例 #7
0
ファイル: mountain.py プロジェクト: wptmdoorn/methcomp
    def plot(
        self,
        xlabel: str = "Method difference",
        ylabel: str = "Folded CDF (%)",
        label: str = "$M_1$ - $M_2$",
        unit: str = None,
        title: str = None,
        color: str = "blue",
        legend: bool = True,
        square: bool = False,
        show_hline: bool = True,
        show_vlines: bool = True,
        show_markers: bool = True,
        ax: matplotlib.axes.Axes = None,
    ) -> matplotlib.axes.Axes:
        """Plot mountain plot

        Parameters
        ----------
        xlabel : str, optional
            The label which is added to the X-axis. (default: "Method difference")
        ylabel : str, optional
            The label which is added to the Y-axis. (default: "Folded CDF (%)")
        label : str, optional
            mountaint line legend label (default:"Method 1 - Method 2" )
        unit : str, optional
            unit to disply for x-axis
        title : str, optional
            figure title, if none there will be no title
            (default: None)
        legend : bool, optional
            If True, will provide a legend containing the computed regression equation.
            (default: True)
        square : bool, optional
            If True, set the Axes aspect to "equal" so each cell will be
            square-shaped. (default: True)
        color : str, optional
            Color for mountain plot elements
        show_hline: bool, optional
            If set show horizontal lines for iqr
        show_vlines: bool, optional
            If set show vertical lines at iqr and median
        show_markers: bool, optional
            If set show markers at iqr and median
        ax : matplotlib.axes.Axes, optional
            matplotlib axis object, if not passed, uses gca()

        Returns
        -------
        matplotlib.axes.Axes
            axes object with the plot
        """
        ax = ax or plt.gca()
        ax.step(
            y=self.result["mountain"],
            x=self.result["quantile"],
            where="mid",
            label=f"{label} AUC={self.result['auc']:.2f}",
            color=color,
        )
        if show_hline:
            ax.hlines(
                self.result["mountain_iqr"],
                xmin=self.result["iqr"][0],
                xmax=self.result["iqr"][1],
                color=color,
            )
        if show_vlines:
            ax.vlines(
                self.result["median"],
                ymin=0,
                ymax=50,
                label=f"median={self.result['median']:.2f} {unit or ''}",
                linestyle="--",
                color=color,
            )
            if self.iqr > 0:
                ax.vlines(
                    self.result["iqr"],
                    ymin=0,
                    ymax=50 - self.iqr / 2,
                    label=(
                        f"{self.iqr:.2f}% IQR ="
                        f"{self.result['iqr'][1] - self.result['iqr'][0]:.2f}"
                        "{unit or ''}"),
                    linestyle=":",
                    color=color,
                )
        if show_markers:
            ax.plot(
                self.result["median"],
                self.result["mountain_median"],
                "o",
                color=color,
            )
            if self.iqr > 0:
                ax.plot(
                    self.result["iqr"],
                    self.result["mountain_iqr"],
                    "o",
                    color=color,
                )
        u = f"({unit})" if unit else ""
        ax.set(xlabel=f"{xlabel} {u}",
               ylabel=ylabel or None,
               title=title or None)
        ax.legend(loc="upper left", fontsize="medium")

        if legend:
            ax.legend(loc="upper left", frameon=False)

        if square:
            ax.set_aspect("equal")

        return ax
コード例 #8
0
ファイル: regressor.py プロジェクト: wptmdoorn/methcomp
    def plot(
        self,
        x_label: str = "Method 1",
        y_label: str = "Method 2",
        title: str = None,
        line_reference: bool = True,
        line_CI: bool = True,
        legend: bool = True,
        square: bool = False,
        ax: matplotlib.axes.Axes = None,
        point_kws: Optional[Dict] = None,
        color_regr: Optional[str] = None,
        alpha_regr: Optional[float] = None,
    ) -> matplotlib.axes.Axes:
        """Plot regression result

        Parameters
        ----------
        x_label : str, optional
            The label which is added to the X-axis. (default: "Method 1")
        y_label : str, optional
            The label which is added to the Y-axis. (default: "Method 2")
        title : str, optional
            Title of the regression plot. If None is provided, no title will be plotted.
        line_reference : bool, optional
            If True, a grey reference line at y=x will be plotted in the plot
            (default: True)
        line_CI : bool, optional
            If True, dashed lines will be plotted at the boundaries of the confidence
            intervals.
            (default: False)
        legend : bool, optional
            If True, will provide a legend containing the computed regression equation.
            (default: True)
        square : bool, optional
            If True, set the Axes aspect to "equal" so each cell will be
            square-shaped. (default: True)
        ax : matplotlib.axes.Axes, optional
            matplotlib axis object, if not passed, uses gca()
        point_kws : Optional[Dict], optional
            Additional keywords to plt
        color_regr : Optional[str], optional
            color for regression line and CI area
        alpha_regr : Optional[float], optional
            alpha for regression CI area

        Returns
        ------------------
        matplotlib.axes.Axes
            axes object with the plot
        """
        ax = ax or plt.gca()

        # Set scatter plot keywords to defaults and apply override
        pkws = self.DEFAULT_POINT_KWS.copy()
        pkws.update(point_kws or {})

        # Get regression parameters
        slope = self.result["slope"]
        intercept = self.result["intercept"]

        # plot individual points
        ax.scatter(self.method1, self.method2, **pkws)

        # plot reference line
        if line_reference:
            ax.plot(
                [0, 1],
                [0, 1],
                label="Reference",
                color="grey",
                linestyle="--",
                transform=ax.transAxes,
            )

        # Compute x and y values
        xvals = np.array(ax.get_xlim())
        yvals = xvals[:, None] * slope + intercept

        # Plot regression line 0
        ax.plot(
            xvals,
            yvals[:, 0],
            label=
            f"{y_label} = {intercept[0]:.2f} + {slope[0]:.2f} * {x_label}",
            color=color_regr,
            linestyle="-",
        )

        # Plot confidence region
        if yvals.shape[1] > 2:
            ax.fill_between(
                xvals,
                yvals[:, 1],
                yvals[:, 2],
                color=color_regr or self.DEFAULT_REGRESSION_KWS["color"],
                alpha=alpha_regr or self.DEFAULT_REGRESSION_KWS["alpha"],
            )
            if line_CI:
                ax.plot(xvals, yvals[:, 1], linestyle="--")
                ax.plot(xvals, yvals[:, 2], linestyle="--")

        # Set axes labels
        ax.set(
            xlabel=x_label or "",
            ylabel=y_label or "",
            title=title or "",
        )

        if legend:
            ax.legend(loc="upper left", frameon=False)

        if square:
            ax.set_aspect("equal")
        return ax
コード例 #9
0
ファイル: blandaltman.py プロジェクト: wptmdoorn/methcomp
    def plot(
        self,
        x_label: str = "Mean of methods",
        y_label: str = "Difference between methods",
        graph_title: str = None,
        reference: bool = False,
        xlim: Tuple = None,
        ylim: Tuple = None,
        color_mean: str = "#008bff",
        color_loa: str = "#FF7000",
        color_points: str = "#000000",
        point_kws: Dict = None,
        ci_alpha: float = 0.2,
        loa_linestyle: str = "--",
        ax: matplotlib.axes.Axes = None,
    ):
        """Provide a method comparison using Bland-Altman plotting.
        This is an Axis-level function which will draw the Bland-Altman plot
        onto the current active Axis object unless ``ax`` is provided.
        Parameters
        ----------
        x_label : str, optional
            The label which is added to the X-axis. If None is provided, a standard
            label will be added.
        y_label : str, optional
            The label which is added to the Y-axis. If None is provided, a standard
            label will be added.
        graph_title : str, optional
            Title of the Bland-Altman plot.
            If None is provided, no title will be plotted.
        reference : bool, optional
            If True, a grey reference line at y=0 will be plotted in the Bland-Altman.
        xlim : list, optional
            Minimum and maximum limits for X-axis. Should be provided as list or tuple.
            If not set, matplotlib will decide its own bounds.
        ylim : list, optional
            Minimum and maximum limits for Y-axis. Should be provided as list or tuple.
            If not set, matplotlib will decide its own bounds.
        color_mean : str, optional
            Color of the mean difference line that will be plotted.
        color_loa : str, optional
            Color of the limit of agreement lines that will be plotted.
        color_points : str, optional
            Color of the individual differences that will be plotted.
        point_kws : dict of key, value mappings, optional
            Additional keyword arguments for `plt.scatter`.
        ci_alpha: float, optional
            Alpha value of the confidence interval.
        loa_linestyle: str, optional
            Linestyle of the limit of agreement lines.
        ax : matplotlib Axes, optional
            Axes in which to draw the plot, otherwise use the currently-active
            Axes.

        Returns
        -------
        ax : matplotlib Axes
            Axes object with the Bland-Altman plot.
        """

        ax = ax or plt.gca()

        pkws = self.DEFAULT_POINTS_KWS.copy()
        pkws.update(point_kws or {})

        # Get parameters
        mean, mean_CI = self.result["mean"], self.result["mean_CI"]
        loa_upper, loa_upper_CI = self.result["loa_upper"], self.result[
            "loa_upper_CI"]
        loa_lower, loa_lower_CI = self.result["loa_lower"], self.result[
            "loa_lower_CI"]
        sd_diff = self.result["sd_diff"]

        # individual points
        ax.scatter(self.mean, self.diff, **pkws)

        # mean difference and SD lines
        ax.axhline(mean, color=color_mean, linestyle=loa_linestyle)
        ax.axhline(loa_upper, color=color_loa, linestyle=loa_linestyle)
        ax.axhline(loa_lower, color=color_loa, linestyle=loa_linestyle)

        if reference:
            ax.axhline(0, color="grey", linestyle="-", alpha=0.4)

        # confidence intervals (if requested)
        if self.CI is not None:
            ax.axhspan(*mean_CI, color=color_mean, alpha=ci_alpha)
            ax.axhspan(*loa_upper_CI, color=color_loa, alpha=ci_alpha)
            ax.axhspan(*loa_lower_CI, color=color_loa, alpha=ci_alpha)

        # text in graph
        trans: matplotlib.transform = transforms.blended_transform_factory(
            ax.transAxes, ax.transData)
        offset: float = (((self.loa * sd_diff) * 2) / 100) * 1.2
        ax.text(
            0.98,
            mean + offset,
            "Mean",
            ha="right",
            va="bottom",
            transform=trans,
        )
        ax.text(
            0.98,
            mean - offset,
            f"{mean:.2f}",
            ha="right",
            va="top",
            transform=trans,
        )
        ax.text(
            0.98,
            loa_upper + offset,
            f"+{self.loa:.2f} SD",
            ha="right",
            va="bottom",
            transform=trans,
        )
        ax.text(
            0.98,
            loa_upper - offset,
            f"{loa_upper:.2f}",
            ha="right",
            va="top",
            transform=trans,
        )
        ax.text(
            0.98,
            loa_lower - offset,
            f"-{self.loa:.2f} SD",
            ha="right",
            va="top",
            transform=trans,
        )
        ax.text(
            0.98,
            loa_lower + offset,
            f"{loa_lower:.2f}",
            ha="right",
            va="bottom",
            transform=trans,
        )

        # transform graphs
        ax.spines["right"].set_visible(False)
        ax.spines["top"].set_visible(False)

        # set X and Y limits
        if xlim is not None:
            ax.set_xlim(xlim[0], xlim[1])
        if ylim is not None:
            ax.set_ylim(ylim[0], ylim[1])

        # graph labels
        ax.set(xlabel=x_label, ylabel=y_label, title=graph_title)

        return ax