-
Notifications
You must be signed in to change notification settings - Fork 20
/
curlyBrace.py
534 lines (327 loc) · 12 KB
/
curlyBrace.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
# -*- coding: utf-8 -*-
'''
Module Name : curlyBrace
Author : 高斯羽 博士 (Dr. GAO, Siyu)
Version : 1.0.2
Last Modified : 2019-04-22
This module is basically an Python implementation of the function written Pål Næverlid Sævik
for MATLAB (link in Reference).
The function "curlyBrace" allows you to plot an optionally annotated curly bracket between
two points when using matplotlib.
The usual settings for line and fonts in matplotlib also apply.
The function takes the axes scales into account automatically. But when the axes aspect is
set to "equal", the auto switch should be turned off.
Change Log
----------------------
* **Notable changes:**
+ Version : 1.0.2
- Added considerations for different scaled axes and log scale
+ Version : 1.0.1
- First version.
Reference
----------------------
https://uk.mathworks.com/matlabcentral/fileexchange/38716-curly-brace-annotation
List of functions
----------------------
* getAxSize_
* curlyBrace_
'''
import matplotlib.pyplot as plt
import numpy as np
def getAxSize(fig, ax):
'''
.. _getAxSize :
Get the axes size in pixels.
Parameters
----------
fig : matplotlib figure object
The of the target axes.
ax : matplotlib axes object
The target axes.
Returns
-------
ax_width : float
The axes width in pixels.
ax_height : float
The axes height in pixels.
Reference
-----------
https://stackoverflow.com/questions/19306510/determine-matplotlib-axis-size-in-pixels
'''
bbox = ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
ax_width, ax_height = bbox.width, bbox.height
ax_width *= fig.dpi
ax_height *= fig.dpi
return ax_width, ax_height
def curlyBrace(fig, ax, p1, p2, k_r=0.1, bool_auto=True, str_text='', int_line_num=2, fontdict={}, **kwargs):
# def curlyBrace(fig, ax, p1, p2, k_r=0.1, bool_auto=True, str_text='', int_line_num=2, fontdict={}, **kwargs):
'''
.. _curlyBrace :
Plot an optionally annotated curly bracket on the given axes of the given figure.
Note that the brackets are anti-clockwise by default. To reverse the text position, swap
"p1" and "p2".
Note that, when the axes aspect is not set to "equal", the axes coordinates need to be
transformed to screen coordinates, otherwise the arcs may not be seeable.
Parameters
----------
fig : matplotlib figure object
The of the target axes.
ax : matplotlib axes object
The target axes.
p1 : two element numeric list
The coordinates of the starting point.
p2 : two element numeric list
The coordinates of the end point.
k_r : float
This is the gain controlling how "curvy" and "pointy" (height) the bracket is.
Note that, if this gain is too big, the bracket would be very strange.
bool_auto : boolean
This is a switch controlling wether to use the auto calculation of axes
scales.
When the two axes do not have the same aspects, i.e., not "equal" scales,
this should be turned on, i.e., True.
When "equal" aspect is used, this should be turned off, i.e., False.
If you do not set this to False when setting the axes aspect to "equal",
the bracket will be in funny shape.
Default = True
str_text : string
The annotation text of the bracket. It would displayed at the mid point
of bracket with the same rotation as the bracket.
By default, it follows the anti-clockwise convention. To flip it, swap
the end point and the starting point.
The appearance of this string can be set by using "fontdict", which follows
the same syntax as the normal matplotlib syntax for font dictionary.
Default = empty string (no annotation)
int_line_num : int
This argument determines how many lines the string annotation is from the summit
of the bracket.
The distance would be affected by the font size, since it basically just a number of
lines appended to the given string.
Default = 2
fontdict : dictionary
This is font dictionary setting the string annotation. It is the same as normal
matplotlib font dictionary.
Default = empty dict
**kwargs : matplotlib line setting arguments
This allows the user to set the line arguments using named arguments that are
the same as in matplotlib.
Returns
-------
theta : float
The bracket angle in radians.
summit : list
The positions of the bracket summit.
arc1 : list of lists
arc1 positions.
arc2 : list of lists
arc2 positions.
arc3 : list of lists
arc3 positions.
arc4 : list of lists
arc4 positions.
Reference
----------
https://uk.mathworks.com/matlabcentral/fileexchange/38716-curly-brace-annotation
'''
pt1 = [None, None]
pt2 = [None, None]
ax_width, ax_height = getAxSize(fig, ax)
ax_xlim = list(ax.get_xlim())
ax_ylim = list(ax.get_ylim())
# log scale consideration
if 'log' in ax.get_xaxis().get_scale():
if p1[0] > 0.0:
pt1[0] = np.log(p1[0])
elif p1[0] < 0.0:
pt1[0] = -np.log(abs(p1[0]))
else:
pt1[0] = 0.0
if p2[0] > 0.0:
pt2[0] = np.log(p2[0])
elif p2[0] < 0.0:
pt2[0] = -np.log(abs(p2[0]))
else:
pt2[0] = 0
for i in range(0, len(ax_xlim)):
if ax_xlim[i] > 0.0:
ax_xlim[i] = np.log(ax_xlim[i])
elif ax_xlim[i] < 0.0:
ax_xlim[i] = -np.log(abs(ax_xlim[i]))
else:
ax_xlim[i] = 0.0
else:
pt1[0] = p1[0]
pt2[0] = p2[0]
if 'log' in ax.get_yaxis().get_scale():
if p1[1] > 0.0:
pt1[1] = np.log(p1[1])
elif p1[1] < 0.0:
pt1[1] = -np.log(abs(p1[1]))
else:
pt1[1] = 0.0
if p2[1] > 0.0:
pt2[1] = np.log(p2[1])
elif p2[1] < 0.0:
pt2[1] = -np.log(abs(p2[1]))
else:
pt2[1] = 0.0
for i in range(0, len(ax_ylim)):
if ax_ylim[i] > 0.0:
ax_ylim[i] = np.log(ax_ylim[i])
elif ax_ylim[i] < 0.0:
ax_ylim[i] = -np.log(abs(ax_ylim[i]))
else:
ax_ylim[i] = 0.0
else:
pt1[1] = p1[1]
pt2[1] = p2[1]
# get the ratio of pixels/length
xscale = ax_width / abs(ax_xlim[1] - ax_xlim[0])
yscale = ax_height / abs(ax_ylim[1] - ax_ylim[0])
# this is to deal with 'equal' axes aspects
if bool_auto:
pass
else:
xscale = 1.0
yscale = 1.0
# convert length to pixels,
# need to minus the lower limit to move the points back to the origin. Then add the limits back on end.
pt1[0] = (pt1[0] - ax_xlim[0]) * xscale
pt1[1] = (pt1[1] - ax_ylim[0]) * yscale
pt2[0] = (pt2[0] - ax_xlim[0]) * xscale
pt2[1] = (pt2[1] - ax_ylim[0]) * yscale
# calculate the angle
theta = np.arctan2(pt2[1] - pt1[1], pt2[0] - pt1[0])
# calculate the radius of the arcs
r = np.hypot(pt2[0] - pt1[0], pt2[1] - pt1[1]) * k_r
# arc1 centre
x11 = pt1[0] + r * np.cos(theta)
y11 = pt1[1] + r * np.sin(theta)
# arc2 centre
x22 = (pt2[0] + pt1[0]) / 2.0 - 2.0 * r * np.sin(theta) - r * np.cos(theta)
y22 = (pt2[1] + pt1[1]) / 2.0 + 2.0 * r * np.cos(theta) - r * np.sin(theta)
# arc3 centre
x33 = (pt2[0] + pt1[0]) / 2.0 - 2.0 * r * np.sin(theta) + r * np.cos(theta)
y33 = (pt2[1] + pt1[1]) / 2.0 + 2.0 * r * np.cos(theta) + r * np.sin(theta)
# arc4 centre
x44 = pt2[0] - r * np.cos(theta)
y44 = pt2[1] - r * np.sin(theta)
# prepare the rotated
q = np.linspace(theta, theta + np.pi/2.0, 50)
# reverse q
# t = np.flip(q) # this command is not supported by lower version of numpy
t = q[::-1]
# arc coordinates
arc1x = r * np.cos(t + np.pi/2.0) + x11
arc1y = r * np.sin(t + np.pi/2.0) + y11
arc2x = r * np.cos(q - np.pi/2.0) + x22
arc2y = r * np.sin(q - np.pi/2.0) + y22
arc3x = r * np.cos(q + np.pi) + x33
arc3y = r * np.sin(q + np.pi) + y33
arc4x = r * np.cos(t) + x44
arc4y = r * np.sin(t) + y44
# convert back to the axis coordinates
arc1x = arc1x / xscale + ax_xlim[0]
arc2x = arc2x / xscale + ax_xlim[0]
arc3x = arc3x / xscale + ax_xlim[0]
arc4x = arc4x / xscale + ax_xlim[0]
arc1y = arc1y / yscale + ax_ylim[0]
arc2y = arc2y / yscale + ax_ylim[0]
arc3y = arc3y / yscale + ax_ylim[0]
arc4y = arc4y / yscale + ax_ylim[0]
# log scale consideration
if 'log' in ax.get_xaxis().get_scale():
for i in range(0, len(arc1x)):
if arc1x[i] > 0.0:
arc1x[i] = np.exp(arc1x[i])
elif arc1x[i] < 0.0:
arc1x[i] = -np.exp(abs(arc1x[i]))
else:
arc1x[i] = 0.0
for i in range(0, len(arc2x)):
if arc2x[i] > 0.0:
arc2x[i] = np.exp(arc2x[i])
elif arc2x[i] < 0.0:
arc2x[i] = -np.exp(abs(arc2x[i]))
else:
arc2x[i] = 0.0
for i in range(0, len(arc3x)):
if arc3x[i] > 0.0:
arc3x[i] = np.exp(arc3x[i])
elif arc3x[i] < 0.0:
arc3x[i] = -np.exp(abs(arc3x[i]))
else:
arc3x[i] = 0.0
for i in range(0, len(arc4x)):
if arc4x[i] > 0.0:
arc4x[i] = np.exp(arc4x[i])
elif arc4x[i] < 0.0:
arc4x[i] = -np.exp(abs(arc4x[i]))
else:
arc4x[i] = 0.0
else:
pass
if 'log' in ax.get_yaxis().get_scale():
for i in range(0, len(arc1y)):
if arc1y[i] > 0.0:
arc1y[i] = np.exp(arc1y[i])
elif arc1y[i] < 0.0:
arc1y[i] = -np.exp(abs(arc1y[i]))
else:
arc1y[i] = 0.0
for i in range(0, len(arc2y)):
if arc2y[i] > 0.0:
arc2y[i] = np.exp(arc2y[i])
elif arc2y[i] < 0.0:
arc2y[i] = -np.exp(abs(arc2y[i]))
else:
arc2y[i] = 0.0
for i in range(0, len(arc3y)):
if arc3y[i] > 0.0:
arc3y[i] = np.exp(arc3y[i])
elif arc3y[i] < 0.0:
arc3y[i] = -np.exp(abs(arc3y[i]))
else:
arc3y[i] = 0.0
for i in range(0, len(arc4y)):
if arc4y[i] > 0.0:
arc4y[i] = np.exp(arc4y[i])
elif arc4y[i] < 0.0:
arc4y[i] = -np.exp(abs(arc4y[i]))
else:
arc4y[i] = 0.0
else:
pass
# plot arcs
ax.plot(arc1x, arc1y, **kwargs)
ax.plot(arc2x, arc2y, **kwargs)
ax.plot(arc3x, arc3y, **kwargs)
ax.plot(arc4x, arc4y, **kwargs)
# plot lines
ax.plot([arc1x[-1], arc2x[1]], [arc1y[-1], arc2y[1]], **kwargs)
ax.plot([arc3x[-1], arc4x[1]], [arc3y[-1], arc4y[1]], **kwargs)
summit = [arc2x[-1], arc2y[-1]]
if str_text:
int_line_num = int(int_line_num)
str_temp = '\n' * int_line_num
# convert radians to degree and within 0 to 360
ang = np.degrees(theta) % 360.0
if (ang >= 0.0) and (ang <= 90.0):
rotation = ang
str_text = str_text + str_temp
if (ang > 90.0) and (ang < 270.0):
rotation = ang + 180.0
str_text = str_temp + str_text
elif (ang >= 270.0) and (ang <= 360.0):
rotation = ang
str_text = str_text + str_temp
else:
rotation = ang
ax.axes.text(arc2x[-1], arc2y[-1], str_text, ha='center', va='center', rotation=rotation, fontdict=fontdict)
else:
pass
arc1 = [arc1x, arc1y]
arc2 = [arc2x, arc2y]
arc3 = [arc3x, arc3y]
arc4 = [arc4x, arc4y]
return theta, summit, arc1, arc2, arc3, arc4