incrementalgraph/test.py

82 lines
2.7 KiB
Python

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2 * np.pi, 100)
fig, ax = plt.subplots()
ax.set_xlim(0, 100)
ax.set_ylim(0, 10000)
# animated=True tells matplotlib to only draw the artist when we
# explicitly request it
(ln,) = ax.plot([0,1], [0,1], 'r-', animated=True)
# make sure the window is raised, but the script keeps going
plt.show(block=False)
# stop to admire our empty window axes and ensure it is rendered at
# least once.
#
# We need to fully draw the figure at its final size on the screen
# before we continue on so that :
# a) we have the correctly sized and drawn background to grab
# b) we have a cached renderer so that ``ax.draw_artist`` works
# so we spin the event loop to let the backend process any pending operations
plt.pause(0.1)
ax.draw_artist(ln)
# get copy of entire figure (everything inside fig.bbox) sans animated artist
bg = fig.canvas.copy_from_bbox(fig.bbox)
# draw the animated artist, this uses a cached renderer
ax.draw_artist(ln)
# show the result to the screen, this pushes the updated RGBA buffer from the
# renderer to the GUI framework so you can see it
fig.canvas.blit(fig.bbox)
x_ax = []
y_ax = []
for j in range(100):
# reset the background back in the canvas state, screen unchanged
#fig.canvas.restore_region(bg)
# update the artist, neither the canvas state nor the screen have changed
x_ax.append(j)
y_ax.append(j*j)
ln.set_data([j,j+1], [j*j,(j+1)*(j+1)])
# re-render the artist, updating the canvas state, but not the screen
ax.draw_artist(ln)
# copy the image to the GUI state, but screen might not be changed yet
fig.canvas.blit(fig.bbox)
# flush any pending GUI events, re-painting the screen if needed
fig.canvas.flush_events()
# you can put a pause in if you want to slow things down
plt.pause(.1)
ax.clear()
ax.set_xlim(0, 200)
ax.set_ylim(0, 200*200)
(ln,) = ax.plot(x_ax, y_ax, 'r-', animated = True)
plt.pause(0.1)
ax.draw_artist(ln)
bg = fig.canvas.copy_from_bbox(fig.bbox)
fig.canvas.blit(fig.bbox)
for j in range(100, 200):
# reset the background back in the canvas state, screen unchanged
#fig.canvas.restore_region(bg)
# update the artist, neither the canvas state nor the screen have changed
x_ax.append(j)
y_ax.append(j*j)
ln.set_data([j,j+1], [j*j,(j+1)*(j+1)])
# re-render the artist, updating the canvas state, but not the screen
ax.draw_artist(ln)
# copy the image to the GUI state, but screen might not be changed yet
fig.canvas.blit(fig.bbox)
# flush any pending GUI events, re-painting the screen if needed
fig.canvas.flush_events()
# you can put a pause in if you want to slow things down
plt.pause(.1)