def plot_save(path, figs=None, dpi=180, tight_plot=False, clear_all=True, log=True):
"""
Parameters
----------
clear_all: bool
if True, remove all saved figures from current figure list
in matplotlib
"""
try:
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt
if tight_plot:
plt.tight_layout()
if os.path.exists(path) and os.path.isfile(path):
os.remove(path)
pp = PdfPages(path)
if figs is None:
figs = [plt.figure(n) for n in plt.get_fignums()]
for fig in figs:
fig.savefig(pp, format='pdf', bbox_inches="tight")
pp.close()
if log:
sys.stderr.write('Saved pdf figures to:%s \n' % str(path))
if clear_all:
plt.close('all')
except Exception as e:
sys.stderr.write('Cannot save figures to pdf, error:%s \n' % str(e))
评论列表
文章目录