Skip to content

Commit f7c68b6

Browse files
committed
Merge pull request #3425 from thisch/pep8p1
Pep8ify examples
2 parents a44673b + 43c4c50 commit f7c68b6

Some content is hidden

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

75 files changed

+795
-739
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ script:
3434
# Generate the font caches in a single process before starting the
3535
# multiple processes
3636
- python -c "from matplotlib import font_manager"
37+
- if [[ $BUILD_DOCS == false ]]; then export MPL_REPO_DIR=$PWD; fi # pep8-conformance test of the examples
3738
- if [[ $BUILD_DOCS == false ]]; then mkdir ../tmp_test_dir; fi
3839
- if [[ $BUILD_DOCS == false ]]; then cd ../tmp_test_dir; fi
3940
- if [[ $BUILD_DOCS == false ]]; then python ../matplotlib/tests.py -sv --processes=8 --process-timeout=300 $TEST_ARGS; fi

examples/animation/animate_decay.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
import matplotlib.pyplot as plt
33
import matplotlib.animation as animation
44

5+
56
def data_gen():
67
t = data_gen.t
78
cnt = 0
89
while cnt < 1000:
9-
cnt+=1
10+
cnt += 1
1011
t += 0.05
1112
yield t, np.sin(2*np.pi*t) * np.exp(-t/10.)
1213
data_gen.t = 0
@@ -17,9 +18,11 @@ def data_gen():
1718
ax.set_xlim(0, 5)
1819
ax.grid()
1920
xdata, ydata = [], []
21+
22+
2023
def run(data):
2124
# update the data
22-
t,y = data
25+
t, y = data
2326
xdata.append(t)
2427
ydata.append(y)
2528
xmin, xmax = ax.get_xlim()
@@ -32,5 +35,5 @@ def run(data):
3235
return line,
3336

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

examples/animation/basic_example.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
import matplotlib.pyplot as plt
33
import matplotlib.animation as animation
44

5+
56
def update_line(num, data, line):
6-
line.set_data(data[...,:num])
7+
line.set_data(data[..., :num])
78
return line,
89

910
fig1 = plt.figure()
@@ -15,7 +16,7 @@ def update_line(num, data, line):
1516
plt.xlabel('x')
1617
plt.title('test')
1718
line_ani = animation.FuncAnimation(fig1, update_line, 25, fargs=(data, l),
18-
interval=50, blit=True)
19+
interval=50, blit=True)
1920
#line_ani.save('lines.mp4')
2021

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

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

3435
plt.show()

examples/animation/basic_example_writer.py

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

10+
1011
def update_line(num, data, line):
11-
line.set_data(data[...,:num])
12+
line.set_data(data[..., :num])
1213
return line,
1314

1415
# Set up formatting for the movie files
@@ -25,7 +26,7 @@ def update_line(num, data, line):
2526
plt.xlabel('x')
2627
plt.title('test')
2728
line_ani = animation.FuncAnimation(fig1, update_line, 25, fargs=(data, l),
28-
interval=50, blit=True)
29+
interval=50, blit=True)
2930
line_ani.save('lines.mp4', writer=writer)
3031

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

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

examples/animation/bayes_update.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import scipy.stats as ss
55
from matplotlib.animation import FuncAnimation
66

7+
78
class UpdateDist(object):
89
def __init__(self, ax, prob=0.5):
910
self.success = 0
@@ -43,5 +44,5 @@ def __call__(self, i):
4344
ax = fig.add_subplot(1, 1, 1)
4445
ud = UpdateDist(ax, prob=0.7)
4546
anim = FuncAnimation(fig, ud, frames=np.arange(100), init_func=ud.init,
46-
interval=100, blit=True)
47+
interval=100, blit=True)
4748
plt.show()

examples/animation/double_pendulum_animated.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
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):
@@ -22,7 +22,8 @@ def derivs(state, t):
2222
del_ = state[2]-state[0]
2323
den1 = (M1+M2)*L1 - M2*L1*cos(del_)*cos(del_)
2424
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_)
25+
+ M2*G*sin(state[2])*cos(del_)
26+
+ M2*L2*state[3]*state[3]*sin(del_)
2627
- (M1+M2)*G*sin(state[0]))/den1
2728

2829
dydx[2] = state[3]
@@ -46,19 +47,17 @@ def derivs(state, t):
4647
th2 = -10.0
4748
w2 = 0.0
4849

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

5453
# integrate your ODE using scipy.integrate.
5554
y = integrate.odeint(derivs, state, t)
5655

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

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

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

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

76+
7677
def animate(i):
7778
thisx = [0, x1[i], x2[i]]
7879
thisy = [0, y1[i], y2[i]]
7980

8081
line.set_data(thisx, thisy)
81-
time_text.set_text(time_template%(i*dt))
82+
time_text.set_text(time_template % (i*dt))
8283
return line, time_text
8384

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

8788
#ani.save('double_pendulum.mp4', fps=15)
8889
plt.show()

examples/animation/dynamic_image.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
fig = plt.figure()
1010

11+
1112
def f(x, y):
1213
return np.sin(x) + np.cos(y)
1314

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

1718
im = plt.imshow(f(x, y), cmap=plt.get_cmap('jet'))
1819

20+
1921
def updatefig(*args):
20-
global x,y
22+
global x, y
2123
x += np.pi / 15.
2224
y += np.pi / 20.
23-
im.set_array(f(x,y))
25+
im.set_array(f(x, y))
2426
return im,
2527

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

examples/animation/dynamic_image2.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
fig = plt.figure()
1010

11+
1112
def f(x, y):
1213
return np.sin(x) + np.cos(y)
1314

@@ -24,7 +25,7 @@ def f(x, y):
2425
ims.append([im])
2526

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

2930
#ani.save('dynamic_images.mp4')
3031

examples/animation/histogram.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,34 +28,36 @@
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(barpath, facecolor='green', edgecolor='yellow', alpha=0.5)
46+
patch = patches.PathPatch(
47+
barpath, facecolor='green', edgecolor='yellow', alpha=0.5)
4748
ax.add_patch(patch)
4849

4950
ax.set_xlim(left[0], right[-1])
5051
ax.set_ylim(bottom.min(), top.max())
5152

53+
5254
def animate(i):
5355
# simulate new data coming in
5456
data = np.random.randn(1000)
5557
n, bins = np.histogram(data, 100)
5658
top = bottom + n
57-
verts[1::5,1] = top
58-
verts[2::5,1] = top
59+
verts[1::5, 1] = top
60+
verts[2::5, 1] = top
5961

6062
ani = animation.FuncAnimation(fig, animate, 100, repeat=False)
6163
plt.show()

examples/animation/moviewriter.py

Lines changed: 2 additions & 3 deletions
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,12 +21,11 @@
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-

examples/animation/rain.py

Lines changed: 8 additions & 8 deletions
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

@@ -42,7 +42,7 @@ def update(frame_number):
4242

4343
# Make all colors more transparent as time progresses.
4444
rain_drops['color'][:, 3] -= 1.0/len(rain_drops)
45-
rain_drops['color'][:,3] = np.clip(rain_drops['color'][:,3], 0, 1)
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.

examples/animation/random_data.py

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

9+
910
def update(data):
1011
line.set_ydata(data)
1112
return line,
1213

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

1619
ani = animation.FuncAnimation(fig, update, data_gen, interval=100)
1720
plt.show()

0 commit comments

Comments
 (0)