【问题标题】:Matplotlib with annotation cut off from the saved figureMatplotlib 带有从保存的图形中截断的注释
【发布时间】:2015-07-06 05:47:40
【问题描述】:

我正在使用 matplotlib 在使用注释时绘制一些图形。我遇到的问题是注释(在绘图区域之外)从保存的图片中被切断,如下图所示。

下图就是我想要的:

有人知道如何解决这个问题吗?我注意到人们建议在 rcParams 中使用 plt.tight_plot() 或 fig.autolayout,但这似乎不起作用。下面是生成图形的代码。

fig, ax = plt.subplots()
ax.set_xlim([-0.02,1.1])
ax.set_ylim([-0.02,1.1])

ax.plot([0,0,0,0.5,0.5,0.5,1,1,1], [0,0.5,1,0,0.5,1,0,0.5,1], 'go')

ax.annotate("Digit 2",
            xy=(0.5, -0.1), xycoords='data',
            xytext=(0.5, -0.3), textcoords='data',
            arrowprops=dict(arrowstyle="->",
                            connectionstyle="arc3"),
             annotation_clip=False,
             fontsize = 12,
             ha='center',
            )

ax.annotate("Level 2",
            xy=(-0.1, 1), xycoords='data',
            xytext=(-0.35, 1), textcoords='data',
            arrowprops=dict(arrowstyle="->",
                            connectionstyle="arc3"),
                    annotation_clip=False,
                    fontsize = 12,
                    va='center',
            )

plt.savefig('sample.png', dpi = 300)

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    使用bbox_inches 参数保存图

    plt.savefig('sample.png', bbox_inches="tight")
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-22
    • 2015-12-19
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 2016-10-13
    • 2020-03-27
    相关资源
    最近更新 更多