Created
February 18, 2015 18:42
-
-
Save tgarc/9881ad1d316393967a9b to your computer and use it in GitHub Desktop.
Barebones manual animations in matplotlib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Using matplotlib for manually creating animations. Works similar to FuncAnimation. Manual animation is a bit simpler than using the animations API in general since it is still capable of handling some events (like window resizing) automatically. | |
""" | |
import matplotlib.pyplot as plt | |
import numpy as np | |
# Create a figure and axis for plotting on | |
fig = plt.figure() | |
ax = fig.add_subplot(111) | |
# Add the line 'artist' to the plotting axis | |
# use 'steps' to plot binary codes | |
line = plt.Line2D((),(),drawstyle='steps-pre') | |
ax.add_line(line) | |
# Apply any static formatting to the axis | |
ax.grid() | |
ax.set_ylim(-2, 2) | |
# ... | |
try: | |
limit = int(raw_input('Enter the value of limit:')) | |
codelength = int(np.ceil(np.log2(limit)))+1 # see note* | |
ax.set_xlim(0,codelength) | |
# Enable interactive plotting | |
plt.ion() | |
for x in range(0,limit): | |
# create your fake data | |
y = bin(x)[2:].zfill(codelength) | |
data = [int(i) for i in y] | |
print data | |
xs = range(len(data)) | |
line.set_data(xs,data) # Update line data | |
fig.canvas.draw() # Ask to redraw the figure | |
# Do a *required* pause to allow figure to redraw | |
plt.pause(2) # delay in seconds between frames | |
except KeyboardInterrupt: # allow user to escape with Ctrl+c | |
pass | |
finally: # Always clean up! | |
plt.close(fig) | |
plt.ioff() | |
del ax,fig |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment