-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_numbering.py
72 lines (60 loc) · 2.08 KB
/
add_numbering.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
import string
def add_numbering(ax, i=0, loc=(0.8, 0.8), label='', style='APS', numbering='abc', **kwargs):
"""
Add numbering (a,b,c,...) to axis.
Parameters
----------
ax : matplotlib.pyplot.axis object
i : int
The axis index, e.g., i=1 -> (a)
loc : tuple or list
Position of label, relative to axis
[x, y] where 0 <= x,y <= 1
(values outside this limit are allowed but may result in labels outside axis)
label : string
Override with custom label
style : string
If 'APS' or 'Nature' will use preset styles
Otherwise if string matches format r'{i}' will use that as template string
Otherwise style overwrites the label
numbering : string
Which type of numbering
'abc' -> a, b, c
'ABC' -> A, B, C
'123' -> 1, 2, 3
'roman' -> i, ii, iii
'ROMAN' -> I, II, III
**kwargs : string keyword arguments
e.g. {'color', 'red'}
Returns
-------
ax : matplotlib.pyplot.axis object
"""
if not label:
if i<0 or i>25:
raise ValueError("i must be between 0 and 25 \
(support for i>25 will come in future version)")
roman_nums = ['i', 'ii', 'iii', 'iv', 'v', 'vi', 'vii', 'viii', 'ix', 'x',\
'xi', 'xii', 'xiii', 'xiv', 'xv', 'xvi', 'xvii', 'xviii', \
'xix', 'xx', 'xxi', 'xxii', 'xxiii', 'xxiv', 'xxv', 'xxvi']
# different formats:
if numbering == 'abc': # a, b, c
label = string.ascii_lowercase[i]
elif numbering == 'ABC': # A, B, C
label = string.ascii_uppercase[i]
elif numbering == '123': # 1, 2, 3
label = i
elif numbering == 'roman': # i, ii, iii, ...
label = r'${}$'.format(roman_nums[i])
elif numbering == 'ROMAN': # I, II, III, ...
label = r'{}'.format(roman_nums[i].upper())
else:
raise ValueError("numbering option not a recognized value")
if style == 'APS':
label = r'({})'.format(label)
elif style == 'Nature':
label = r'\textbf{{{i}}}'.format(i=label)
else:
label = style.format(i=label)
ax.text(loc[0], loc[1], label, transform=ax.transAxes, **kwargs)
return ax