删除pyplot中的帧

时间:2018-05-03 08:47:02

标签: python matplotlib

如何删除pyplot图像中的删除边框? 我尝试了几种方法,但似乎没有一种方法适合我。 对我来说,保持自定义轴也很重要,但我想将它们移动到绝对顶部/右侧/左侧/底部,因此边框是不可见的。

Generated image

How it should look like

def draw(screen, palette, figures, output):
    my_dpi = 100

    size = plt.rcParams["figure.figsize"]

    size[0] = screen.height / my_dpi
    size[1] = screen.width / my_dpi

    plt.rcParams['figure.facecolor'] = screen.bg_color.code

    fig, ax = plt.subplots()
    plt.axis([0, screen.height, 0, screen.width])
    plt.axis('off')

    for figure in figures:
        col = figure.color
        if not col:
            col = screen.fg_color

        # --- #

        elif isinstance(figure, Circle):
            shape = plt.Circle((figure.point.x, figure.point.y), figure.radius, color=col.code, clip_on=True)
            ax.add_patch(shape)

        # --- #

    fig.show()
    if output:
        fig.savefig(output, facecolor=fig.get_facecolor(), bbox_inches='tight', pad_inches=0)


import matplotlib.pyplot as plt

可运行的例子:

import matplotlib.pyplot as plt

def draw():
    my_dpi = 100
    height = 600
    width = 800
    size = plt.rcParams["figure.figsize"]

    size[0] = height / my_dpi
    size[1] = width / my_dpi

    plt.rcParams['figure.facecolor'] = "#2c3e50"

    fig, ax = plt.subplots()
    plt.axis([0, height, 0, width])

    plt.axis('off')

    shape = plt.Circle((40, 40), 40, color="#e67e22", clip_on=True)
    ax.add_patch(shape)

    shape = plt.Circle((600, 800), 100, color="#e67e22", clip_on=True)
    ax.add_patch(shape)

    fig.show()
    fig.savefig("./shape.png", facecolor=fig.get_facecolor(), bbox_inches='tight', pad_inches=0)

1 个答案:

答案 0 :(得分:0)

一种解决方案是使用plt.subplots_adjust手动设置轴和图形边缘之间的空白,并将所有内容设置为0或1(取决于它是什么参数)。您还需要删除修改fig.savefig调用中的图形参数的参数,因为它们将覆盖之前设置的plt.subplots_adjust参数。

使用您的示例:

def draw():
    my_dpi = 100
    height = 600
    width = 800
    size = plt.rcParams["figure.figsize"]

    size[0] = height / my_dpi
    size[1] = width / my_dpi

    plt.rcParams['figure.facecolor'] = "#2c3e50"

    fig, ax = plt.subplots()
    plt.axis([0, height, 0, width])

    plt.axis('off')

    shape = plt.Circle((40, 40), 40, color="#e67e22", clip_on=True)
    ax.add_patch(shape)

    shape = plt.Circle((600, 800), 100, color="#e67e22", clip_on=True)
    ax.add_patch(shape)

    plt.subplots_adjust(left=0, right=1, top=1, bottom=0)
    fig.savefig("./shape.png", facecolor=fig.get_facecolor())

    plt.show()

draw()
相关问题