【问题标题】:Plot something with matplotlib/pyplot and save the plot only (centered, with equal aspect ratio and without axes or borders)使用 matplotlib/pyplot 绘图并仅保存绘图(居中,具有相等的纵横比且没有轴或边框)
【发布时间】:2017-12-17 00:00:34
【问题描述】:

我只想将生成的绘图图像保存到文件中并编写以下 MCVE:

import matplotlib.pyplot as plt

plt.rcParams["axes.facecolor"] = "red"
plt.rcParams["savefig.facecolor"] = "red"
plt.rcParams["savefig.bbox"] = "tight"
plt.rcParams["savefig.pad_inches"] = 0

plt.plot([0, 1], [0, 0], "b-", linewidth="10")
plt.plot([1, 1], [0, 1], "b-", linewidth="10")
plt.plot([1, 0], [1, 1], "b-", linewidth="10")
plt.plot([0, 0], [1, 0], "b-", linewidth="10")

#plt.axis("equal")
#plt.axis("off")
plt.savefig("test.png")


plt.axis("equal")
#plt.axis("off")


#plt.axis("equal")
plt.axis("off")


plt.axis("equal")
plt.axis("off")


我发现了 bboxpad_inches。但是,它仍然不完美。轴似乎是隐藏的,而不是完全关闭 - 所以情节不会居中。另外,我想我什至需要将轴设置为相等的比例,否则矩形不会是正方形?

我想要的是

【问题讨论】:

    标签: python image matplotlib plot


    【解决方案1】:

    plt.rcParams["savefig.bbox"] = "tight" 自动调整图形周围的填充。这似乎是不受欢迎的。将其保留并创建一个没有边距的正方形图形 (plt.subplots_adjust(0,0,1,1)) 将允许轴占据图形中的所有空间。这样就不需要再专门设置方面了,但当然对于可能仍然有意义的不同情况。

    import matplotlib.pyplot as plt
    
    plt.rcParams["axes.facecolor"] = "red"
    plt.rcParams["savefig.facecolor"] = "red"
    
    plt.figure(figsize=(4,4))
    plt.subplots_adjust(0,0,1,1)
    plt.plot([0, 1, 1, 0,0], [0, 0,1,1,0], "b-", linewidth="10", solid_joinstyle="miter")
    
    #plt.gca().set_aspect("equal",adjustable="box" ) # <- not needed
    plt.axis("off")
    plt.savefig("test.png")
    plt.show()
    

    要获得没有红色边框的图形,可以将数据边距设置为0%(plt.margins(0));但是,由于线条是半切的,因此将其线宽加倍以获得相同的蓝色边框是有意义的。

    import matplotlib.pyplot as plt
    
    plt.rcParams["axes.facecolor"] = "red"
    plt.rcParams["savefig.facecolor"] = "red"
    
    plt.figure(figsize=(4,4))
    plt.subplots_adjust(0,0,1,1)
    plt.plot([0, 1, 1, 0,0], [0, 0,1,1,0], "b-", linewidth=20, solid_joinstyle="miter")
    
    #plt.gca().set_aspect("equal",adjustable="box" ) # <- not needed
    plt.axis("off")
    plt.margins(0.0)
    plt.savefig("test.png")
    plt.show()
    

    【讨论】:

    • 参数是图形大小的分数。如果您希望子图的右侧(在本例中为一个子图)在右图形边缘处紧,则设置 right=1(表示图形宽度的 100%)。左侧子图侧设置为 0,因此在图形边缘和子图之间基本上没有空间。换句话说,plt.subplots_adjust(0,0,1,1) 使子图与图形本身一样大。 (这在这种情况下效果很好,因为该图形与子图具有相同的方面,都是正方形。)
    • 嗯,不是真的。如果您保持轴可见,如果轴线(脊椎)不完全是一个像素宽(默认情况下不是这样),那么它的一半可能仍然在图中。 (这在 jupyter notebook 中又是不同的,其中 dpi 设置为 1 像素宽。)令人困惑的事情,底线:在保存方面,您仍然可以将其关闭。
    • axes.facecolor 如果您只对保存的图像感兴趣,可以省略。为了在屏幕上显示,它需要有轴背景为红色。
    【解决方案2】:

    您可以定义一个正方形的图形并使用tight_layout() 自动使填充紧凑。

    import matplotlib.pyplot as plt
    
    # Creates a figure object of size 6x6 inches
    fig = plt.figure(figsize=(6,6))
    
    plt.rcParams["axes.facecolor"] = "red"
    plt.rcParams["savefig.facecolor"] = "red"
    plt.rcParams["savefig.bbox"] = "tight"
    plt.rcParams["savefig.pad_inches"] = 0
    
    plt.plot([0, 1], [0, 0], "b-", linewidth="10")
    plt.plot([1, 1], [0, 1], "b-", linewidth="10")
    plt.plot([1, 0], [1, 1], "b-", linewidth="10")
    plt.plot([0, 0], [1, 0], "b-", linewidth="10")
    
    plt.axis("equal")
    plt.axis("off")
    fig.tight_layout()
    fig.savefig("test.png")
    

    【讨论】:

    • 快速解决方法是使用fig.subplots_adjust(top=1.0, left=0.0, right=1.0, bottom=0.0)。这将使您的身材居中。
    猜你喜欢
    • 1970-01-01
    • 2011-12-29
    • 1970-01-01
    • 1970-01-01
    • 2013-05-01
    • 2017-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多