-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlotOptions.py
323 lines (264 loc) · 10.6 KB
/
PlotOptions.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# Bunch of overrides for matplotlib settings not changeable in
# matplotlibrc. To use,
#
# PCSJ
#
# >>> from CommonFiles.PlotOptions import PlotOptions, layout_pad
# >>> PlotOptions()
# >>> *** make plots here ***
# >>> fig.tight_layout(**layout_pad)
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from time import time
def PlotOptions(uselatex=False, ticks='out'):
import matplotlib
import matplotlib.axis, matplotlib.scale
from matplotlib.ticker import (MaxNLocator, NullLocator,
NullFormatter, ScalarFormatter)
MaxNLocator.default_params['nbins']=6
MaxNLocator.default_params['steps']=[1, 2, 5, 10]
def set_my_locators_and_formatters(self, axis):
# choose the default locator and additional parameters
if isinstance(axis, matplotlib.axis.XAxis):
axis.set_major_locator(MaxNLocator())
elif isinstance(axis, matplotlib.axis.YAxis):
axis.set_major_locator(MaxNLocator())
# copy&paste from the original method
axis.set_major_formatter(ScalarFormatter())
axis.set_minor_locator(NullLocator())
axis.set_minor_formatter(NullFormatter())
#override original method
matplotlib.scale.LinearScale.set_default_locators_and_formatters = \
set_my_locators_and_formatters
matplotlib.backend_bases.GraphicsContextBase.dashd = {
'solid': (None, None),
'dashed': (0, (2.0, 2.0)),
'dashdot': (0, (1.5, 2.5, 0.5, 2.5)),
'dotted': (0, (0.25, 1.50)),
}
# colors: 377EB8, E41A1C, 4DAF4A, 984EA3, FF7F00, FFFF33, A65628, F781BF
#medium darkness
matplotlib.colors.ColorConverter.colors['f'] = \
(55/255, 126/255, 184/255)
matplotlib.colors.ColorConverter.colors['h'] = \
(228/255, 26/255, 28/255)
matplotlib.colors.ColorConverter.colors['i'] = \
(77/255, 175/255, 74/255)
matplotlib.colors.ColorConverter.colors['j'] = \
(152/255, 78/255, 163/255)
matplotlib.colors.ColorConverter.colors['l'] = \
(255/255, 127/255, 0/255)
#lighter
matplotlib.colors.ColorConverter.colors['fl'] = \
(166/255, 206/255,227/255)
matplotlib.colors.ColorConverter.colors['hl'] = \
(251/255, 154/255, 153/255)
matplotlib.colors.ColorConverter.colors['il'] = \
(178/255, 223/255,138/255)
matplotlib.colors.ColorConverter.colors['jl'] = \
(202/255, 178/255, 214/255)
matplotlib.colors.ColorConverter.colors['ll'] = \
(253/255, 191/255, 111/255)
if uselatex:
matplotlib.rc('text', usetex=True)
matplotlib.rc('font', family='serif')
from matplotlib import rcParams
rcParams['xtick.direction'] = ticks
rcParams['ytick.direction'] = ticks
# Padding for formatting figures using tight_layout
layout_pad = {
'pad' : 0.05,
'h_pad' : 0.6,
'w_pad' : 0.6}
# Plot shortcuts for a number of circadian-relevant features
def plot_gray_zero(ax, **kwargs):
ax.axhline(0, ls='--', color='grey', **kwargs)
def format_2pi_axis(ax, x=True, y=False):
import numpy as np
if x:
ax.set_xticks([0, np.pi/2, np.pi, 3*np.pi/2, 2*np.pi])
ax.set_xlim([0, 2*np.pi])
ax.set_xticklabels(['$0$', r'$\nicefrac{\pi}{2}$', r'$\pi$',
r'$\nicefrac{3\pi}{2}$', r'$2\pi$'])
if y:
ax.set_yticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi])
ax.set_ylim([-np.pi, np.pi])
ax.set_yticklabels([r'$-\pi$', r'$-\frac{\pi}{2}$','$0$',
r'$\frac{\pi}{2}$', r'$\pi$'])
def format_4pi_axis(ax, x=True, y=False):
import numpy as np
if x:
ax.set_xticks([0, np.pi, 2*np.pi, 3*np.pi, 4*np.pi])
ax.set_xlim([0, 4*np.pi])
ax.set_xticklabels(['$0$', r'$\pi$', r'$2\pi$',
r'$3\pi$', r'$4\pi$'])
if y:
ax.set_yticks([-2*np.pi, -np.pi, 0, np.pi, 2*np.pi])
ax.set_ylim([-2*np.pi, 2*np.pi])
ax.set_yticklabels([r'-2$\pi$', r'-$\pi$','0',
r'$\pi$', r'2$\pi$'])
def format_npi_axis(ax, n=6, x=True, y=False):
import numpy as np
if x:
ax.set_xticks([nn*np.pi for nn in range(n+1)])
ax.set_xlim([0, n*np.pi])
ax.set_xticklabels(['$0$', r'$\pi$', r'$2\pi$',
r'$3\pi$', r'$4\pi$', '$5\pi$', r'$6\pi$'])
if y:
pass
# def highlight_xrange(ax, xmin, xmax, color='y', alpha=0.5, **kwargs):
# ax.axvspan(xmin, xmax, color=color, alpha=alpha, **kwargs)
class HistRCToggle:
""" Class to toggle the xtick directional update of
histogram-specific RC settings """
hist_params = {'xtick.direction' : 'out',
'ytick.direction' : 'out'}
def __init__(self):
self.rcdef = plt.rcParams.copy()
def on(self):
plt.rcParams.update(self.hist_params)
def off(self):
plt.rcParams.update(self.rcdef)
blue = '#9999ff'
red = '#ff9999'
'''
def histogram(ax, data1=None, data2=None, color1=blue, color2=red,
bins=20, range=None, alpha=1., label1=None, label2=None):
""" Function to display a pretty histogram of up to two different
distributions. Approximates transparency to get around annoying
issue of pdflatex and matplotlib. """
from mimic_alpha import colorAlpha_to_rgb
weights1 = np.ones_like(data1)/len(data1)
hist1 = ax.hist(data1, range=range, bins=bins, weights=weights1,
facecolor=color1, edgecolor='white', label=label1)
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('axes', -0.05))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('axes', -0.05))
if range:
ax.set_xlim([range[0]*(1-1E-3), range[1]*(1+1E-3)])
ax.xaxis.grid(False)
ax.yaxis.grid(False)
if data2 is not None:
weights2 = np.ones_like(data2)/len(data2)
c2_on_w = colorAlpha_to_rgb(color2, alpha=0.5, bg='w')[0]
c2_on_c1 = colorAlpha_to_rgb(color2, alpha=0.5, bg=color1)[0]
hist2 = ax.hist(data2, range=range, bins=bins, weights=weights2,
facecolor=c2_on_w, edgecolor='white',
label=label2)
ax.legend(loc='upper left')
orders = hist2[0] > hist1[0]
for i, order in enumerate(orders):
if order:
hist1[-1][i].set_facecolor(c2_on_c1)
hist1[-1][i].set_zorder(2)
else:
hist2[-1][i].set_facecolor(c2_on_c1)
return (hist1, hist2)
else:
ax.legend(loc='upper left')
return hist1
'''
def boxplot(ax, data, color='k', sym='b.'):
""" Create a nice-looking boxplot with the data in data. Columns
should be the different samples. sym handles the outlier mark,
default is no mark. """
data = np.asarray(data)
# Shortcut method if there is no nan data
if not np.any(np.isnan(data)): cdata = data
else:
cdata = [col[~np.isnan(col)] for col in data.T]
bp = ax.boxplot(cdata, sym=sym, widths=0.65)
plt.setp(bp['medians'], color=color, linewidth=0.75,
solid_capstyle='butt')
plt.setp(bp['boxes'], color=color, linewidth=0.5)
plt.setp(bp['whiskers'], color=color, linewidth=0.5, linestyle='--',
dashes=(4,3))
plt.setp(bp['caps'], color=color, linewidth=0.5)
plt.setp(bp['fliers'], markerfacecolor=color, markeredgecolor=color)
hide_spines(ax)
def hide_spines(ax):
"""Hides the top and rightmost axis spines from view for all active
figures and their respective axes."""
# Disable spines.
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
# Disable ticks.
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
def density_contour(ax, xdata, ydata, nbins_x, nbins_y, range=None,
levels=None, **contour_kwargs):
""" Create a density contour plot.
Parameters
----------
xdata : numpy.ndarray
ydata : numpy.ndarray
nbins_x : int
Number of bins along x dimension
nbins_y : int
Number of bins along y dimension
ax : matplotlib.Axes (optional)
If supplied, plot the contour to this axis. Otherwise, open a
new figure
contour_kwargs : dict
kwargs to be passed to pyplot.contour()
"""
H, xedges, yedges = np.histogram2d(xdata, ydata,
bins=(nbins_x, nbins_y),
normed=True, range=range)
x_bin_sizes = (xedges[1:] - xedges[:-1]).reshape((1,nbins_x))
y_bin_sizes = (yedges[1:] - yedges[:-1]).reshape((nbins_y,1))
pdf = (H*(x_bin_sizes*y_bin_sizes))
X, Y = 0.5*(xedges[1:]+xedges[:-1]), 0.5*(yedges[1:]+yedges[:-1])
Z = pdf.T
contour = ax.contour(X, Y, Z, levels=levels, colors='0.2')
ax.contourf(X, Y, Z, levels=levels, cmap=matplotlib.cm.PuBu,
**contour_kwargs)
return contour
def lighten_color(color, degree):
cin = matplotlib.colors.colorConverter.to_rgb(color)
cw = np.array([1.0]*3)
return tuple(cin + (cw - cin)*degree)
def color_range(NUM_COLORS, cm=None):
if cm is None: cm = matplotlib.cm.get_cmap('gist_rainbow')
return (cm(1.*i/NUM_COLORS) for i in range(NUM_COLORS))
def jitter_uni(x_values, x_range = None):
"""Adds jitter in the x direction according to
http://matplotlib.1069221.n5.nabble.com/jitter-in-matplotlib-td12573.html
where we use a uniform distribution in x."""
if len(x_values)==1:
print("No need to jitter_uni, single x value")
return
from scipy import stats
if x_range == None:
x_range = (np.max(x_values) - np.min(x_values))/len(np.unique(x_values))
jitter = x_values + stats.uniform.rvs(-x_range,2*x_range,len(x_values))
return jitter
def jitter_norm(y_values, y_range = None):
"""Adds jitter in the y direction according to
http://stackoverflow.com/questions/8671808/matplotlib-preventing-overlaying-datapoints
where we use a normal distribution in y."""
if len(y_values)==1:
print("No need to jitter_norm, single y value")
return
y_range = .01*(np.max(y_values)-min(y_values))
jitter = y_values + np.random.randn(len(y_values)) * y_range
return jitter
class laptimer:
"""
Whenever you call it, it times laps.
"""
def __init__(self):
self.time = time()
def __call__(self):
ret = time() - self.time
self.time = time()
return ret
def __str__(self):
return "%.3E"%self()
def __repr__(self):
return "%.3E"%self()