Skip to content

Updated some examples [MEP12] #6486

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

Closed
wants to merge 4 commits into from
Closed
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
18 changes: 14 additions & 4 deletions examples/animation/animate_decay.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
"""
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.
"""
for count in range(1000):
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[:]
Expand All @@ -26,7 +36,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)
Expand Down
64 changes: 39 additions & 25 deletions examples/animation/double_pendulum_animated.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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))
Expand All @@ -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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you leave this line? It is useful to show how to save a movie even if we do not want to do it as part of building the docs.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure - I see how that could be useful. Keep as a comment?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

On Thu, May 26, 2016 at 9:55 PM, Ashley DaSilva notifications@github.com
wrote:

In examples/animation/double_pendulum_animated.py
#6486 (comment):

@@ -86,5 +97,4 @@ def animate(i):
ani = animation.FuncAnimation(fig, animate, np.arange(1, len(y)),
interval=25, blit=True, init_func=init)

-#ani.save('double_pendulum.mp4', fps=15)

Sure - I see how that could be useful. Keep as a comment?


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/matplotlib/matplotlib/pull/6486/files/0bd23dcdf6e696b67318eab42fabfff0be41e1b9#r64848024,
or mute the thread
https://github.com/notifications/unsubscribe/AARy-NhveMgFHXFuPt8O6sicISR_Y-7Nks5qFk8hgaJpZM4IoFO_
.

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()
2 changes: 1 addition & 1 deletion examples/lines_bars_and_markers/barh_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
plt.rcdefaults()

# Example data
people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
people = ('Sally', 'Pedro', 'Jing', 'Sydney', 'Jim')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

y_pos = np.arange(len(people))
performance = 3 + 10 * np.random.rand(len(people))
error = np.random.rand(len(people))
Expand Down