From 12c4bb0dcff1a0c05065a3e791b63632e57a380c Mon Sep 17 00:00:00 2001 From: ashley Date: Thu, 26 May 2016 20:15:47 -0400 Subject: [PATCH 1/4] cleaned up animate_decay.py --- examples/animation/animate_decay.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/examples/animation/animate_decay.py b/examples/animation/animate_decay.py index 3ddcbdf2408e..97eb32d40c7a 100644 --- a/examples/animation/animate_decay.py +++ b/examples/animation/animate_decay.py @@ -1,17 +1,29 @@ +""" +Animated plot showing a decay process with updating x-scale. +""" + import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation def data_gen(t=0): - cnt = 0 - while cnt < 1000: - cnt += 1 + """Generate data + + Generates a decaying sine wave. + """ + count = 0 + while count < 1000: + count += 1 t += 0.1 yield t, np.sin(2*np.pi*t) * np.exp(-t/10.) def init(): + """Create a line + + Returns a matplotlib line object. + """ ax.set_ylim(-1.1, 1.1) ax.set_xlim(0, 10) del xdata[:] @@ -26,7 +38,7 @@ def init(): def run(data): - # update the data + """Update the data, check and set axis scale""" t, y = data xdata.append(t) ydata.append(y) From 4e0181b37ecde5e871849f222af4404cef914542 Mon Sep 17 00:00:00 2001 From: ashley Date: Thu, 26 May 2016 20:32:38 -0400 Subject: [PATCH 2/4] cleaned up double_pendulum_animated.py --- .../animation/double_pendulum_animated.py | 64 +++++++++++-------- 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/examples/animation/double_pendulum_animated.py b/examples/animation/double_pendulum_animated.py index 3b4b48204edb..2cecac4ae520 100644 --- a/examples/animation/double_pendulum_animated.py +++ b/examples/animation/double_pendulum_animated.py @@ -1,38 +1,44 @@ -# Double pendulum formula translated from the C code at -# http://www.physics.usyd.edu.au/~wheat/dpend_html/solve_dpend.c +""" +Show animation of a double pendulum. + +Double pendulum formula translated from the C code at +http://www.physics.usyd.edu.au/~wheat/dpend_html/solve_dpend.c +""" -from numpy import sin, cos import numpy as np import matplotlib.pyplot as plt 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 +length1 = 1.0 # length of pendulum 1 in m +length2 = 1.0 # length of pendulum 2 in m +mass1 = 1.0 # mass of pendulum 1 in kg +mass2 = 1.0 # mass of pendulum 2 in kg def derivs(state, t): - + """Derivatives""" 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 + den1 = ((mass1 + mass2) * length1 - + mass2 * length1 * np.cos(del_) * np.cos(del_)) + dydx[1] = (mass2 * length1 * state[1] * state[1] * np.sin(del_) * + np.cos(del_) + + mass2 * g * np.sin(state[2]) * np.cos(del_) + + mass2 * length2 * state[3] * state[3] * np.sin(del_) - + (mass1 + mass2) * g * np.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 = (length2 / length1) * den1 + dydx[3] = (-mass2 * length2 * state[3] * state[3] * np.sin(del_) * + np.cos(del_) + + (mass1 + mass2) * g * np.sin(state[0]) * np.cos(del_) - + (mass1 + mass2) * length1 * state[1] * state[1] * np.sin(del_) - + (mass1 + mass2) * g * np.sin(state[2])) / den2 return dydx @@ -53,11 +59,11 @@ def derivs(state, t): # integrate your ODE using scipy.integrate. y = integrate.odeint(derivs, state, t) -x1 = L1*sin(y[:, 0]) -y1 = -L1*cos(y[:, 0]) +x1 = length1 * np.sin(y[:, 0]) +y1 = -length1 * np.cos(y[:, 0]) -x2 = L2*sin(y[:, 2]) + x1 -y2 = -L2*cos(y[:, 2]) + y1 +x2 = length2 * np.sin(y[:, 2]) + x1 +y2 = -length2 * np.cos(y[:, 2]) + y1 fig = plt.figure() ax = fig.add_subplot(111, autoscale_on=False, xlim=(-2, 2), ylim=(-2, 2)) @@ -70,21 +76,29 @@ def derivs(state, t): def init(): + """Create a line and text to show time of the animation.""" line.set_data([], []) time_text.set_text('') + plt.xlabel('horizontal position (m)') + plt.ylabel('vertical position (m)') return line, time_text def animate(i): + """Update the animation. + + Updates the positions of the double pendulum and the time shown, + indexed by 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) -#ani.save('double_pendulum.mp4', fps=15) +# ani.save('double_pendulum.mp4', fps=15) plt.show() From e5ec6a0a039f151fd28d515cd081fd46dad87181 Mon Sep 17 00:00:00 2001 From: ashley Date: Thu, 26 May 2016 20:43:01 -0400 Subject: [PATCH 3/4] diversified names on vertical axis --- examples/lines_bars_and_markers/barh_demo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/lines_bars_and_markers/barh_demo.py b/examples/lines_bars_and_markers/barh_demo.py index 965d135b43ac..869dd0d6ca3f 100644 --- a/examples/lines_bars_and_markers/barh_demo.py +++ b/examples/lines_bars_and_markers/barh_demo.py @@ -8,7 +8,7 @@ plt.rcdefaults() # Example data -people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim') +people = ('Sally', 'Pedro', 'Jing', 'Sydney', 'Jim') y_pos = np.arange(len(people)) performance = 3 + 10 * np.random.rand(len(people)) error = np.random.rand(len(people)) From 9619f22cc2a04f6a0864cfaba16b764d6abe8dd9 Mon Sep 17 00:00:00 2001 From: ashley Date: Thu, 26 May 2016 22:08:01 -0400 Subject: [PATCH 4/4] changed while loop to for loop in animate_decay.py --- examples/animation/animate_decay.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/examples/animation/animate_decay.py b/examples/animation/animate_decay.py index 97eb32d40c7a..d66e6d31f703 100644 --- a/examples/animation/animate_decay.py +++ b/examples/animation/animate_decay.py @@ -12,9 +12,7 @@ def data_gen(t=0): Generates a decaying sine wave. """ - count = 0 - while count < 1000: - count += 1 + for count in range(1000): t += 0.1 yield t, np.sin(2*np.pi*t) * np.exp(-t/10.)