-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathtest_animation.py
More file actions
60 lines (44 loc) · 1.51 KB
/
test_animation.py
File metadata and controls
60 lines (44 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from unittest.mock import MagicMock
import numpy as np
import pytest
from matplotlib.animation import FuncAnimation
import ultraplot as uplt
def test_auto_layout_not_called_on_every_frame():
"""
Test that auto_layout is not called on every frame of a FuncAnimation.
"""
fig, ax = uplt.subplots()
fig.auto_layout = MagicMock()
x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)
(line,) = ax.plot(x, y)
def update(frame):
line.set_ydata(np.sin(x + frame / 10.0))
return (line,)
ani = FuncAnimation(fig, update, frames=10, blit=False)
# The animation is not actually run, but the initial draw will call auto_layout once
fig.canvas.draw()
assert fig.auto_layout.call_count == 1
def test_draw_idle_skips_auto_layout_after_first_draw():
"""
draw_idle should not re-run auto_layout after the initial draw.
"""
fig, ax = uplt.subplots()
fig.auto_layout = MagicMock()
fig.canvas.draw()
assert fig.auto_layout.call_count == 1
fig.canvas.draw_idle()
assert fig.auto_layout.call_count == 1
def test_layout_array_no_crash():
"""
Test that using layout_array with FuncAnimation does not crash.
"""
layout = [[1, 1], [2, 3]]
fig, axs = uplt.subplots(array=layout)
def update(frame):
for ax in axs:
ax.clear()
ax.plot(np.sin(np.linspace(0, 2 * np.pi) + frame / 10.0))
ani = FuncAnimation(fig, update, frames=10)
# The test passes if no exception is raised
fig.canvas.draw()