Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python matplotlib绘图标签&;plt.tight不起作用,如何修复此代码?_Python_Matplotlib_Plot - Fatal编程技术网

Python matplotlib绘图标签&;plt.tight不起作用,如何修复此代码?

Python matplotlib绘图标签&;plt.tight不起作用,如何修复此代码?,python,matplotlib,plot,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=

您好,我已经生成了绘图,但无法正确拟合,函数使用外部库,我不确定如何修复此问题

 #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向导可以提供帮助

绘图输出

该函数编写得不好;它来自哪里?问题在于它将pyplot与创建的对象(可能是外部对象)混合在一起。签名中还有一个错误。但是,由于它不是很复杂,您可以在自己的scipt中重新创建它的功能。无论如何,您可以将该功能复制到您的代码中,并删除行
fig.set\u size\u inches(width,height)
。这应该足以不让数字缩小。