Example #1
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")