matplotlib图标签和plt.tight无法正常工作,我该如何解决此代码?

时间:2019-11-24 00:12:10

标签: python matplotlib plot

嗨,我已经生成了图,但无法使其正确拟合,该函数使用了外部库,不确定如何解决此问题?

 #My code      
opponents = [s() for s in axl.demo_strategies]
    for i, player in enumerate(g_players):
        tf = axl.TransitiveFingerprint(player, opponents=opponents)
        data = tf.fingerprint(turns=20, repetitions=10)
        ...  # Write data to disk for analysis
        plt = tf.plot(display_names=True)
        plt.savefig(f"plot_{i}.png")  # Saves a plot for each player of interest
        plt.show()

外部库为绘图生成了这段代码。

 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

我试图更改图形大小并尝试了其他解答,但是有没有matplotlib向导可以帮助您?

图的输出 output of plot

0 个答案:

没有答案