Skip to content

Instantly share code, notes, and snippets.

@electronut
Created November 26, 2015 09:57
Show Gist options
  • Save electronut/332b9097179f33c7fcd7 to your computer and use it in GitHub Desktop.
Save electronut/332b9097179f33c7fcd7 to your computer and use it in GitHub Desktop.
A simple python matplotlib animation example that shows an oscillating circle.
"""
oscillating_circle.py
A simple matplotlib animation example that shows an oscillating circle.
electronut.in
"""
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import math
import numpy as np
count = 0
def update(frameNum, a0):
global count
A = math.sin(math.radians(count))
x = [A*math.cos(a) for a in np.arange(0, 2*math.pi+0.1, 0.1)]
y = [math.sin(a) for a in np.arange(0, 2*math.pi+0.1, 0.1)]
a0.set_data(x, y)
count = (count + 1) % 360
fig = plt.figure()
ax = plt.axes(xlim=(-1, 1), ylim=(-1, 1))
a0, = ax.plot([], [])
anim = animation.FuncAnimation(fig, update,
fargs=(a0,),
interval=20)
# show plot
plt.show()
@electronut
Copy link
Author

This is what it looks like:

screen shot 2015-11-26 at 3 30 21 pm

@chupocro
Copy link

chupocro commented Jul 29, 2018

Hi,

this code works really well. Do you by any chance know how to modify your code to animate two 3D lines defined by x, y and z coordinates of their start and end points - where these coordinates are changing in time?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment