-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmpl_utils.py
More file actions
49 lines (44 loc) · 1.45 KB
/
Copy pathmpl_utils.py
File metadata and controls
49 lines (44 loc) · 1.45 KB
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
import numpy as np
import matplotlib.pyplot as plt
def gcd(a, b):
while b:
a, b = b, a % b
return a
def multiple_formatter(denominator=2, number=np.pi, latex='\pi'):
def _multiple_formatter(x, pos):
den = denominator
num = int(np.rint(den*x/number))
com = gcd(num,den)
(num,den) = (int(num/com),int(den/com))
if den==1:
if num==0:
return r'$0$'
if num==1:
return r'$%s$'%latex
elif num==-1:
return r'$-%s$'%latex
else:
return r'$%s%s$'%(num,latex)
else:
if num==1:
return r'$\frac{%s}{%s}$'%(latex,den)
elif num==-1:
return r'$-\frac{%s}{%s}$'%(latex,den)
elif num < 0:
return r'$-\frac{%s%s}{%s}$'%(-num,latex,den)
else:
return r'$\frac{%s%s}{%s}$'%(num,latex,den)
return _multiple_formatter
if __name__ == "__main__":
x = np.linspace(-np.pi, 3 * np.pi, 500)
plt.plot(x, np.cos(x))
plt.title(r'Multiples of $\pi$')
ax = plt.gca()
ax.grid(True)
ax.set_aspect(1.0)
ax.axhline(0, color='black', lw=2)
ax.axvline(0, color='black', lw=2)
ax.xaxis.set_major_locator(plt.MultipleLocator(np.pi / 2))
ax.xaxis.set_minor_locator(plt.MultipleLocator(np.pi / 12))
ax.xaxis.set_major_formatter(plt.FuncFormatter(multiple_formatter()))
plt.show()