예제 #1
0
    def plot(
        self,
        cmap: str = "viridis",
        interpolation: str = "none",
        title: str = None,
        colorbar: bool = True,
        labels: bool = True,
        display_names: bool = False,
        ax: plt.Figure = None,
    ) -> plt.Figure:
        """Plot the results of the spatial tournament.
        Parameters
        ----------
        cmap : str, optional
            A matplotlib colour map, full list can be found at
            http://matplotlib.org/examples/color/colormaps_reference.html
        interpolation : str, optional
            A matplotlib interpolation, full list can be found at
            http://matplotlib.org/examples/images_contours_and_fields/interpolation_methods.html
        title : str, optional
            A title for the plot
        colorbar : bool, optional
            Choose whether the colorbar should be included or not
        labels : bool, optional
            Choose whether the axis labels and ticks should be included
        display_names : bool, optional
            Choose whether to display the names of the strategies
        ax: matplotlib axis
            Allows the plot to be written to a given matplotlib axis.
            Default is None.
        Returns
        ----------
        figure : matplotlib figure
            A heat plot of the results of the spatial tournament
        """
        if ax is None:
            fig, ax = plt.subplots()
        else:
            ax = ax

        fig = ax.get_figure()
        mat = ax.imshow(self.data, cmap=cmap, interpolation=interpolation)

        width = len(self.data) / 2
        height = width
        fig.set_size_inches(width, height)

        plt.xlabel("turns")
        ax.tick_params(axis="both", which="both", length=0)

        if display_names:
            plt.yticks(range(len(self.opponents)),
                       [str(player) for player in self.opponents])
        else:
            plt.yticks([0, len(self.opponents) - 1], [0, 1])
            plt.ylabel("Probability of cooperation")

        if not labels:
            plt.axis("off")

        if title is not None:
            plt.title(title)

        if colorbar:
            max_score = 0
            min_score = 1
            ticks = [min_score, 1 / 2, max_score]

            divider = make_axes_locatable(ax)
            cax = divider.append_axes("right", size="5%", pad=0.2)
            cbar = fig.colorbar(mat, cax=cax, ticks=ticks)

        plt.tight_layout()
        return fig
예제 #2
0
    def plot(
        self,
        cmap: str = "viridis",
        interpolation: str = "none",
        title: str = None,
        colorbar: bool = True,
        labels: bool = True,
        display_names: bool = False,
        ax: plt.Figure = None,
    ) -> plt.Figure:
        """Plot the results of the spatial tournament.
        Parameters
        ----------
        cmap : str, optional
            A matplotlib colour map, full list can be found at
            http://matplotlib.org/examples/color/colormaps_reference.html
        interpolation : str, optional
            A matplotlib interpolation, full list can be found at
            http://matplotlib.org/examples/images_contours_and_fields/interpolation_methods.html
        title : str, optional
            A title for the plot
        colorbar : bool, optional
            Choose whether the colorbar should be included or not
        labels : bool, optional
            Choose whether the axis labels and ticks should be included
        display_names : bool, optional
            Choose whether to display the names of the strategies
        ax: matplotlib axis
            Allows the plot to be written to a given matplotlib axis.
            Default is None.
        Returns
        ----------
        figure : matplotlib figure
            A heat plot of the results of the spatial tournament
        """
        if ax is None:
            fig, ax = plt.subplots()
        else:
            ax = ax

        fig = ax.get_figure()
        mat = ax.imshow(self.data, cmap=cmap, interpolation=interpolation)

        width = len(self.data) / 2
        height = width
        fig.set_size_inches(width, height)

        plt.xlabel("turns")
        ax.tick_params(axis="both", which="both", length=0)

        if display_names:
            plt.yticks(
                range(len(self.opponents)), [str(player) for player in self.opponents]
            )
        else:
            plt.yticks([0, len(self.opponents) - 1], [0, 1])
            plt.ylabel("Probability of cooperation")

        if not labels:
            plt.axis("off")

        if title is not None:
            plt.title(title)

        if colorbar:
            max_score = 0
            min_score = 1
            ticks = [min_score, 1 / 2, max_score]

            divider = make_axes_locatable(ax)
            cax = divider.append_axes("right", size="5%", pad=0.2)
            cbar = fig.colorbar(mat, cax=cax, ticks=ticks)

        plt.tight_layout()
        return fig