Exemplo n.º 1
0
    def _violinplot(
            self,
            data: dataType,
            names: namesType,
            title: titleType = None,
            ax: matplotlib.axes.SubplotBase = None
    ) -> matplotlib.figure.Figure:
        """For making violinplots."""

        if ax is None:
            _, ax = plt.subplots()
        else:
            ax = ax

        figure = ax.get_figure()
        width = max(self.nplayers / 3, 12)
        height = width / 2
        spacing = 4
        positions = spacing * arange(1, self.nplayers + 1, 1)
        figure.set_size_inches(width, height)
        ax.violinplot(data,
                      positions=positions,
                      widths=spacing / 2,
                      showmedians=True,
                      showextrema=False)
        ax.set_xticks(positions)
        ax.set_xticklabels(names, rotation=90)
        ax.set_xlim([0, spacing * (self.nplayers + 1)])
        ax.tick_params(axis='both', which='both', labelsize=8)
        if title:
            ax.set_title(title)
        plt.tight_layout()
        return figure
Exemplo n.º 2
0
    def _payoff_heatmap(
            self,
            data: dataType,
            names: namesType,
            title: titleType = None,
            ax: matplotlib.axes.SubplotBase = None
    ) -> matplotlib.figure.Figure:
        """Generic heatmap plot"""

        if ax is None:
            _, ax = plt.subplots()
        else:
            ax = ax

        figure = ax.get_figure()
        width = max(self.nplayers / 4, 12)
        height = width
        figure.set_size_inches(width, height)
        matplotlib_version = matplotlib.__version__
        cmap = default_cmap(matplotlib_version)
        mat = ax.matshow(data, cmap=cmap)
        ax.set_xticks(range(self.result_set.nplayers))
        ax.set_yticks(range(self.result_set.nplayers))
        ax.set_xticklabels(names, rotation=90)
        ax.set_yticklabels(names)
        ax.tick_params(axis='both', which='both', labelsize=16)
        if title:
            ax.set_xlabel(title)
        figure.colorbar(mat, ax=ax)
        plt.tight_layout()
        return figure
Exemplo n.º 3
0
    def _payoff_heatmap(
            self,
            data: dataType,
            names: namesType,
            title: titleType = None,
            ax: matplotlib.axes.SubplotBase = None
    ) -> matplotlib.figure.Figure:
        """Generic heatmap plot"""
        if not self.matplotlib_installed:
            return None

        if ax is None:
            _, ax = plt.subplots()
        else:
            ax = ax

        figure = ax.get_figure()
        width = max(self.nplayers / 4, 12)
        height = width
        figure.set_size_inches(width, height)
        cmap = default_cmap()
        mat = ax.matshow(data, cmap=cmap)
        plt.xticks(range(self.result_set.nplayers))
        plt.yticks(range(self.result_set.nplayers))
        ax.set_xticklabels(names, rotation=90)
        ax.set_yticklabels(names)
        plt.tick_params(axis='both', which='both', labelsize=16)
        if title:
            plt.xlabel(title)
        # Make the colorbar match up with the plot
        divider = make_axes_locatable(plt.gca())
        cax = divider.append_axes("right", "5%", pad="3%")
        plt.colorbar(mat, cax=cax)
        return figure
Exemplo n.º 4
0
    def _violinplot(
        self,
        data: dataType,
        names: namesType,
        title: titleType = None,
        ax: matplotlib.axes.SubplotBase = None,
    ) -> matplotlib.figure.Figure:
        """For making violinplots."""

        if ax is None:
            _, ax = plt.subplots()
        else:
            ax = ax

        figure = ax.get_figure()
        width = max(self.num_players / 3, 12)
        height = width / 2
        spacing = 4
        positions = spacing * arange(1, self.num_players + 1, 1)
        figure.set_size_inches(width, height)
        ax.violinplot(
            data,
            positions=positions,
            widths=spacing / 2,
            showmedians=True,
            showextrema=False,
        )
        ax.set_xticks(positions)
        ax.set_xticklabels(names, rotation=90)
        ax.set_xlim([0, spacing * (self.num_players + 1)])
        ax.tick_params(axis="both", which="both", labelsize=8)
        if title:
            ax.set_title(title)
        plt.tight_layout()
        return figure
Exemplo n.º 5
0
    def _payoff_heatmap(
        self,
        data: dataType,
        names: namesType,
        title: titleType = None,
        ax: matplotlib.axes.SubplotBase = None,
    ) -> matplotlib.figure.Figure:
        """Generic heatmap plot"""

        if ax is None:
            _, ax = plt.subplots()
        else:
            ax = ax

        figure = ax.get_figure()
        width = max(self.num_players / 4, 12)
        height = width
        figure.set_size_inches(width, height)
        matplotlib_version = matplotlib.__version__
        cmap = default_cmap(matplotlib_version)
        mat = ax.matshow(data, cmap=cmap)
        ax.set_xticks(range(self.result_set.num_players))
        ax.set_yticks(range(self.result_set.num_players))
        ax.set_xticklabels(names, rotation=90)
        ax.set_yticklabels(names)
        ax.tick_params(axis="both", which="both", labelsize=16)
        if title:
            ax.set_xlabel(title)
        figure.colorbar(mat, ax=ax)
        plt.tight_layout()
        return figure
Exemplo n.º 6
0
def plt_settings_axes(g: matplotlib.axes.SubplotBase,
                      count_df: dask.dataframe.core.DataFrame,
                      grouping_col: list, facet: str, hide_xtitle: bool,
                      log_y: bool) -> None:
    """
    Helper function for plot settings, used in function plt_generic_1d.
    Modifies parameter g for setting titles, axis, formats, etc.
    :param g: matplotlib Axes which will be modified directly in the function.
    :param count_df: pandas dataframe which is plotted.
    :param grouping_col: column for x axis.
    :param facet: parameter passed by function plt_generic_1d, giving information
    on whether we are plotting and average or a count value (on y axis).
    :param hide_xtitle: if set to True, doesn't display title for x axis 
    :param log_y: if set to True, plot in logarithmic scale (for y axis)
    :return: nothing. changes are done directly by modifying parameter g.
    """

    if facet not in ['freq', 'avg']:
        raise ValueError(
            'Parameter facet should be a string of value either "freq" or "avg"'
        )

    # SET X AXIS
    # Labels
    # no particular setup if number of labels is less than the first threshold
    num_xlabels = len(count_df[grouping_col])

    if num_xlabels < LABEL_THRESHOLD_ROTATION:
        g.set_xticklabels(count_df[grouping_col])

    # rotate by 90 degrees if number of labels is between first and second threshold
    elif num_xlabels < LABEL_THRESHOLD_SELECT:
        g.set_xticklabels(count_df[grouping_col], rotation=90)

    # display only certain labels (and rotate by 45 degrees) if number of labels is higher
    else:
        number_of_steps = num_xlabels / 50

        l = np.arange(0, num_xlabels, number_of_steps)

        pos = (l / num_xlabels) * (max(g.get_xticks()) - min(g.get_xticks()))
        g.set_xticks(pos)
        g.set_xticklabels(count_df[grouping_col].iloc[l], rotation=45)

    # Title
    # option to remove the x axis title (when its obvious, e.g. for the years)
    if hide_xtitle:
        g.set_xlabel('')
    else:
        g.set_xlabel(grouping_col)

    # SET Y AXIS
    # log scale option
    if log_y:
        g.set_yscale("log")
        if facet == 'freq':
            g.set_ylabel('# content items (log scale)')
        elif facet == 'avg':
            g.set_ylabel('title length (log scale)')

    else:
        if facet == 'freq':
            g.set_ylabel('# content items')
        elif facet == 'avg':
            g.set_ylabel('title length')

    # Labels
    ylabels = ['{:,.0f}'.format(y) for y in g.get_yticks()]
    g.set_yticklabels(ylabels)

    # Plot Title
    if facet == 'freq':
        g.set_title('Number of content items by %s' % grouping_col)
    elif facet == 'avg':
        g.set_title('Average title length of content items by %s' %
                    grouping_col)