-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplot_utils.py
129 lines (106 loc) · 3.48 KB
/
plot_utils.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import argparse
import numpy as np
import tqdm.auto as tqdm
import math
import tempfile
import os
import atexit
import shutil
C = np.array([
(230, 159, 0),
(86, 180, 233),
(0, 158, 115),
(240, 228, 66),
(0, 114, 178),
]) / 255
def import_matplotlib():
mpldir = tempfile.mkdtemp()
atexit.register(shutil.rmtree, mpldir)
umask = os.umask(0)
os.umask(umask)
os.chmod(mpldir, 0o777 & ~umask)
os.environ['MPLCONFIGDIR'] = mpldir
import matplotlib
if not os.environ.get('DISPLAY'):
matplotlib.use('PDF')
import matplotlib.texmanager
class TexManager(matplotlib.texmanager.TexManager):
texcache = os.path.join(mpldir, 'tex.cache')
matplotlib.texmanager.TexManager = TexManager
pdfmpl = {
# Use LaTeX to write all text
"text.usetex": matplotlib.checkdep_usetex(True),
"font.family": "serif",
# Use 10pt font in plots, to match 10pt font in document
"axes.labelsize": 25,
"font.size": 23,
# Make the legend/label fonts a little smaller
"legend.fontsize": 14,
"xtick.labelsize": 24,
"ytick.labelsize": 24,
"figure.figsize": (8, 4),
"figure.dpi": 100,
"legend.loc": 'best',
'axes.titlepad': 20,
'pdf.use14corefonts': True,
'ps.useafm': True,
'lines.linewidth': 3,
'lines.markersize': 10,
}
matplotlib.rcParams.update(pdfmpl)
import_matplotlib()
import matplotlib.pyplot as plt
def format_axes(ax, twinx=False):
SPINE_COLOR = 'gray'
if twinx:
visible_spines = ['bottom', 'right']
invisible_spines = ['top', 'left']
else:
visible_spines = ['bottom', 'left']
invisible_spines = ['top', 'right']
for spine in invisible_spines:
ax.spines[spine].set_visible(False)
for spine in visible_spines:
ax.spines[spine].set_color(SPINE_COLOR)
ax.spines[spine].set_linewidth(0.5)
ax.xaxis.set_ticks_position('bottom')
if twinx:
ax.yaxis.set_ticks_position('right')
else:
ax.yaxis.set_ticks_position('left')
for axis in [ax.xaxis, ax.yaxis]:
axis.set_tick_params(direction='out', color=SPINE_COLOR)
return ax
import matplotlib.pyplot as plt
from matplotlib.ticker import Locator
class MinorSymLogLocator(Locator):
"""
Dynamically find minor tick positions based on the positions of
major ticks for a symlog scaling.
"""
def __init__(self, linthresh):
"""
Ticks will be placed between the major ticks.
The placement is linear for x between -linthresh and linthresh,
otherwise its logarithmically
"""
self.linthresh = linthresh
def __call__(self):
'Return the locations of the ticks'
majorlocs = self.axis.get_majorticklocs()
# iterate through minor locs
minorlocs = []
# handle the lowest part
for i in range(1, len(majorlocs)):
majorstep = majorlocs[i] - majorlocs[i-1]
if abs(majorlocs[i-1] + majorstep/2) < self.linthresh:
ndivs = 10
else:
ndivs = 9
minorstep = majorstep / ndivs
locs = np.arange(majorlocs[i-1], majorlocs[i], minorstep)[1:]
minorlocs.extend(locs)
return self.raise_if_exceeds(np.array(minorlocs))
def tick_values(self, vmin, vmax):
raise NotImplementedError('Cannot get tick locations for a '
'%s type.' % type(self))