Skip to content

Commit ff42dcb

Browse files
committed
Merge pull request matplotlib#3200 from matplotlib/revert-3182-pep8examplesapiv2
Revert "pep8ify more examples in examples/ + use np.radians/np.degrees"
2 parents f896381 + 6bda3c1 commit ff42dcb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+478
-501
lines changed

Diff for: examples/animation/animate_decay.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
import matplotlib.pyplot as plt
33
import matplotlib.animation as animation
44

5-
65
def data_gen():
76
t = data_gen.t
87
cnt = 0
98
while cnt < 1000:
10-
cnt += 1
9+
cnt+=1
1110
t += 0.05
12-
yield t, np.sin(2 * np.pi * t) * np.exp(-t / 10.)
11+
yield t, np.sin(2*np.pi*t) * np.exp(-t/10.)
1312
data_gen.t = 0
1413

1514
fig, ax = plt.subplots()
@@ -18,22 +17,20 @@ def data_gen():
1817
ax.set_xlim(0, 5)
1918
ax.grid()
2019
xdata, ydata = [], []
21-
22-
2320
def run(data):
2421
# update the data
25-
t, y = data
22+
t,y = data
2623
xdata.append(t)
2724
ydata.append(y)
2825
xmin, xmax = ax.get_xlim()
2926

3027
if t >= xmax:
31-
ax.set_xlim(xmin, 2 * xmax)
28+
ax.set_xlim(xmin, 2*xmax)
3229
ax.figure.canvas.draw()
3330
line.set_data(xdata, ydata)
3431

3532
return line,
3633

3734
ani = animation.FuncAnimation(fig, run, data_gen, blit=True, interval=10,
38-
repeat=False)
35+
repeat=False)
3936
plt.show()

Diff for: examples/animation/basic_example.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
import matplotlib.pyplot as plt
33
import matplotlib.animation as animation
44

5-
65
def update_line(num, data, line):
7-
line.set_data(data[..., :num])
6+
line.set_data(data[...,:num])
87
return line,
98

109
fig1 = plt.figure()
@@ -16,8 +15,8 @@ def update_line(num, data, line):
1615
plt.xlabel('x')
1716
plt.title('test')
1817
line_ani = animation.FuncAnimation(fig1, update_line, 25, fargs=(data, l),
19-
interval=50, blit=True)
20-
# line_ani.save('lines.mp4')
18+
interval=50, blit=True)
19+
#line_ani.save('lines.mp4')
2120

2221
fig2 = plt.figure()
2322

@@ -29,7 +28,7 @@ def update_line(num, data, line):
2928
ims.append((plt.pcolor(x, y, base + add, norm=plt.Normalize(0, 30)),))
3029

3130
im_ani = animation.ArtistAnimation(fig2, ims, interval=50, repeat_delay=3000,
32-
blit=True)
31+
blit=True)
3332
#im_ani.save('im.mp4', metadata={'artist':'Guido'})
3433

3534
plt.show()

Diff for: examples/animation/basic_example_writer.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
import matplotlib.pyplot as plt
88
import matplotlib.animation as animation
99

10-
1110
def update_line(num, data, line):
12-
line.set_data(data[..., :num])
11+
line.set_data(data[...,:num])
1312
return line,
1413

1514
# Set up formatting for the movie files
@@ -26,7 +25,7 @@ def update_line(num, data, line):
2625
plt.xlabel('x')
2726
plt.title('test')
2827
line_ani = animation.FuncAnimation(fig1, update_line, 25, fargs=(data, l),
29-
interval=50, blit=True)
28+
interval=50, blit=True)
3029
line_ani.save('lines.mp4', writer=writer)
3130

3231
fig2 = plt.figure()
@@ -39,5 +38,5 @@ def update_line(num, data, line):
3938
ims.append((plt.pcolor(x, y, base + add, norm=plt.Normalize(0, 30)),))
4039

4140
im_ani = animation.ArtistAnimation(fig2, ims, interval=50, repeat_delay=3000,
42-
blit=True)
41+
blit=True)
4342
im_ani.save('im.mp4', writer=writer)

Diff for: examples/animation/bayes_update.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
import scipy.stats as ss
55
from matplotlib.animation import FuncAnimation
66

7-
87
class UpdateDist(object):
9-
108
def __init__(self, ax, prob=0.5):
119
self.success = 0
1210
self.prob = prob
@@ -45,5 +43,5 @@ def __call__(self, i):
4543
ax = fig.add_subplot(1, 1, 1)
4644
ud = UpdateDist(ax, prob=0.7)
4745
anim = FuncAnimation(fig, ud, frames=np.arange(100), init_func=ud.init,
48-
interval=100, blit=True)
46+
interval=100, blit=True)
4947
plt.show()

Diff for: examples/animation/double_pendulum_animated.py

+24-25
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,31 @@
77
import scipy.integrate as integrate
88
import matplotlib.animation as animation
99

10-
G = 9.8 # acceleration due to gravity, in m/s^2
11-
L1 = 1.0 # length of pendulum 1 in m
12-
L2 = 1.0 # length of pendulum 2 in m
13-
M1 = 1.0 # mass of pendulum 1 in kg
14-
M2 = 1.0 # mass of pendulum 2 in kg
10+
G = 9.8 # acceleration due to gravity, in m/s^2
11+
L1 = 1.0 # length of pendulum 1 in m
12+
L2 = 1.0 # length of pendulum 2 in m
13+
M1 = 1.0 # mass of pendulum 1 in kg
14+
M2 = 1.0 # mass of pendulum 2 in kg
1515

1616

1717
def derivs(state, t):
1818

1919
dydx = np.zeros_like(state)
2020
dydx[0] = state[1]
2121

22-
del_ = state[2] - state[0]
23-
den1 = (M1 + M2) * L1 - M2 * L1 * cos(del_) * cos(del_)
24-
dydx[1] = (M2 * L1 * state[1] * state[1] * sin(del_) * cos(del_)
25-
+ M2 * G * sin(state[2]) * cos(del_) + M2 *
26-
L2 * state[3] * state[3] * sin(del_)
27-
- (M1 + M2) * G * sin(state[0])) / den1
22+
del_ = state[2]-state[0]
23+
den1 = (M1+M2)*L1 - M2*L1*cos(del_)*cos(del_)
24+
dydx[1] = (M2*L1*state[1]*state[1]*sin(del_)*cos(del_)
25+
+ M2*G*sin(state[2])*cos(del_) + M2*L2*state[3]*state[3]*sin(del_)
26+
- (M1+M2)*G*sin(state[0]))/den1
2827

2928
dydx[2] = state[3]
3029

31-
den2 = (L2 / L1) * den1
32-
dydx[3] = (-M2 * L2 * state[3] * state[3] * sin(del_) * cos(del_)
33-
+ (M1 + M2) * G * sin(state[0]) * cos(del_)
34-
- (M1 + M2) * L1 * state[1] * state[1] * sin(del_)
35-
- (M1 + M2) * G * sin(state[2])) / den2
30+
den2 = (L2/L1)*den1
31+
dydx[3] = (-M2*L2*state[3]*state[3]*sin(del_)*cos(del_)
32+
+ (M1+M2)*G*sin(state[0])*cos(del_)
33+
- (M1+M2)*L1*state[1]*state[1]*sin(del_)
34+
- (M1+M2)*G*sin(state[2]))/den2
3635

3736
return dydx
3837

@@ -47,17 +46,19 @@ def derivs(state, t):
4746
th2 = -10.0
4847
w2 = 0.0
4948

49+
rad = pi/180
50+
5051
# initial state
51-
state = np.radians([th1, w1, th2, w2])
52+
state = np.array([th1, w1, th2, w2])*pi/180.
5253

5354
# integrate your ODE using scipy.integrate.
5455
y = integrate.odeint(derivs, state, t)
5556

56-
x1 = L1 * sin(y[:, 0])
57-
y1 = -L1 * cos(y[:, 0])
57+
x1 = L1*sin(y[:,0])
58+
y1 = -L1*cos(y[:,0])
5859

59-
x2 = L2 * sin(y[:, 2]) + x1
60-
y2 = -L2 * cos(y[:, 2]) + y1
60+
x2 = L2*sin(y[:,2]) + x1
61+
y2 = -L2*cos(y[:,2]) + y1
6162

6263
fig = plt.figure()
6364
ax = fig.add_subplot(111, autoscale_on=False, xlim=(-2, 2), ylim=(-2, 2))
@@ -67,23 +68,21 @@ def derivs(state, t):
6768
time_template = 'time = %.1fs'
6869
time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes)
6970

70-
7171
def init():
7272
line.set_data([], [])
7373
time_text.set_text('')
7474
return line, time_text
7575

76-
7776
def animate(i):
7877
thisx = [0, x1[i], x2[i]]
7978
thisy = [0, y1[i], y2[i]]
8079

8180
line.set_data(thisx, thisy)
82-
time_text.set_text(time_template % (i * dt))
81+
time_text.set_text(time_template%(i*dt))
8382
return line, time_text
8483

8584
ani = animation.FuncAnimation(fig, animate, np.arange(1, len(y)),
86-
interval=25, blit=True, init_func=init)
85+
interval=25, blit=True, init_func=init)
8786

8887
#ani.save('double_pendulum.mp4', fps=15)
8988
plt.show()

Diff for: examples/animation/dynamic_image.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
fig = plt.figure()
1010

11-
1211
def f(x, y):
1312
return np.sin(x) + np.cos(y)
1413

@@ -17,12 +16,11 @@ def f(x, y):
1716

1817
im = plt.imshow(f(x, y), cmap=plt.get_cmap('jet'))
1918

20-
2119
def updatefig(*args):
22-
global x, y
20+
global x,y
2321
x += np.pi / 15.
2422
y += np.pi / 20.
25-
im.set_array(f(x, y))
23+
im.set_array(f(x,y))
2624
return im,
2725

2826
ani = animation.FuncAnimation(fig, updatefig, interval=50, blit=True)

Diff for: examples/animation/dynamic_image2.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
fig = plt.figure()
1010

11-
1211
def f(x, y):
1312
return np.sin(x) + np.cos(y)
1413

@@ -25,9 +24,9 @@ def f(x, y):
2524
ims.append([im])
2625

2726
ani = animation.ArtistAnimation(fig, ims, interval=50, blit=True,
28-
repeat_delay=1000)
27+
repeat_delay=1000)
2928

30-
# ani.save('dynamic_images.mp4')
29+
#ani.save('dynamic_images.mp4')
3130

3231

3332
plt.show()

Diff for: examples/animation/histogram.py

+12-14
Original file line numberDiff line numberDiff line change
@@ -28,36 +28,34 @@
2828
# for each rect: 1 for the MOVETO, 3 for the LINETO, 1 for the
2929
# CLOSEPOLY; the vert for the closepoly is ignored but we still need
3030
# it to keep the codes aligned with the vertices
31-
nverts = nrects * (1 + 3 + 1)
31+
nverts = nrects*(1+3+1)
3232
verts = np.zeros((nverts, 2))
3333
codes = np.ones(nverts, int) * path.Path.LINETO
3434
codes[0::5] = path.Path.MOVETO
3535
codes[4::5] = path.Path.CLOSEPOLY
36-
verts[0::5, 0] = left
37-
verts[0::5, 1] = bottom
38-
verts[1::5, 0] = left
39-
verts[1::5, 1] = top
40-
verts[2::5, 0] = right
41-
verts[2::5, 1] = top
42-
verts[3::5, 0] = right
43-
verts[3::5, 1] = bottom
36+
verts[0::5,0] = left
37+
verts[0::5,1] = bottom
38+
verts[1::5,0] = left
39+
verts[1::5,1] = top
40+
verts[2::5,0] = right
41+
verts[2::5,1] = top
42+
verts[3::5,0] = right
43+
verts[3::5,1] = bottom
4444

4545
barpath = path.Path(verts, codes)
46-
patch = patches.PathPatch(
47-
barpath, facecolor='green', edgecolor='yellow', alpha=0.5)
46+
patch = patches.PathPatch(barpath, facecolor='green', edgecolor='yellow', alpha=0.5)
4847
ax.add_patch(patch)
4948

5049
ax.set_xlim(left[0], right[-1])
5150
ax.set_ylim(bottom.min(), top.max())
5251

53-
5452
def animate(i):
5553
# simulate new data coming in
5654
data = np.random.randn(1000)
5755
n, bins = np.histogram(data, 100)
5856
top = bottom + n
59-
verts[1::5, 1] = top
60-
verts[2::5, 1] = top
57+
verts[1::5,1] = top
58+
verts[2::5,1] = top
6159

6260
ani = animation.FuncAnimation(fig, animate, 100, repeat=False)
6361
plt.show()

Diff for: examples/animation/moviewriter.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
FFMpegWriter = manimation.writers['ffmpeg']
1414
metadata = dict(title='Movie Test', artist='Matplotlib',
15-
comment='Movie support!')
15+
comment='Movie support!')
1616
writer = FFMpegWriter(fps=15, metadata=metadata)
1717

1818
fig = plt.figure()
@@ -21,11 +21,12 @@
2121
plt.xlim(-5, 5)
2222
plt.ylim(-5, 5)
2323

24-
x0, y0 = 0, 0
24+
x0,y0 = 0, 0
2525

2626
with writer.saving(fig, "writer_test.mp4", 100):
2727
for i in range(100):
2828
x0 += 0.1 * np.random.randn()
2929
y0 += 0.1 * np.random.randn()
3030
l.set_data(x0, y0)
3131
writer.grab_frame()
32+

Diff for: examples/animation/rain.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
"""
99
import numpy as np
1010
import matplotlib.pyplot as plt
11-
from matplotlib.animation import FuncAnimation
11+
from matplotlib.animation import FuncAnimation
1212

1313

14-
# Create new Figure and an Axes which fills it.
15-
fig = plt.figure(figsize=(7, 7))
14+
# Create new Figure and an Axes which fills it.
15+
fig = plt.figure(figsize=(7,7))
1616
ax = fig.add_axes([0, 0, 1, 1], frameon=False)
17-
ax.set_xlim(0, 1), ax.set_xticks([])
18-
ax.set_ylim(0, 1), ax.set_yticks([])
17+
ax.set_xlim(0,1), ax.set_xticks([])
18+
ax.set_ylim(0,1), ax.set_yticks([])
1919

2020
# Create rain data
2121
n_drops = 50
@@ -31,7 +31,7 @@
3131

3232
# Construct the scatter which we will update during animation
3333
# as the raindrops develop.
34-
scat = ax.scatter(rain_drops['position'][:, 0], rain_drops['position'][:, 1],
34+
scat = ax.scatter(rain_drops['position'][:,0], rain_drops['position'][:,1],
3535
s=rain_drops['size'], lw=0.5, edgecolors=rain_drops['color'],
3636
facecolors='none')
3737

@@ -41,8 +41,8 @@ def update(frame_number):
4141
current_index = frame_number % n_drops
4242

4343
# Make all colors more transparent as time progresses.
44-
rain_drops['color'][:, 3] -= 1.0 / len(rain_drops)
45-
rain_drops['color'][:, 3] = np.clip(rain_drops['color'][:, 3], 0, 1)
44+
rain_drops['color'][:, 3] -= 1.0/len(rain_drops)
45+
rain_drops['color'][:,3] = np.clip(rain_drops['color'][:,3], 0, 1)
4646

4747
# Make all circles bigger.
4848
rain_drops['size'] += rain_drops['growth']
@@ -58,7 +58,7 @@ def update(frame_number):
5858
scat.set_edgecolors(rain_drops['color'])
5959
scat.set_sizes(rain_drops['size'])
6060
scat.set_offsets(rain_drops['position'])
61-
61+
6262

6363
# Construct the animation, using the update function as the animation
6464
# director.

Diff for: examples/animation/random_data.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@
66
line, = ax.plot(np.random.rand(10))
77
ax.set_ylim(0, 1)
88

9-
109
def update(data):
1110
line.set_ydata(data)
1211
return line,
1312

14-
1513
def data_gen():
16-
while True:
17-
yield np.random.rand(10)
14+
while True: yield np.random.rand(10)
1815

1916
ani = animation.FuncAnimation(fig, update, data_gen, interval=100)
2017
plt.show()

0 commit comments

Comments
 (0)