Memo‎ > ‎

Save the subplot of matplotlib

posted Apr 18, 2019, 7:46 PM by Teng-Yok Lee   [ updated Apr 18, 2019, 7:47 PM ]
REF: https://stackoverflow.com/questions/4325733/save-a-subplot-in-matplotlib

The solution is calling ax.get_tightbox() to get the entire region and converting per the figure's DPI. Based on the answer above, I wrote a short function to combine multiple axes:

def SaveAxes(fig, axes, fig_filepath):
    extents = [];
    for ax in axes:
        extents.append(ax.get_tightbbox(fig.canvas.get_renderer()).transformed(fig.dpi_scale_trans.inverted()))
    extent = Bbox.union(extents);
    plt.savefig(fig_filepath, bbox_inches = extent);

Comments