forked from cirosantilli/python-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared.py
More file actions
executable file
·62 lines (48 loc) · 1.48 KB
/
shared.py
File metadata and controls
executable file
·62 lines (48 loc) · 1.48 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
#!/usr/bin/env python
"""
Contains stuff that is common to all plots of a project
Takes as input exactly one command line argument which is the path
of a python file with a `plot` which has the same signature as `plot`:func:
The goal of this design is to separte separate plots into different files so that:
- it all works well with makefiles so that only plots corresponding to
modified `.py` files will be replotted on make
- the code is better organized
sample call:
./THIS_FILENAME.py subplots
"""
import sys
import os.path
import imp
import matplotlib.pyplot as plt
#global params:
out_dir = 'out'
out_ext = 'png'
class DefaultParameters:
"""
Encapsulates all the default plot parameters
"""
def plot(plt, params):
"""plot on an empty plt object
:param plt: a clean ``matplotlib.pyplot`` object
:type plt: ``matplotlib.pyplot``
:param params: default plot params. Function may override those defaults.
:type params: the class `DefaultParameters`:class: (not an instance)
"""
raise NotImplementedError
if __name__ == '__main__':
path = sys.argv[1]
name = os.path.split(os.path.splitext(path)[0])[1]
try:
plotter = imp.load_source(name, path)
except IOError:
print path
print name
raise
else:
plotter.plot(plt, DefaultParameters)
plt.savefig(
os.path.join( out_dir, name + '.' + out_ext),
format=out_ext,
bbox_inches='tight'
)
plt.clf()