Skip to content

Commit 08c1cd4

Browse files
author
p12
committed
images: Add a script for generation of 2D color plot of atan2() function
1 parent f8f73f9 commit 08c1cd4

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

images/math-atan2.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
import matplotlib.cm as cm
5+
import matplotlib.colors as colors
6+
import matplotlib.pyplot as plt
7+
8+
import os
9+
import numpy as np
10+
import math
11+
import pprint
12+
13+
font = {'family' : 'DejaVu Serif',
14+
'weight' : 'normal',
15+
'size' : 18}
16+
17+
plt.rc('font', **font)
18+
plt.rc('contour', **{ 'negative_linestyle' : 'solid'})
19+
20+
# generate data
21+
X = np.arange(-1, 1.05, 0.02)
22+
Y = np.arange(-1, 1.05, 0.02)
23+
X, Y = np.meshgrid(X, Y)
24+
Z = np.arctan2(Y,X)
25+
26+
# generate labels
27+
levels = [-math.pi*3/4, -math.pi*2/4, -math.pi/4,
28+
0, math.pi/4, math.pi/2, math.pi*3/4 ]
29+
labels = [u'-3/4 π', u'-1/2 π', u'-1/4 π', '0', u'1/4 π', u'1/2 π', u'3/4 π' ]
30+
31+
fmt = {}
32+
for l,s in zip( levels, labels ):
33+
fmt[l] = s
34+
35+
# generate plot
36+
fig = plt.figure(figsize=(475.0/72.0,400.0/72.0))
37+
ax = fig.add_subplot(111)
38+
39+
ct = ax.contour(X, Y, Z, levels=levels, linewidths=0.5, colors='k')
40+
ax.clabel(ct, fmt=fmt, colors = '#000000', fontsize=14)
41+
42+
# HACK - remove the contour label at discontinuity of atan2
43+
ct.labelTexts[5].set_text('')
44+
45+
pc = ax.pcolor(X, Y, Z, cmap=plt.get_cmap("Spectral_r"), vmin=-3.2, vmax=3.2)
46+
fig.colorbar(pc)
47+
48+
ax.set_xlim((-1.0, 1.0))
49+
ax.set_ylim((-1.0, 1.0))
50+
ax.set_axisbelow(True)
51+
ax.tick_params(pad = 15)
52+
ax.set_aspect(1.0)
53+
54+
fig.savefig('output/math-atan2.png')
55+
56+
plt.close()

0 commit comments

Comments
 (0)