-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpdfplot.py
More file actions
70 lines (58 loc) · 2.04 KB
/
Copy pathpdfplot.py
File metadata and controls
70 lines (58 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import os
import subprocess
import platform
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
import time
import warnings
def save_figs(filename=None, open=True, folder='.', save_latest=True):
"""
:param filename: (optional) path to pdf file for saving.
:param open: boolean flag for opening pdf file after saving
:param folder: folder to save pdf files (default=current dir)
:param save_latest: boolean flag for creating a "latest.pdf" in folder directory, symlinked to latest plots.
:return:
"""
if filename is None:
filename = time.strftime("%Y%m%d-%H%M%S.pdf")
if folder is not None:
if not os.path.exists(folder):
os.makedirs(folder, exist_ok=True)
filename = os.path.join(folder, filename)
fn = os.path.join(os.getcwd(), filename)
pp = PdfPages(fn)
for i in plt.get_fignums():
plt.figure(i).tight_layout()
pp.savefig(plt.figure(i))
plt.close(plt.figure(i))
pp.close()
if save_latest:
try:
latest_path = os.path.join(folder, 'latest.pdf')
if os.path.exists(latest_path):
os.remove(latest_path)
os.symlink(filename, latest_path)
if open:
_open_figs(latest_path)
return
except OSError:
warnings.warn('Cannot create symbolic link in Windows without administrator privileges. Skipping.')
if open:
_open_figs(filename)
def _open_figs(filename):
pdf_path = os.path.abspath(filename)
if os.path.exists(pdf_path):
if platform.system() == 'Darwin': # macOS
subprocess.call(('open', pdf_path))
elif platform.system() == 'Windows': # Windows
os.startfile(pdf_path)
else: # linux variants
subprocess.call(('xdg-open', pdf_path))
def make_fig():
"""
Returns figure and axis for plotting. Can easily override to adjust defaults.
:return:
"""
fig, ax = plt.subplots()
fig.tight_layout()
return fig, ax