-
Notifications
You must be signed in to change notification settings - Fork 0
/
plots_stuff.py
695 lines (573 loc) · 23.2 KB
/
plots_stuff.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
import os
import pickle
from itertools import repeat, zip_longest
from typing import List, Union, Callable, Tuple, Iterable, Dict, Iterator
import matplotlib as mpl
import matplotlib.cm as cm
import matplotlib.figure
import matplotlib.ticker
import mpl_toolkits
import mpl_toolkits.mplot3d
import numpy as np
from matplotlib import cycler, ticker
from matplotlib.colors import Normalize
from matplotlib.lines import Line2D
from matplotlib.pyplot import subplots, show
from scipy.interpolate import interp1d
from scipy.signal import get_window
from numpy import ma
plot_opts_imshow_waterfall = dict(aspect="auto", interpolation="nearest", origin='lower')
def matplotlib_IEEE_style():
"""IEEE style by Olga Galinina. Applies to all figures that will be created"""
mpl.rc('figure', facecolor='0.85')
mpl.rc('axes', edgecolor='(0.04, 0.14, 0.42)',
facecolor='(0.87, 0.92, 0.98)',
labelsize='medium',
labelweight='bold',
labelcolor='(0.04, 0.14, 0.42)',
grid='True')
mpl.rc('text', color='(0.04, 0.14, 0.42)')
mpl.rc('font', family='sans-serif', weight='bold', size=12)
mpl.rc('legend', fontsize='medium', handlelength=3)
mpl.rc('axes', prop_cycle=cycler(color=[
(0.04, 0.52, 0.78),
(0.01, 0.57, 0.58),
(1, 0, 0.6)]))
def matplotlib_WINTER_style():
"""WINTER style by Alex Pyattaev. Applies to all figures that will be created"""
mpl.rc('figure', facecolor='1',
titleweight='bold', # weight of the figure title
figsize=(9, 6)) # figure size in inches)
mpl.rc('axes', edgecolor='(0.04, 0.14, 0.42)',
# facecolor='(0.87, 0.92, 0.98)',
labelsize='large',
labelweight='bold',
# labelcolor='(0.04, 0.14, 0.42)',
grid='True')
# mpl.rc('text', color='(0.04, 0.14, 0.42)')
mpl.rc('lines', linewidth=2, markersize=9)
mpl.rc('font', family='Sans', weight='bold', size=12)
mpl.rc('legend', fontsize='medium', handlelength=3)
mpl.rc('errorbar', capsize=5)
mpl.rc('axes', prop_cycle=cycler(color=['blue', 'green', 'red'], marker=('v', 'o', '+')))
mpl.rc('font', **{'family': 'sans-serif', 'serif': ['Helvetica']})
def plot_timelines(data: Dict[str, np.ndarray], colors=None, time_axis=None, fig_args=None,
ax_args=None) -> mpl.figure.Figure:
if fig_args is None:
fig_args = {'figsize': [14, 10]}
if ax_args is None:
ax_args = {}
num_lines = len(data)
colormap = matplotlib.cm.get_cmap('jet')
if colors is None:
colors = [colormap(k) for k in np.linspace(0, 1, num_lines)]
f, axes = subplots(nrows=num_lines, ncols=1, sharex="all", **fig_args)
all_handles = []
all_labels = []
for ax, key, clr in zip(axes, data, colors):
if time_axis is None:
time_axis = np.arange(len(data[key]))
assert len(time_axis) == len(data[key]), "All timeline lengths must agree!"
ax.plot(data[key], label=f"{key}", color=clr, **ax_args)
ax.get_xaxis().set_major_locator(matplotlib.ticker.MaxNLocator(nbins='auto', min_n_ticks=10))
handles, labels = ax.get_legend_handles_labels()
all_handles += handles
all_labels += labels
ax.grid()
f.legend(all_handles, all_labels, 'right')
return f
def mpl_figure(title: str, xlabel: str = None, ylabel: str = None) -> mpl.figure.Figure:
import matplotlib.pyplot as plt
f = plt.figure()
plt.title(title)
if xlabel:
plt.xlabel(xlabel)
if ylabel:
plt.ylabel(ylabel)
plt.grid()
return f
def make_colors(keys: list, cmap=None) -> Callable[[object], list]:
if cmap is None:
if len(keys) < 5:
# noinspection PyUnresolvedReferences
cmap = cm.jet
else:
# noinspection PyUnresolvedReferences
cmap = cm.viridis
COLORS = [cmap(i) for i in np.linspace(0, 1, len(keys))]
def colors_fn(q):
return COLORS[keys.index(q)]
return colors_fn
def smooth(x, window_len=11, window='hanning'):
"""smooth the data using a window with requested size.
This method is based on the convolution of a scaled window with the signal.
The signal is prepared by introducing reflected copies of the signal
(with the window size) in both ends so that transient parts are minimized
in the begining and end part of the output signal.
input:
x: the input signal
window_len: the dimension of the smoothing window; should be an odd integer
window: the type of window from 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'
flat window will produce a moving average smoothing.
output:
the smoothed signal
example:
t=linspace(-2,2,0.1)
x=sin(t)+randn(len(t))*0.1
y=smooth(x)
see also:
numpy.hanning, numpy.hamming, numpy.bartlett, numpy.blackman, numpy.convolve
scipy.signal.lfilter
"""
if x.ndim != 1:
raise ValueError("smooth only accepts 1 dimension arrays.")
if x.size < window_len:
raise ValueError("Input vector needs to be bigger than window size.")
if window_len < 3:
return x
if window not in ['flat', 'hanning', 'hamming', 'bartlett', 'blackman']:
raise ValueError("Window must be one of 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'")
s = np.r_[x[window_len - 1:0:-1], x, x[-2:-window_len - 1:-1]]
# print(len(s))
if window == 'flat': # moving average
w = np.ones(window_len, 'd')
else:
w = get_window(window, window_len)
y = np.convolve(w / w.sum(), s, mode='valid')
return y[(window_len // 2 - 1):-(window_len // 2 + 1)]
def cdfplot(data: np.ndarray, yrange: float = 1.0):
"""
Generate CDF plot data
:param data: the data array
:param yrange: range of Y values, e.g. use 100 for percents
:return: the X and Y values to be used in plot
"""
X = np.sort(data)
L = X.shape[0]
Y = np.linspace(yrange / L, yrange, L)
return X, Y
def pdf_to_cdf_data(X, counts) -> Iterator:
"""
convert pdf (histogram data) to CDF inputs
:param X: X positions in histogram
:param counts: counts in histogram
:return: iterator over values (same dtype as stuff in X)
"""
for x, c in zip(X, counts):
for i in range(c):
yield x
def pdfplot(data: np.ndarray, final_samples: int = 100, Q: int = 6):
"""
Generate PDF of the data given
:param data: the data array (1-d)
:param final_samples: amount of samples in output. Keep this lower than data size!
:param Q: specifies fraction of original data span to cover on each side.
:return: the X and Y values to be used in plot
"""
if len(data) == 0:
raise ValueError("Data can not be empty")
X, Y = cdfplot(data)
if final_samples > X.shape[0] / 2:
raise ValueError("keep downsampling reasonable! not enough data points!")
# Prepare to interpolate the data for uniform support
f = interp1d(X, Y, kind="linear", fill_value=(0, 1), assume_sorted=True, bounds_error=False)
# Find limits of support data
# noinspection PyArgumentList
xmin = X.min()
# noinspection PyArgumentList
xmax = X.max()
span = xmax - xmin
# make even-spaced support with reserve value for ringing
offset = int(span / Q)
X = np.linspace(xmin - offset, xmax + offset, Y.shape[0])
Y = f(X)
# plt.plot(X, Y, label='sampled')
W = int(Y.shape[0] / final_samples * 2)
if W % 2 == 0:
W += 1
# print(W)
Y1 = smooth(Y, window='blackman', window_len=W)
# null out borders
Y1[Y == 0] = 0
Y1[Y == 1] = 1
# plt.plot(X,Y1, label='smoothed')
# Downsample
f = interp1d(X, Y1, kind="quadratic", fill_value=(0, 1), assume_sorted=True, bounds_error=False)
X = np.linspace(xmin - offset, xmax + offset, final_samples)
Y2 = f(X)
# get gradient (i.e. derivative)
Y2 = np.gradient(Y2, X[1] - X[0])
# Ensure resulting area is correct
area = np.trapz(Y2, X)
# print(area)
Y2 = Y2 / area
return X, Y2
def nice_cdf_plot(datas, title: str, names: Iterable = tuple(),
markers: Iterable = tuple(), styles: Iterable = tuple(),
linewidths: Iterable = tuple(),
colors: Iterable = tuple(), ignore_missed=False,
vertical_threshold_lines=(0.5, 2.0),
legend_outside=8,
xlabel='Distance, m',
ylabel='CDF, percent'):
"""
Makes a nice-looking CDF plot from multiple data sets.
:param datas:
:param title:
:param names:
:param markers:
:param styles:
:param colors:
:param ignore_missed:
:param vertical_threshold_lines:
:param xlabel:
:param ylabel:
:return:
"""
if not isinstance(datas, list):
datas = [datas]
names = [names]
f = matplotlib.pyplot.figure(figsize=[16, 10])
ax = matplotlib.pyplot.gca()
for data, tit, mrk, sty, clr, lw in zip_longest(datas, names, markers, styles, colors, linewidths):
if data is None:
raise ValueError(f'data and names length do not match {len(datas)}, {len(names)}')
if not ignore_missed:
data[data > 50] = 0
if len(data) == 0:
continue
X, Y = cdfplot(data, 100)
if title is None:
tit = ""
ax.plot(X, Y, color=clr, marker=mrk, linestyle=sty, label=tit, linewidth=lw)
ax.set_xlabel(xlabel)
ax.vlines(x=vertical_threshold_lines, ymin=0, ymax=100, label='thresholds', colors='k')
if len(datas) <= legend_outside:
ax.legend()
else:
ax.legend(bbox_to_anchor=(1.05, 1), loc='upper left')
power_scale_axes(ax, axes="x")
ax.set_ylabel(ylabel)
ax.set_title(title)
ax.xaxis.set_major_locator(ticker.MultipleLocator(1.0))
ax.yaxis.set_major_locator(ticker.MultipleLocator(10))
ax.grid()
matplotlib.pyplot.tight_layout()
return f
def draw_point_labels(ax: Union[matplotlib.figure.Axes, mpl_toolkits.mplot3d.axes3d.Axes3D],
P: np.ndarray, labels: List[str] = None, **kwargs) -> None:
"""
Draws points with autolabel_service
:param ax: axes to use. Can be 2d or 3d, either way will work
:param P: array of points
:param labels:
:param kwargs: kwargs passed to plot
"""
if labels is None:
labels = [f"{i}" for i in range(len(P))]
for l, p in zip(labels, P):
if isinstance(ax, mpl_toolkits.mplot3d.axes3d.Axes3D):
ax.text(p[0] + 0.1, p[1] + 0.1, p[2] + 0.1, l, **kwargs)
else:
ax.text(p[0] + 0.1, p[1] + 0.1, l, **kwargs)
def plot_cylinder(ax: mpl_toolkits.mplot3d.axes3d.Axes3D, pos, size: List[float], **kwargs):
"""Plot a cylinder in 3d.
:param ax: axes
:param pos: position (3-vect)
:param size: size (radius and height)
"""
P = 100
x = np.linspace(-size[0], size[0], P)
z = np.linspace(0, size[1], P)
Xc, Zc = np.meshgrid(x, z)
Yc = np.sqrt(size[0] - Xc ** 2)
Xc += pos[0]
Yc += pos[1]
Zc += pos[2]
# Draw parameters
rcount = 5
ccount = 10
ax.plot_surface(Xc, Yc, Zc, rcount=rcount, ccount=ccount, **kwargs)
ax.plot_surface(Xc, 2 * pos[1] - Yc, Zc, rcount=rcount, ccount=ccount, **kwargs)
def plot_line(ax, p1: np.ndarray, p2: np.ndarray, *args, **kwargs)->Line2D:
"""
Plot a line between two points on axes ax.
:param ax: axes to use. Can be 2d or 3d, either way will work
:param p1: first point
:param p2: second point
:param args: args passed to plot
:param kwargs: kwargs passed to plot
:return: None
"""
p = np.vstack((p1, p2))
if ax.name == "3d":
return ax.plot(p[:, 0], p[:, 1], *args, zs=p[:, 2], **kwargs)[0]
else:
return ax.plot(p[:, 0], p[:, 1], *args, **kwargs)[0]
def plot_var_thick(x, wmin: float = 0.5, wmax: float = 2, **style):
"""
Prepare a line collection to plot with variable thickness
:param x: coordinates of points to plot, can be 2d or 3d
:param wmin: minimal width
:param wmax: maximal width
:param style: stuff passed to plot
:return:
"""
T, D = x.shape
lwidths = np.linspace(wmin, wmax, T)
# Turn the points into segment groups
xx = x.reshape(-1, 1, D)
segments = np.concatenate([xx[:-1], xx[1:]], axis=1)
if D == 2:
from matplotlib.collections import LineCollection
return LineCollection(segments, linewidths=lwidths, **style)
elif D == 3:
from mpl_toolkits.mplot3d.art3d import Line3DCollection
return Line3DCollection(segments, linewidths=lwidths, **style)
else:
raise NotImplementedError("unsupported dimension {}".format(D))
def show_matrix(*args, vmin=None, vmax=None, block=True, row_break=5, sharex='none', sharey='none',
colormaps=repeat('viridis'),
**kwargs) -> Tuple[matplotlib.figure.Figure, Iterable[matplotlib.figure.Axes]]:
"""
:param args:
:param vmin:
:param vmax:
:param block:
:param row_break:
:param sharex:
:param sharey:
:param colormaps:
:param kwargs:
:return:
"""
kwargs.update({f"arg {i}": a for i, a in enumerate(args)})
colormaps = iter(colormaps)
N_cols = len(kwargs)
N_rows = N_cols // row_break + 1
if N_rows > 1:
N_cols = row_break
f, axs = subplots(N_rows, N_cols, squeeze=False, sharex=sharex, sharey=sharey)
for i, (name, data) in enumerate(kwargs.items()):
ndim = data.ndim
col = i % row_break
row = i // row_break
ax = axs[row, col]
ax.set_title(name)
if ndim == 1:
ax.plot(data, label=name)
ax.set_ylim([vmin, vmax])
elif ndim == 2:
im = ax.imshow(data, vmin=vmin, vmax=vmax, interpolation='nearest', aspect='auto', origin='lower',
cmap=next(colormaps))
f.colorbar(im, ax=ax)
else:
print(f"Provided input {name} has number of dimensions {ndim} which is not supported")
raise ValueError('number of dimensions not supported')
if block:
show(block=True)
return f, axs
def axisEqual3D(ax: mpl_toolkits.mplot3d.axes3d.Axes3D):
extents = np.array([getattr(ax, 'get_{}lim'.format(dim))() for dim in 'xyz'])
sz = extents[:, 1] - extents[:, 0]
centers = np.mean(extents, axis=1)
maxsize = max(abs(sz))
r = maxsize / 2
for ctr, dim in zip(centers, 'xyz'):
getattr(ax, 'set_{}lim'.format(dim))(ctr - r, ctr + r)
default_plot_path = "./plots" # The place where all plots will be dropped by default
def add_watermark(fig: matplotlib.figure.Figure, text: str)->None:
"""
Add a watermark to figure. Such as a version tag.
:param fig: figure to modify
:param text: watermark text.
"""
fig.text(1.0, 0.0, " "+text, color='gray', alpha=0.5, fontsize=8, ha='right', va='bottom', rotation=90)
def savefig(fig: matplotlib.figure.Figure, title: str,
path: str = default_plot_path,
img_formats: Iterable[str] = ('svg', 'png'), mplfig=False) -> None:
"""
Save a matplotlib figure into variety of formats.
Saves static snapshot into svg and png by default.
In addition, the figure is pickled into .mplfig file for further interactive use
:param fig: the figure handle (Matplotlib Figure)
:param title: the title part of filename
:param path: the saving prefix/directory
:param img_formats: the formats for static rendering
:param mplfig: Save interactive Matplotlib figure along with images
"""
assert isinstance(fig, matplotlib.figure.Figure), "Invalid object passed to savefig"
for fmt in img_formats:
fig.savefig(os.path.join(path, title + '.' + fmt), transparent=True, bbox_inches='tight')
if mplfig:
fobj = open(os.path.join(path, "{}.mplfig".format(title)), 'wb')
pickle.dump(fig, fobj)
fobj.close()
def loadfig(fname: str) -> object:
"""
Load a matplotlib figure from pickle file
may not work with some figures, use with caution
:param fname: filename to read (full path)
:return: the figure handle
"""
fobj = open(fname, 'rb')
fig = pickle.load(fobj)
fobj.close()
return fig
def setticks(ax, stepsize=1) -> None:
"""
Set ticks on axes at regular intervals. Useful for showing floorplans and other scaled image data
:param ax: axes to work on
:param stepsize: step size in plot units
"""
ax.get_xaxis().set_major_locator(matplotlib.ticker.MultipleLocator(base=stepsize))
ax.get_yaxis().set_major_locator(matplotlib.ticker.MultipleLocator(base=stepsize))
def draw_polygons(ax: matplotlib.figure.Axes, vertices: np.ndarray, color: str, linestyle: str = '-',
linewidth: int = 1):
"""Draw vertices (e.g. for cells) given array of polygon points"""
for arr in vertices:
ax.plot(arr[:, 0], arr[:, 1], color=color, linestyle=linestyle, linewidth=linewidth)
def power_scale_axes(ax: matplotlib.figure.Axes, axes: str = "x", scale: float = 0.7) -> None:
"""
:param ax: axes object to operate on
:param axes: axes to manipulate scale on. can be x,y or both
:param scale: power scale to apply
"""
def compress(x, pow=scale):
return np.sign(x) * (np.abs(x) ** pow)
def decompress(x, pow=scale):
return x ** 1 / pow
if "x" in axes:
ax.set_xscale('function', functions=(compress, decompress))
if "y" in axes:
ax.set_yscale('function', functions=(compress, decompress))
def remappedColorMap(cmap, start=0, midpoint=0.5, stop=1.0, name='shiftedcmap'):
'''
Function to offset the median value of a colormap, and scale the
remaining color range. Useful for data with a negative minimum and
positive maximum where you want the middle of the colormap's dynamic
range to be at zero.
Taken from https://stackoverflow.com/questions/7404116/defining-the-midpoint-of-a-colormap-in-matplotlib
https://github.com/TheChymera/chr-helpers/blob/d05eec9e42ab8c91ceb4b4dcc9405d38b7aed675/chr_matplotlib.py
Input
-----
cmap : The matplotlib colormap to be altered
start : Offset from lowest point in the colormap's range.
Defaults to 0.0 (no lower ofset). Should be between
0.0 and 0.5; if your dataset mean is negative you should leave
this at 0.0, otherwise to (vmax-abs(vmin))/(2*vmax)
midpoint : The new center of the colormap. Defaults to
0.5 (no shift). Should be between 0.0 and 1.0; usually the
optimal value is abs(vmin)/(vmax+abs(vmin))
stop : Offset from highets point in the colormap's range.
Defaults to 1.0 (no upper ofset). Should be between
0.5 and 1.0; if your dataset mean is positive you should leave
this at 1.0, otherwise to (abs(vmin)-vmax)/(2*abs(vmin))
'''
cdict = {
'red': [],
'green': [],
'blue': [],
'alpha': []
}
# regular index to compute the colors
reg_index = np.hstack([
np.linspace(start, 0.5, 128, endpoint=False),
np.linspace(0.5, stop, 129)
])
# shifted index to match the data
shift_index = np.hstack([
np.linspace(0.0, midpoint, 128, endpoint=False),
np.linspace(midpoint, 1.0, 129)
])
for ri, si in zip(reg_index, shift_index):
r, g, b, a = cmap(ri)
cdict['red'].append((si, r, r))
cdict['green'].append((si, g, g))
cdict['blue'].append((si, b, b))
cdict['alpha'].append((si, a, a))
newcmap = matplotlib.colors.LinearSegmentedColormap(name, cdict)
cm.register_cmap(cmap=newcmap)
return newcmap
def test_shifted_colormap():
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import AxesGrid
biased_data = np.random.random_integers(low=-15, high=5, size=(37, 37))
orig_cmap = matplotlib.cm.coolwarm
shifted_cmap = remappedColorMap(orig_cmap, midpoint=0.75, name='shifted')
shrunk_cmap = remappedColorMap(orig_cmap, start=0.15, midpoint=0.75, stop=0.85, name='shrunk')
fig = plt.figure(figsize=(6, 6))
grid = AxesGrid(fig, 111, nrows_ncols=(2, 2), axes_pad=0.5,
label_mode="1", share_all=True,
cbar_location="right", cbar_mode="each",
cbar_size="7%", cbar_pad="2%")
# normal cmap
im0 = grid[0].imshow(biased_data, interpolation="none", cmap=orig_cmap)
grid.cbar_axes[0].colorbar(im0)
grid[0].set_title('Default behavior (hard to see bias)', fontsize=8)
im1 = grid[1].imshow(biased_data, interpolation="none", cmap=orig_cmap, vmax=15, vmin=-15)
grid.cbar_axes[1].colorbar(im1)
grid[1].set_title('Centered zero manually,\nbut lost upper end of dynamic range', fontsize=8)
im2 = grid[2].imshow(biased_data, interpolation="none", cmap=shifted_cmap)
grid.cbar_axes[2].colorbar(im2)
grid[2].set_title('Recentered cmap with function', fontsize=8)
im3 = grid[3].imshow(biased_data, interpolation="none", cmap=shrunk_cmap)
grid.cbar_axes[3].colorbar(im3)
grid[3].set_title('Recentered cmap with function\nand shrunk range', fontsize=8)
for ax in grid:
ax.set_yticks([])
ax.set_xticks([])
if 'INTERACTIVE' in os.environ:
plt.show()
class MidPointNorm(Normalize):
def __init__(self, midpoint=0, vmin=None, vmax=None, clip=False):
Normalize.__init__(self, vmin, vmax, clip)
self.midpoint = midpoint
def __call__(self, value, clip=None):
if clip is None:
clip = self.clip
result, is_scalar = self.process_value(value)
self.autoscale_None(result)
vmin, vmax, midpoint = self.vmin, self.vmax, self.midpoint
if not (vmin < midpoint < vmax):
raise ValueError("midpoint must be between maxvalue and minvalue.")
elif vmin == vmax:
result.fill(0) # Or should it be all masked? Or 0.5?
elif vmin > vmax:
raise ValueError("maxvalue must be bigger than minvalue")
else:
vmin = float(vmin)
vmax = float(vmax)
if clip:
mask = ma.getmask(result)
result = ma.array(np.clip(result.filled(vmax), vmin, vmax),
mask=mask)
# ma division is very slow; we can take a shortcut
resdat = result.data
# First scale to -1 to 1 range, than to from 0 to 1.
resdat -= midpoint
resdat[resdat > 0] /= abs(vmax - midpoint)
resdat[resdat < 0] /= abs(vmin - midpoint)
resdat /= 2.
resdat += 0.5
result = ma.array(resdat, mask=result.mask, copy=False)
if is_scalar:
result = result[0]
return result
def inverse(self, value):
if not self.scaled():
raise ValueError("Not invertible until scaled")
vmin, vmax, midpoint = self.vmin, self.vmax, self.midpoint
if isinstance(value, Iterable):
val = ma.asarray(value)
val = 2 * (val - 0.5)
val[val > 0] *= abs(vmax - midpoint)
val[val < 0] *= abs(vmin - midpoint)
val += midpoint
return val
else:
val = 2 * (value - 0.5)
if val < 0:
return val * abs(vmin - midpoint) + midpoint
else:
return val * abs(vmax - midpoint) + midpoint