Created
December 10, 2012 23:32
-
-
Save spinnau/4254321 to your computer and use it in GitHub Desktop.
Test blitting for matplotlib backends
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
from gi.repository import GObject | |
import matplotlib.pyplot as plt | |
from matplotlib.transforms import Bbox | |
from matplotlib.testing.compare import compare_images | |
import numpy as np | |
import subprocess | |
fig = plt.figure(0) | |
ax = fig.add_subplot(111) | |
canvas = fig.canvas | |
x = np.arange(0, 10, 0.1) | |
y = np.sin(x) | |
bbox = Bbox.from_bounds(0, 0, 10, 1) | |
background = canvas.copy_from_bbox(bbox) | |
line, = plt.plot(x, y, color='k') | |
def blit_test(*args): | |
""" Test backend's blit method """ | |
# create reference screenshot for normal plot | |
if blit_test.n == 0: | |
subprocess.Popen(['import', '-window', 'Figure 0', 'ref.png'], stdout=subprocess.PIPE).communicate() | |
else: | |
canvas.restore_region(background) | |
ax.draw_artist(line) | |
canvas.blit(ax.bbox) | |
# create blitting screenshot and compare | |
if blit_test.n == 4: | |
subprocess.Popen(['import', '-window', 'Figure 0', 'blit.png'], stdout=subprocess.PIPE).communicate() | |
# some tolerance is needed, because plot line thickness is not exact | |
# the same during blitting | |
rms = compare_images('ref.png', 'blit.png', tol=1e-2) | |
if rms is None: | |
print('Test passed...') | |
else: | |
print('Test failed...') | |
plt.close() | |
blit_test.n += 1 | |
return True | |
blit_test.n = 0 | |
GObject.idle_add(blit_test) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment