-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathternary_axes_subplot.py
481 lines (403 loc) · 16.4 KB
/
ternary_axes_subplot.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
"""
Wrapper class for all ternary plotting functions.
"""
from collections import namedtuple
from functools import partial
import numpy as np
from matplotlib import pyplot as plt
from . import heatmapping
from . import lines
from . import plotting
from .helpers import project_point, convert_coordinates_sequence
BackgroundParameters = namedtuple('BackgroundParameters', ['color', 'alpha', 'zorder'])
def figure(ax=None, scale=None, permutation=None):
"""
Wraps a Matplotlib AxesSubplot or generates a new one. Emulates matplotlib's
> figure, ax = plt.subplots()
Parameters
----------
ax: AxesSubplot, None
The matplotlib AxesSubplot to wrap
scale: float, None
The scale factor of the ternary plot
"""
ternary_ax = TernaryAxesSubplot(ax=ax, scale=scale, permutation=permutation)
return ternary_ax.get_figure(), ternary_ax
def mpl_redraw_callback(event, tax):
"""
Callback to properly rotate and redraw text labels when the plot is drawn
or resized.
Parameters
----------
event: a matplotlib event
either 'resize_event' or 'draw_event'
tax: TernaryAxesSubplot
the TernaryAxesSubplot
"""
tax._redraw_labels()
class TernaryAxesSubplot(object):
"""
Wrapper for python-ternary and matplotlib figure. Parameters for member
functions simply pass through to ternary's functions with the same names.
This class manages the matplotlib axes, the scale, and the boundary scale
to ease the use of ternary plotting functions.
"""
def __init__(self, ax=None, scale=None, permutation=None):
if not scale:
scale = 1.0
if ax:
self.ax = ax
else:
_, self.ax = plt.subplots()
self.set_scale(scale=scale)
self._permutation = permutation
self._boundary_scale = scale
# Container for the axis labels supplied by the user
self._labels = dict()
self._corner_labels = dict()
self._ticks = dict()
# Container for the redrawing of labels
self._to_remove = []
self._connect_callbacks()
# Background
self._background_parameters = None
# Cache for the background triangle object, so it can be removed and redrawn as needed.
self._background_triangle = None
self.set_background_color(color="whitesmoke", zorder=-1000, alpha=0.75)
def _connect_callbacks(self):
"""Connect resize matplotlib callbacks."""
figure = self.get_figure()
callback = partial(mpl_redraw_callback, tax=self)
event_names = ('resize_event', 'draw_event')
for event_name in event_names:
figure.canvas.mpl_connect(event_name, callback)
def __repr__(self):
return "TernaryAxesSubplot: %s" % self.ax.__hash__()
def get_axes(self):
"""Returns the underlying matplotlib AxesSubplot object."""
return self.ax
def get_figure(self):
"""Return the underlying matplotlib figure object."""
ax = self.get_axes()
return ax.get_figure()
def set_scale(self, scale=None):
self._scale = scale
self.resize_drawing_canvas()
def get_scale(self):
return self._scale
def set_axis_limits(self, axis_limits=None):
"""
Set min and max data limits for each of the three axes.
axis_limits = dict
keys are 'b','l' and 'r' for the three axes
vals are lists of the min and max values for the axis in
data units.
"""
self._axis_limits = axis_limits
def get_axis_limits(self):
return self._axis_limits
# Title and Axis Labels
def set_title(self, title, **kwargs):
"""Sets the title on the underlying matplotlib AxesSubplot."""
ax = self.get_axes()
ax.set_title(title, **kwargs)
def left_axis_label(self, label, position=None, rotation=60, offset=0.08,
**kwargs):
"""
Sets the label on the left axis.
Parameters
----------
label: String
The axis label
position: 3-Tuple of floats, None
The position of the text label
rotation: float, 60
The angle of rotation of the label
offset: float,
Used to compute the distance of the label from the axis
kwargs:
Any kwargs to pass through to matplotlib.
"""
if not position:
position = (-offset, 3./5, 2./5)
self._labels["left"] = (label, position, rotation, kwargs)
def right_axis_label(self, label, position=None, rotation=-60, offset=0.08,
**kwargs):
"""
Sets the label on the right axis.
Parameters
----------
label: String
The axis label
position: 3-Tuple of floats, None
The position of the text label
rotation: float, -60
The angle of rotation of the label
offset: float,
Used to compute the distance of the label from the axis
kwargs:
Any kwargs to pass through to matplotlib.
"""
if not position:
position = (2. / 5 + offset, 3. / 5, 0)
self._labels["right"] = (label, position, rotation, kwargs)
def bottom_axis_label(self, label, position=None, rotation=0, offset=0.02,
**kwargs):
"""
Sets the label on the bottom axis.
Parameters
----------
label: String
The axis label
position: 3-Tuple of floats, None
The position of the text label
rotation: float, 0
The angle of rotation of the label
offset: float,
Used to compute the distance of the label from the axis
kwargs:
Any kwargs to pass through to matplotlib.
"""
if not position:
position = (0.5, -offset / 2., 0.5)
self._labels["bottom"] = (label, position, rotation, kwargs)
def right_corner_label(self, label, position=None, rotation=0, offset=0.08,
**kwargs):
"""
Sets the label on the right corner (complements left axis).
Parameters
----------
label: String
The axis label
position: 3-Tuple of floats, None
The position of the text label
rotation: float, 0
The angle of rotation of the label
offset: float,
Used to compute the distance of the label from the axis
kwargs:
Any kwargs to pass through to matplotlib.
"""
if not position:
position = (1, offset / 2, 0)
self._corner_labels["right"] = (label, position, rotation, kwargs)
def left_corner_label(self, label, position=None, rotation=0, offset=0.08,
**kwargs):
"""
Sets the label on the left corner (complements right axis.)
Parameters
----------
label: string
The axis label
position: 3-Tuple of floats, None
The position of the text label
rotation: float, 0
The angle of rotation of the label
offset: float,
Used to compute the distance of the label from the axis
kwargs:
Any kwargs to pass through to matplotlib.
"""
if not position:
position = (-offset / 2, offset / 2, 0)
self._corner_labels["left"] = (label, position, rotation, kwargs)
def top_corner_label(self, label, position=None, rotation=0, offset=0.2,
**kwargs):
"""
Sets the label on the bottom axis.
Parameters
----------
label: String
The axis label
position: 3-Tuple of floats, None
The position of the text label
rotation: float, 0
The angle of rotation of the label
offset: float,
Used to compute the distance of the label from the axis
kwargs:
Any kwargs to pass through to matplotlib.
"""
if not position:
position = (-offset / 2, 1 + offset, 0)
self._corner_labels["top"] = (label, position, rotation, kwargs)
def annotate(self, text, position, **kwargs):
ax = self.get_axes()
p = project_point(position)
ax.annotate(text, (p[0], p[1]), **kwargs)
# Boundary and Gridlines
def boundary(self, scale=None, axes_colors=None, **kwargs):
# Sometimes you want to draw a bigger boundary
if not scale:
scale = self._boundary_scale # defaults to self._scale
ax = self.get_axes()
self.resize_drawing_canvas(scale)
lines.boundary(scale=scale, ax=ax, axes_colors=axes_colors, **kwargs)
def gridlines(self, multiple=None, horizontal_kwargs=None, left_kwargs=None,
right_kwargs=None, **kwargs):
ax = self.get_axes()
scale = self.get_scale()
lines.gridlines(scale=scale, multiple=multiple,
ax=ax, horizontal_kwargs=horizontal_kwargs,
left_kwargs=left_kwargs, right_kwargs=right_kwargs,
**kwargs)
# Various Lines
def line(self, p1, p2, **kwargs):
ax = self.get_axes()
lines.line(ax, p1, p2, **kwargs)
def horizontal_line(self, i, **kwargs):
ax = self.get_axes()
scale = self.get_scale()
lines.horizontal_line(ax, scale, i, **kwargs)
def left_parallel_line(self, i, **kwargs):
ax = self.get_axes()
scale = self.get_scale()
lines.left_parallel_line(ax, scale, i, **kwargs)
def right_parallel_line(self, i, **kwargs):
ax = self.get_axes()
scale = self.get_scale()
lines.right_parallel_line(ax, scale, i, **kwargs)
# Matplotlib passthroughs
def close(self):
fig = self.get_figure()
plt.close(fig)
def legend(self, *args, **kwargs):
ax = self.get_axes()
return ax.legend(*args, **kwargs)
def savefig(self, filename, **kwargs):
self._redraw_labels()
fig = self.get_figure()
if 'dpi' not in kwargs:
kwargs['dpi'] = 200
fig.savefig(filename, **kwargs)
def show(self):
self._redraw_labels()
plt.show()
# Axis ticks
def clear_matplotlib_ticks(self, axis="both"):
"""Clears the default matplotlib ticks."""
ax = self.get_axes()
plotting.clear_matplotlib_ticks(ax=ax, axis=axis)
def get_ticks_from_axis_limits(self, multiple=1):
"""
Taking self._axis_limits and self._boundary_scale get the scaled
ticks for all three axes and store them in self._ticks under the
keys 'b' for bottom, 'l' for left and 'r' for right axes.
"""
for k in ['b', 'l', 'r']:
self._ticks[k] = np.linspace(
self._axis_limits[k][0],
self._axis_limits[k][1],
int(self._boundary_scale / float(multiple) + 1)
).tolist()
def set_custom_ticks(self, locations=None, clockwise=False, multiple=1,
axes_colors=None, tick_formats=None, **kwargs):
"""
Having called get_ticks_from_axis_limits, set the custom ticks on the
plot.
"""
for k in ['b', 'l', 'r']:
self.ticks(ticks=self._ticks[k], locations=locations,
axis=k, clockwise=clockwise, multiple=multiple,
axes_colors=axes_colors, tick_formats=tick_formats,
**kwargs)
def ticks(self, ticks=None, locations=None, multiple=1, axis='blr',
clockwise=False, axes_colors=None, tick_formats=None, **kwargs):
ax = self.get_axes()
scale = self.get_scale()
lines.ticks(ax, scale, ticks=ticks, locations=locations,
multiple=multiple, clockwise=clockwise, axis=axis,
axes_colors=axes_colors, tick_formats=tick_formats,
**kwargs)
# Redrawing and resizing
def resize_drawing_canvas(self, scale=None):
ax = self.get_axes()
if not scale:
scale = self.get_scale()
plotting.resize_drawing_canvas(ax, scale=scale)
def _redraw_labels(self):
"""Redraw axis labels, typically after draw or resize events."""
ax = self.get_axes()
# Remove any previous labels
for mpl_object in self._to_remove:
mpl_object.remove()
self._to_remove = []
# Redraw the labels with the appropriate angles
label_data = list(self._labels.values())
label_data.extend(self._corner_labels.values())
for (label, position, rotation, kwargs) in label_data:
transform = ax.transAxes
x, y = project_point(position)
# Calculate the new angle.
position = np.array([x, y])
new_rotation = ax.transData.transform_angles(
np.array((rotation,)), position.reshape((1, 2)))[0]
text = ax.text(x, y, label, rotation=new_rotation,
transform=transform, horizontalalignment="center",
**kwargs)
text.set_rotation_mode("anchor")
self._to_remove.append(text)
def convert_coordinates(self, points, axisorder='blr'):
"""
Convert data coordinates to simplex coordinates for plotting
in the case that axis limits have been applied.
"""
return convert_coordinates_sequence(points,self._boundary_scale,
self._axis_limits, axisorder)
# Various Plots
def scatter(self, points, **kwargs):
ax = self.get_axes()
permutation = self._permutation
plot_ = plotting.scatter(points, ax=ax, permutation=permutation,
**kwargs)
return plot_
def plot(self, points, **kwargs):
ax = self.get_axes()
permutation = self._permutation
return plotting.plot(points, ax=ax, permutation=permutation,
**kwargs)
def plot_colored_trajectory(self, points, cmap=None, **kwargs):
ax = self.get_axes()
permutation = self._permutation
return plotting.plot_colored_trajectory(points, cmap=cmap, ax=ax,
permutation=permutation, **kwargs)
def heatmap(self, data, scale=None, cmap=None, scientific=False,
style='triangular', colorbar=True, use_rgba=False,
vmin=None, vmax=None, cbarlabel=None, cb_kwargs=None):
permutation = self._permutation
if not scale:
scale = self.get_scale()
if style.lower()[0] == 'd':
self._boundary_scale = scale + 1
ax = self.get_axes()
heatmapping.heatmap(data, scale, cmap=cmap, style=style, ax=ax,
scientific=scientific, colorbar=colorbar,
permutation=permutation, use_rgba=use_rgba,
vmin=vmin, vmax=vmax, cbarlabel=cbarlabel,
cb_kwargs=cb_kwargs)
def heatmapf(self, func, scale=None, cmap=None, boundary=True,
style='triangular', colorbar=True, scientific=False,
vmin=None, vmax=None, cbarlabel=None, cb_kwargs=None):
if not scale:
scale = self.get_scale()
if style.lower()[0] == 'd':
self._boundary_scale = scale + 1
permutation = self._permutation
ax = self.get_axes()
heatmapping.heatmapf(func, scale, cmap=cmap, style=style,
boundary=boundary, ax=ax, scientific=scientific,
colorbar=colorbar, permutation=permutation,
vmin=vmin, vmax=vmax, cbarlabel=cbarlabel,
cb_kwargs=cb_kwargs)
def set_background_color(self, color="whitesmoke", zorder=-1000, alpha=0.75):
self._background_parameters = BackgroundParameters(color=color, alpha=alpha, zorder=zorder)
self._draw_background()
def _draw_background(self):
color, alpha, zorder = self._background_parameters
scale = self.get_scale()
ax = self.get_axes()
# Remove any existing background
if self._background_triangle:
self._background_triangle.remove()
# Draw the background
self._background_triangle = heatmapping.background_color(ax, color, scale, alpha=alpha, zorder=zorder)[0]