Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
show test scripts for animated figures with blit, imshow, axis and text in Matplotlib
#!/usr/bin/env python3
import matplotlib.animation as animation
import matplotlib.pyplot as plt
import numpy as np
# https://stackoverflow.com/a/30860027/242583
# patch for ax.xaxis
# 1. ax.xaxis.set_animated(True)
# 2. add ax.xaxis to the returning list of update function
def _blit_draw(self, artists, bg_cache):
# Handles blitted drawing, which renders only the artists given instead
# of the entire figure.
updated_ax = []
for a in artists:
# If we haven't cached the background for this axes object, do
# so now. This might not always be reliable, but it's an attempt
# to automate the process.
if a.axes not in bg_cache:
# bg_cache[a.axes] = a.figure.canvas.copy_from_bbox(a.axes.bbox)
# change here
bg_cache[a.axes] = a.figure.canvas.copy_from_bbox(a.axes.figure.bbox)
a.axes.draw_artist(a)
updated_ax.append(a.axes)
# After rendering all the needed artists, blit each axes individually.
for ax in set(updated_ax):
# and here
# ax.figure.canvas.blit(ax.bbox)
ax.figure.canvas.blit(ax.figure.bbox)
# MONKEY PATCH!!
animation.Animation._blit_draw = _blit_draw
def update(data, axes, im):
im.set_data(data)
ax = axes[1]
xlabels = [str(i) for i in np.random.randint(100, size=4)]
ax.xaxis.set_ticklabels(xlabels)
return axes + [ax.xaxis]
def main():
fig = plt.figure()
ax = plt.axes([0.1, 0.2, 0.8, 0.7])
frames = []
data = np.arange(64).reshape(8, 8)
data = data + np.transpose(data)
data %= 8
frames.append(data / 7)
for i in range(8 - 1):
data = data + 1
data %= 8
frames.append(data / 7)
IMSHOW_OPTS = {
'aspect': 'auto',
'cmap': plt.get_cmap('Purples'),
'interpolation': 'none',
'animated': True,
}
im = ax.imshow(frames[1], **IMSHOW_OPTS)
imdata = im
ax.grid(axis='x', which='major')
ax = plt.axes([0.1, 0.1, 0.8, 0.05])
data = np.arange(256)
data = np.vstack((data, data))
IMSHOW_OPTS['cmap'] = plt.get_cmap('gray')
im = ax.imshow(data, **IMSHOW_OPTS)
ax.set_xticks(np.arange(0, 256, 64))
ax.set_xlim(left=0.0, right=255.0)
ax.grid(axis='x', which='major')
ax.xaxis.set_animated(True)
animation.FuncAnimation(
fig,
update,
frames=frames,
fargs=(fig.get_axes(), imdata, ),
interval=500,
blit=True,
)
plt.show()
main()
#!/usr/bin/env python3
import matplotlib.animation as animation
import matplotlib.pyplot as plt
import numpy as np
def update(data, ax, im):
im.set_data(data)
return [ax]
def main():
fig = plt.figure()
ax = plt.axes([0.1, 0.1, 0.8, 0.8])
frames = []
data = np.arange(64).reshape(8, 8)
data = data + np.transpose(data)
data %= 8
frames.append(data / 7)
for i in range(8 - 1):
data = data + 1
data %= 8
frames.append(data / 7)
IMSHOW_OPTS = {
'aspect': 'auto',
'cmap': plt.get_cmap('Purples'),
'interpolation': 'none',
'animated': True,
}
im = ax.imshow(frames[1], **IMSHOW_OPTS)
ax.grid(axis='x', which='major')
animation.FuncAnimation(
fig,
update,
frames=frames,
fargs=(ax, im, ),
interval=500,
blit=True,
)
plt.show()
main()
#!/usr/bin/env python3
import matplotlib.animation as animation
import matplotlib.pyplot as plt
import numpy as np
# https://stackoverflow.com/a/30860027/242583
# patch for ax.xaxis
# 1. ax.xaxis.set_animated(True)
# 2. add ax.xaxis to the returning list of update function
def _blit_draw(self, artists, bg_cache):
# Handles blitted drawing, which renders only the artists given instead
# of the entire figure.
updated_ax = []
for a in artists:
# If we haven't cached the background for this axes object, do
# so now. This might not always be reliable, but it's an attempt
# to automate the process.
if a.axes not in bg_cache:
# bg_cache[a.axes] = a.figure.canvas.copy_from_bbox(a.axes.bbox)
# change here
bg_cache[a.axes] = a.figure.canvas.copy_from_bbox(a.axes.figure.bbox)
a.axes.draw_artist(a)
updated_ax.append(a.axes)
# After rendering all the needed artists, blit each axes individually.
for ax in set(updated_ax):
# and here
# ax.figure.canvas.blit(ax.bbox)
ax.figure.canvas.blit(ax.figure.bbox)
# MONKEY PATCH!!
animation.Animation._blit_draw = _blit_draw
def update(data, axes, im, text):
im.set_data(data)
ax = axes[1]
xlabels = [str(i) for i in np.random.randint(100, size=4)]
ax.xaxis.set_ticklabels(xlabels)
text.set_text(str(np.random.randint(100)))
return axes + [ax.xaxis, text]
def main():
fig = plt.figure()
ax = plt.axes([0.1, 0.2, 0.8, 0.7])
frames = []
data = np.arange(64).reshape(8, 8)
data = data + np.transpose(data)
data %= 8
frames.append(data / 7)
for i in range(8 - 1):
data = data + 1
data %= 8
frames.append(data / 7)
IMSHOW_OPTS = {
'aspect': 'auto',
'cmap': plt.get_cmap('Purples'),
'interpolation': 'none',
'animated': True,
}
im = ax.imshow(frames[1], **IMSHOW_OPTS)
imdata = im
ax.grid(axis='x', which='major')
ax = plt.axes([0.1, 0.1, 0.8, 0.05])
data = np.arange(256)
data = np.vstack((data, data))
IMSHOW_OPTS['cmap'] = plt.get_cmap('gray')
im = ax.imshow(data, **IMSHOW_OPTS)
ax.set_xticks(np.arange(0, 256, 64))
ax.set_xlim(left=0.0, right=255.0)
ax.grid(axis='x', which='major')
ax.xaxis.set_animated(True)
# not fig.text, animation can only support Artist belonging to Axes
text = ax.text(0.95, 0.05, 'JUST STARTED', transform=fig.transFigure,
horizontalalignment='right', verticalalignment='center')
text.set_animated(True)
animation.FuncAnimation(
fig,
update,
frames=frames,
fargs=(fig.get_axes(), imdata, text),
interval=500,
blit=True,
)
plt.show()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.