|
11 | 11 | import matplotlib as mpl
|
12 | 12 | from matplotlib import pyplot as plt
|
13 | 13 | from matplotlib import animation
|
| 14 | +from matplotlib.testing.decorators import check_figures_equal |
14 | 15 |
|
15 | 16 |
|
16 | 17 | @pytest.fixture()
|
@@ -402,3 +403,37 @@ def update(frame):
|
402 | 403 |
|
403 | 404 | with pytest.warns(UserWarning, match="exhausted"):
|
404 | 405 | anim._start()
|
| 406 | + |
| 407 | + |
| 408 | +@check_figures_equal(extensions=["png"]) |
| 409 | +def test_animation_frame(tmpdir, fig_test, fig_ref): |
| 410 | + # Test the expected image after iterating through a few frames |
| 411 | + # we save the animation to get the iteration because we are not |
| 412 | + # in an interactive framework. |
| 413 | + ax = fig_test.add_subplot() |
| 414 | + ax.set_xlim(0, 2 * np.pi) |
| 415 | + ax.set_ylim(-1, 1) |
| 416 | + x = np.linspace(0, 2 * np.pi, 100) |
| 417 | + line, = ax.plot([], []) |
| 418 | + |
| 419 | + def init(): |
| 420 | + line.set_data([], []) |
| 421 | + return line, |
| 422 | + |
| 423 | + def animate(i): |
| 424 | + line.set_data(x, np.sin(x + i / 100)) |
| 425 | + return line, |
| 426 | + |
| 427 | + anim = animation.FuncAnimation( |
| 428 | + fig_test, animate, init_func=init, frames=5, |
| 429 | + blit=True, repeat=False) |
| 430 | + with tmpdir.as_cwd(): |
| 431 | + anim.save("test.gif") |
| 432 | + |
| 433 | + # Reference figure without animation |
| 434 | + ax = fig_ref.add_subplot() |
| 435 | + ax.set_xlim(0, 2 * np.pi) |
| 436 | + ax.set_ylim(-1, 1) |
| 437 | + |
| 438 | + # 5th frame's data |
| 439 | + ax.plot(x, np.sin(x + 4 / 100)) |
0 commit comments