Skip to content

Revert "pep8ify more examples in examples/ + use np.radians/np.degrees" #3200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 9, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions examples/animation/animate_decay.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
import matplotlib.pyplot as plt
import matplotlib.animation as animation


def data_gen():
t = data_gen.t
cnt = 0
while cnt < 1000:
cnt += 1
cnt+=1
t += 0.05
yield t, np.sin(2 * np.pi * t) * np.exp(-t / 10.)
yield t, np.sin(2*np.pi*t) * np.exp(-t/10.)
data_gen.t = 0

fig, ax = plt.subplots()
Expand All @@ -18,22 +17,20 @@ def data_gen():
ax.set_xlim(0, 5)
ax.grid()
xdata, ydata = [], []


def run(data):
# update the data
t, y = data
t,y = data
xdata.append(t)
ydata.append(y)
xmin, xmax = ax.get_xlim()

if t >= xmax:
ax.set_xlim(xmin, 2 * xmax)
ax.set_xlim(xmin, 2*xmax)
ax.figure.canvas.draw()
line.set_data(xdata, ydata)

return line,

ani = animation.FuncAnimation(fig, run, data_gen, blit=True, interval=10,
repeat=False)
repeat=False)
plt.show()
9 changes: 4 additions & 5 deletions examples/animation/basic_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
import matplotlib.pyplot as plt
import matplotlib.animation as animation


def update_line(num, data, line):
line.set_data(data[..., :num])
line.set_data(data[...,:num])
return line,

fig1 = plt.figure()
Expand All @@ -16,8 +15,8 @@ def update_line(num, data, line):
plt.xlabel('x')
plt.title('test')
line_ani = animation.FuncAnimation(fig1, update_line, 25, fargs=(data, l),
interval=50, blit=True)
# line_ani.save('lines.mp4')
interval=50, blit=True)
#line_ani.save('lines.mp4')

fig2 = plt.figure()

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

im_ani = animation.ArtistAnimation(fig2, ims, interval=50, repeat_delay=3000,
blit=True)
blit=True)
#im_ani.save('im.mp4', metadata={'artist':'Guido'})

plt.show()
7 changes: 3 additions & 4 deletions examples/animation/basic_example_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
import matplotlib.pyplot as plt
import matplotlib.animation as animation


def update_line(num, data, line):
line.set_data(data[..., :num])
line.set_data(data[...,:num])
return line,

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

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

im_ani = animation.ArtistAnimation(fig2, ims, interval=50, repeat_delay=3000,
blit=True)
blit=True)
im_ani.save('im.mp4', writer=writer)
4 changes: 1 addition & 3 deletions examples/animation/bayes_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import scipy.stats as ss
from matplotlib.animation import FuncAnimation


class UpdateDist(object):

def __init__(self, ax, prob=0.5):
self.success = 0
self.prob = prob
Expand Down Expand Up @@ -45,5 +43,5 @@ def __call__(self, i):
ax = fig.add_subplot(1, 1, 1)
ud = UpdateDist(ax, prob=0.7)
anim = FuncAnimation(fig, ud, frames=np.arange(100), init_func=ud.init,
interval=100, blit=True)
interval=100, blit=True)
plt.show()
49 changes: 24 additions & 25 deletions examples/animation/double_pendulum_animated.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,31 @@
import scipy.integrate as integrate
import matplotlib.animation as animation

G = 9.8 # acceleration due to gravity, in m/s^2
L1 = 1.0 # length of pendulum 1 in m
L2 = 1.0 # length of pendulum 2 in m
M1 = 1.0 # mass of pendulum 1 in kg
M2 = 1.0 # mass of pendulum 2 in kg
G = 9.8 # acceleration due to gravity, in m/s^2
L1 = 1.0 # length of pendulum 1 in m
L2 = 1.0 # length of pendulum 2 in m
M1 = 1.0 # mass of pendulum 1 in kg
M2 = 1.0 # mass of pendulum 2 in kg


def derivs(state, t):

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

del_ = state[2] - state[0]
den1 = (M1 + M2) * L1 - M2 * L1 * cos(del_) * cos(del_)
dydx[1] = (M2 * L1 * state[1] * state[1] * sin(del_) * cos(del_)
+ M2 * G * sin(state[2]) * cos(del_) + M2 *
L2 * state[3] * state[3] * sin(del_)
- (M1 + M2) * G * sin(state[0])) / den1
del_ = state[2]-state[0]
den1 = (M1+M2)*L1 - M2*L1*cos(del_)*cos(del_)
dydx[1] = (M2*L1*state[1]*state[1]*sin(del_)*cos(del_)
+ M2*G*sin(state[2])*cos(del_) + M2*L2*state[3]*state[3]*sin(del_)
- (M1+M2)*G*sin(state[0]))/den1

dydx[2] = state[3]

den2 = (L2 / L1) * den1
dydx[3] = (-M2 * L2 * state[3] * state[3] * sin(del_) * cos(del_)
+ (M1 + M2) * G * sin(state[0]) * cos(del_)
- (M1 + M2) * L1 * state[1] * state[1] * sin(del_)
- (M1 + M2) * G * sin(state[2])) / den2
den2 = (L2/L1)*den1
dydx[3] = (-M2*L2*state[3]*state[3]*sin(del_)*cos(del_)
+ (M1+M2)*G*sin(state[0])*cos(del_)
- (M1+M2)*L1*state[1]*state[1]*sin(del_)
- (M1+M2)*G*sin(state[2]))/den2

return dydx

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

rad = pi/180

# initial state
state = np.radians([th1, w1, th2, w2])
state = np.array([th1, w1, th2, w2])*pi/180.

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

x1 = L1 * sin(y[:, 0])
y1 = -L1 * cos(y[:, 0])
x1 = L1*sin(y[:,0])
y1 = -L1*cos(y[:,0])

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

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


def init():
line.set_data([], [])
time_text.set_text('')
return line, time_text


def animate(i):
thisx = [0, x1[i], x2[i]]
thisy = [0, y1[i], y2[i]]

line.set_data(thisx, thisy)
time_text.set_text(time_template % (i * dt))
time_text.set_text(time_template%(i*dt))
return line, time_text

ani = animation.FuncAnimation(fig, animate, np.arange(1, len(y)),
interval=25, blit=True, init_func=init)
interval=25, blit=True, init_func=init)

#ani.save('double_pendulum.mp4', fps=15)
plt.show()
6 changes: 2 additions & 4 deletions examples/animation/dynamic_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

fig = plt.figure()


def f(x, y):
return np.sin(x) + np.cos(y)

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

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


def updatefig(*args):
global x, y
global x,y
x += np.pi / 15.
y += np.pi / 20.
im.set_array(f(x, y))
im.set_array(f(x,y))
return im,

ani = animation.FuncAnimation(fig, updatefig, interval=50, blit=True)
Expand Down
5 changes: 2 additions & 3 deletions examples/animation/dynamic_image2.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

fig = plt.figure()


def f(x, y):
return np.sin(x) + np.cos(y)

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

ani = animation.ArtistAnimation(fig, ims, interval=50, blit=True,
repeat_delay=1000)
repeat_delay=1000)

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


plt.show()
26 changes: 12 additions & 14 deletions examples/animation/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,34 @@
# for each rect: 1 for the MOVETO, 3 for the LINETO, 1 for the
# CLOSEPOLY; the vert for the closepoly is ignored but we still need
# it to keep the codes aligned with the vertices
nverts = nrects * (1 + 3 + 1)
nverts = nrects*(1+3+1)
verts = np.zeros((nverts, 2))
codes = np.ones(nverts, int) * path.Path.LINETO
codes[0::5] = path.Path.MOVETO
codes[4::5] = path.Path.CLOSEPOLY
verts[0::5, 0] = left
verts[0::5, 1] = bottom
verts[1::5, 0] = left
verts[1::5, 1] = top
verts[2::5, 0] = right
verts[2::5, 1] = top
verts[3::5, 0] = right
verts[3::5, 1] = bottom
verts[0::5,0] = left
verts[0::5,1] = bottom
verts[1::5,0] = left
verts[1::5,1] = top
verts[2::5,0] = right
verts[2::5,1] = top
verts[3::5,0] = right
verts[3::5,1] = bottom

barpath = path.Path(verts, codes)
patch = patches.PathPatch(
barpath, facecolor='green', edgecolor='yellow', alpha=0.5)
patch = patches.PathPatch(barpath, facecolor='green', edgecolor='yellow', alpha=0.5)
ax.add_patch(patch)

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


def animate(i):
# simulate new data coming in
data = np.random.randn(1000)
n, bins = np.histogram(data, 100)
top = bottom + n
verts[1::5, 1] = top
verts[2::5, 1] = top
verts[1::5,1] = top
verts[2::5,1] = top

ani = animation.FuncAnimation(fig, animate, 100, repeat=False)
plt.show()
5 changes: 3 additions & 2 deletions examples/animation/moviewriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

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

fig = plt.figure()
Expand All @@ -21,11 +21,12 @@
plt.xlim(-5, 5)
plt.ylim(-5, 5)

x0, y0 = 0, 0
x0,y0 = 0, 0

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

18 changes: 9 additions & 9 deletions examples/animation/rain.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
"""
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from matplotlib.animation import FuncAnimation


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

# Create rain data
n_drops = 50
Expand All @@ -31,7 +31,7 @@

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

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

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

# Make all circles bigger.
rain_drops['size'] += rain_drops['growth']
Expand All @@ -58,7 +58,7 @@ def update(frame_number):
scat.set_edgecolors(rain_drops['color'])
scat.set_sizes(rain_drops['size'])
scat.set_offsets(rain_drops['position'])


# Construct the animation, using the update function as the animation
# director.
Expand Down
5 changes: 1 addition & 4 deletions examples/animation/random_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@
line, = ax.plot(np.random.rand(10))
ax.set_ylim(0, 1)


def update(data):
line.set_ydata(data)
return line,


def data_gen():
while True:
yield np.random.rand(10)
while True: yield np.random.rand(10)

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