1
1
import matplotlib .pyplot as plt
2
- import pickle
2
+ from matplotview . tests . utils import plotting_test , matches_post_pickle
3
3
from matplotview import view , view_wrapper , inset_zoom_axes
4
4
import numpy as np
5
5
6
- def to_image (figure ):
7
- figure .canvas .draw ()
8
- img = np .frombuffer (figure .canvas .tostring_rgb (), dtype = np .uint8 )
9
- return img .reshape (figure .canvas .get_width_height ()[::- 1 ] + (3 ,))
10
-
11
6
12
7
def test_obj_comparison ():
13
8
from matplotlib .axes import Subplot , Axes
@@ -21,12 +16,13 @@ def test_obj_comparison():
21
16
assert view_class2 != view_class3
22
17
23
18
24
- def test_subplot_view_pickle ():
19
+ @plotting_test ()
20
+ def test_subplot_view_pickle (fig_test ):
25
21
np .random .seed (1 )
26
22
im_data = np .random .rand (30 , 30 )
27
23
28
24
# Test case...
29
- fig_test , ( ax_test1 , ax_test2 ) = plt .subplots (1 , 2 )
25
+ ax_test1 , ax_test2 = fig_test .subplots (1 , 2 )
30
26
31
27
ax_test1 .plot ([i for i in range (10 )], "r" )
32
28
ax_test1 .add_patch (plt .Circle ((3 , 3 ), 1 , ec = "black" , fc = "blue" ))
@@ -38,24 +34,15 @@ def test_subplot_view_pickle():
38
34
ax_test2 .set_xlim (ax_test1 .get_xlim ())
39
35
ax_test2 .set_ylim (ax_test1 .get_ylim ())
40
36
41
- img_expected = to_image (fig_test )
42
-
43
- saved_fig = pickle .dumps (fig_test )
44
- plt .clf ()
45
-
46
- fig_test = pickle .loads (saved_fig )
47
- img_result = to_image (fig_test )
37
+ assert matches_post_pickle (fig_test )
48
38
49
- assert np .all (img_expected == img_result )
50
-
51
-
52
- def test_zoom_plot_pickle ():
39
+ @plotting_test ()
40
+ def test_zoom_plot_pickle (fig_test ):
53
41
np .random .seed (1 )
54
- plt .clf ()
55
42
im_data = np .random .rand (30 , 30 )
43
+ arrow_s = dict (arrowstyle = "->" )
56
44
57
45
# Test Case...
58
- fig_test = plt .gcf ()
59
46
ax_test = fig_test .gca ()
60
47
ax_test .plot ([i for i in range (10 )], "r" )
61
48
ax_test .add_patch (plt .Circle ((3 , 3 ), 1 , ec = "black" , fc = "blue" ))
@@ -65,14 +52,29 @@ def test_zoom_plot_pickle():
65
52
axins_test .set_linescaling (False )
66
53
axins_test .set_xlim (1 , 5 )
67
54
axins_test .set_ylim (1 , 5 )
55
+ axins_test .annotate (
56
+ "Interesting" , (3 , 3 ), (0 , 0 ),
57
+ textcoords = "axes fraction" , arrowprops = arrow_s
58
+ )
68
59
ax_test .indicate_inset_zoom (axins_test , edgecolor = "black" )
69
60
70
- img_expected = to_image (fig_test )
61
+ assert matches_post_pickle (fig_test )
62
+
71
63
72
- saved_fig = pickle .dumps (fig_test )
73
- plt .clf ()
64
+ @plotting_test ()
65
+ def test_3d_view_pickle (fig_test ):
66
+ X = Y = np .arange (- 5 , 5 , 0.25 )
67
+ X , Y = np .meshgrid (X , Y )
68
+ Z = np .sin (np .sqrt (X ** 2 + Y ** 2 ))
74
69
75
- fig_test = pickle .loads (saved_fig )
76
- img_result = to_image (fig_test )
70
+ ax1_test , ax2_test = fig_test .subplots (
71
+ 1 , 2 , subplot_kw = dict (projection = "3d" )
72
+ )
73
+ ax1_test .plot_surface (X , Y , Z , cmap = "plasma" )
74
+ view (ax2_test , ax1_test )
75
+ ax2_test .view_init (elev = 80 )
76
+ ax2_test .set_xlim (- 10 , 10 )
77
+ ax2_test .set_ylim (- 10 , 10 )
78
+ ax2_test .set_zlim (- 2 , 2 )
77
79
78
- assert np . all ( img_expected == img_result )
80
+ assert matches_post_pickle ( fig_test )
0 commit comments