Ejemplo n.º 1
0
    def plot(self, filename, cv_errors=None):
        cols, rows = (2, 3)
        plot_width, plot_height = (7, 4)

        fig = plt.figure(figsize=(plot_width * cols, plot_height * rows))
        ax_ids = list(np.arange(cols * rows) + 1)
        ax_ids.reverse()

        if cv_errors is None:
            cv_errors = self.read_cv_errors()

        ymin, ymax = cv_errors.describe().loc[["min", "max"], "CV_error"]

        dataset_labels = cv_errors.index.get_level_values("dataset").unique()
        for dataset_label in dataset_labels:
            dataset = Dataset(dataset_label)

            ax = fig.add_subplot(rows, cols, ax_ids.pop())
            lines = []

            panel_labels = cv_errors.loc[dataset.label].index\
                .get_level_values("panel").unique()

            palette = sns.color_palette("Dark2", len(panel_labels))
            for panel in panel_labels:
                data = cv_errors.loc[(dataset.label, panel)]
                data.plot(ax=ax, marker="", color=palette.pop(0), zorder=1,
                          linestyle="solid", lw=2)

                # Minimum error mark
                x_min = data["CV_error"].idxmin()
                y_min = data["CV_error"].min()
                min_marker = ax.scatter(x_min, y_min, marker="v", zorder=2,
                                        edgecolor="black", color="yellow",
                                        lw=1.5, s=85)

            sns.despine(ax=ax, top=True, right=True, left=True, offset=1)
            lines, labels = ax.get_legend_handles_labels()
            names = [Panel(label).name for label in panel_labels]
            title = "{}".format(dataset.name)
            ax.set_title(title, fontsize=16, y=1.1, family="serif")
            ax.set_ylabel("CV Error", fontsize=16)
            ax.legend_.remove()
            ax.xaxis.grid(linestyle="dotted", color="grey")
            ax.set_ylim([ymin * 0.99, ymax * 1.01])  # Keep the same limits across axes

        # Ugly hack to get the legend in a separate subplot slot
        ax = plt.subplot(rows, cols, ax_ids.pop())

        legend_lines = lines + [min_marker]
        legend_labels = names + ["Valor mínimo de error"]
        legend_subplot(ax, legend_lines, legend_labels)

        plt.tight_layout()
        plt.savefig(join(PLOT_DIR, filename), bbox_inches="tight")

        return ax
Ejemplo n.º 2
0
    def plot_(self, components_df, explained_variance, title, filename,
              component_pairs=[("PC1", "PC2")], plot_size=None, legend_on=True):

        # + 1 axes for the one with the legend, +1 because index starts at 1
        ax_ids = list(np.arange(1, len(component_pairs) + 2))
        nrows, ncols, figsize = self._fig_dimensions(len(ax_ids), plot_size)
        fig = plt.figure(figsize=figsize)

        for components_to_compare in component_pairs:
            ax_id = ax_ids.pop(0)
            ax = fig.add_subplot(nrows, ncols, ax_id)
            ax = self.draw_ax(ax, components_to_compare, components_df,
                              explained_variance, "PEL", title)

        if legend_on:
            # Legend subplot. It will use the handles and labels of the last ax
            handles, labels = ax.get_legend_handles_labels()
            populations_df = ThousandGenomes.population_names()
            descriptions = populations_df.ix[labels, "description"]
            legend_labels = [" - ".join([code, desc])
                            for code, desc in descriptions.iteritems()]

            ax = fig.add_subplot(nrows, ncols, ax_ids.pop(0))
            ax = legend_subplot(ax, handles, legend_labels)

        #  plt.tight_layout()
        fig.suptitle(title, fontsize=18, position=(0.12, 1.1), ha="left",
                     family="serif")
        plt.subplots_adjust(wspace=0.05)

        if filename is not None:
            makedirs(self.FIGS_DIR, exist_ok=True)
            plt.savefig(join(self.FIGS_DIR, filename), facecolor="w",
                        bbox_inches="tight")