1
1
from __future__ import (absolute_import , division , print_function ,
2
2
unicode_literals )
3
3
4
- import six
4
+ import io
5
5
6
6
import numpy as np
7
- import matplotlib
8
- from matplotlib .testing .decorators import image_comparison , knownfailureif , cleanup
7
+ import pytest
8
+
9
+ from matplotlib .testing .decorators import image_comparison , cleanup
9
10
import matplotlib .pyplot as plt
10
11
11
- from matplotlib import patches , path , transforms
12
+ from matplotlib import patches , transforms
13
+ from matplotlib .path import Path
12
14
13
- from nose .tools import raises
14
- import io
15
-
16
- nan = np .nan
17
- Path = path .Path
18
15
19
16
# NOTE: All of these tests assume that path.simplify is set to True
20
17
# (the default)
@@ -24,39 +21,38 @@ def test_clipping():
24
21
t = np .arange (0.0 , 2.0 , 0.01 )
25
22
s = np .sin (2 * np .pi * t )
26
23
27
- fig = plt .figure ()
28
- ax = fig .add_subplot (111 )
24
+ fig , ax = plt .subplots ()
29
25
ax .plot (t , s , linewidth = 1.0 )
30
26
ax .set_ylim ((- 0.20 , - 0.28 ))
31
27
28
+
32
29
@image_comparison (baseline_images = ['overflow' ], remove_text = True )
33
30
def test_overflow ():
34
- x = np .array ([1.0 ,2.0 ,3.0 ,2.0e5 ])
31
+ x = np .array ([1.0 , 2.0 , 3.0 , 2.0e5 ])
35
32
y = np .arange (len (x ))
36
33
37
- fig = plt .figure ()
38
- ax = fig . add_subplot ( 111 )
39
- ax .plot ( x , y )
40
- ax . set_xlim ( xmin = 2 , xmax = 6 )
34
+ fig , ax = plt .subplots ()
35
+ ax . plot ( x , y )
36
+ ax .set_xlim ( xmin = 2 , xmax = 6 )
37
+
41
38
42
39
@image_comparison (baseline_images = ['clipping_diamond' ], remove_text = True )
43
40
def test_diamond ():
44
41
x = np .array ([0.0 , 1.0 , 0.0 , - 1.0 , 0.0 ])
45
42
y = np .array ([1.0 , 0.0 , - 1.0 , 0.0 , 1.0 ])
46
43
47
- fig = plt .figure ()
48
- ax = fig .add_subplot (111 )
44
+ fig , ax = plt .subplots ()
49
45
ax .plot (x , y )
50
46
ax .set_xlim (xmin = - 0.6 , xmax = 0.6 )
51
47
ax .set_ylim (ymin = - 0.6 , ymax = 0.6 )
52
48
49
+
53
50
@cleanup
54
51
def test_noise ():
55
52
np .random .seed (0 )
56
53
x = np .random .uniform (size = (5000 ,)) * 50
57
54
58
- fig = plt .figure ()
59
- ax = fig .add_subplot (111 )
55
+ fig , ax = plt .subplots ()
60
56
p1 = ax .plot (x , solid_joinstyle = 'round' , linewidth = 2.0 )
61
57
62
58
path = p1 [0 ].get_path ()
@@ -66,13 +62,14 @@ def test_noise():
66
62
67
63
assert len (simplified ) == 3884
68
64
65
+
69
66
@cleanup
70
67
def test_sine_plus_noise ():
71
68
np .random .seed (0 )
72
- x = np .sin (np .linspace (0 , np .pi * 2.0 , 1000 )) + np .random .uniform (size = (1000 ,)) * 0.01
69
+ x = (np .sin (np .linspace (0 , np .pi * 2.0 , 1000 )) +
70
+ np .random .uniform (size = (1000 ,)) * 0.01 )
73
71
74
- fig = plt .figure ()
75
- ax = fig .add_subplot (111 )
72
+ fig , ax = plt .subplots ()
76
73
p1 = ax .plot (x , solid_joinstyle = 'round' , linewidth = 2.0 )
77
74
78
75
path = p1 [0 ].get_path ()
@@ -82,32 +79,34 @@ def test_sine_plus_noise():
82
79
83
80
assert len (simplified ) == 876
84
81
82
+
85
83
@image_comparison (baseline_images = ['simplify_curve' ], remove_text = True )
86
84
def test_simplify_curve ():
87
85
pp1 = patches .PathPatch (
88
- Path ([(0 , 0 ), (1 , 0 ), (1 , 1 ), (nan , 1 ), (0 , 0 ), (2 , 0 ), (2 , 2 ), (0 , 0 )],
89
- [Path .MOVETO , Path .CURVE3 , Path .CURVE3 , Path .CURVE3 , Path .CURVE3 , Path .CURVE3 , Path .CURVE3 , Path .CLOSEPOLY ]),
86
+ Path ([(0 , 0 ), (1 , 0 ), (1 , 1 ), (np .nan , 1 ), (0 , 0 ), (2 , 0 ), (2 , 2 ),
87
+ (0 , 0 )],
88
+ [Path .MOVETO , Path .CURVE3 , Path .CURVE3 , Path .CURVE3 , Path .CURVE3 ,
89
+ Path .CURVE3 , Path .CURVE3 , Path .CLOSEPOLY ]),
90
90
fc = "none" )
91
91
92
- fig = plt .figure ()
93
- ax = fig .add_subplot (111 )
92
+ fig , ax = plt .subplots ()
94
93
ax .add_patch (pp1 )
95
94
ax .set_xlim ((0 , 2 ))
96
95
ax .set_ylim ((0 , 2 ))
97
96
97
+
98
98
@image_comparison (baseline_images = ['hatch_simplify' ], remove_text = True )
99
99
def test_hatch ():
100
- fig = plt .figure ()
101
- ax = fig .add_subplot (111 )
100
+ fig , ax = plt .subplots ()
102
101
ax .add_patch (plt .Rectangle ((0 , 0 ), 1 , 1 , fill = False , hatch = "/" ))
103
102
ax .set_xlim ((0.45 , 0.55 ))
104
103
ax .set_ylim ((0.45 , 0.55 ))
105
104
105
+
106
106
@image_comparison (baseline_images = ['fft_peaks' ], remove_text = True )
107
107
def test_fft_peaks ():
108
- fig = plt .figure ()
108
+ fig , ax = plt .subplots ()
109
109
t = np .arange (65536 )
110
- ax = fig .add_subplot (111 )
111
110
p1 = ax .plot (abs (np .fft .fft (np .sin (2 * np .pi * .01 * t )* np .blackman (len (t )))))
112
111
113
112
path = p1 [0 ].get_path ()
@@ -117,6 +116,7 @@ def test_fft_peaks():
117
116
118
117
assert len (simplified ) == 20
119
118
119
+
120
120
@cleanup
121
121
def test_start_with_moveto ():
122
122
# Should be entirely clipped away to a single MOVETO
@@ -153,33 +153,33 @@ def test_start_with_moveto():
153
153
verts = np .fromstring (decodebytes (data ), dtype = '<i4' )
154
154
verts = verts .reshape ((len (verts ) // 2 , 2 ))
155
155
path = Path (verts )
156
- segs = path .iter_segments (transforms .IdentityTransform (), clip = (0.0 , 0.0 , 100.0 , 100.0 ))
156
+ segs = path .iter_segments (transforms .IdentityTransform (),
157
+ clip = (0.0 , 0.0 , 100.0 , 100.0 ))
157
158
segs = list (segs )
158
159
assert len (segs ) == 1
159
160
assert segs [0 ][1 ] == Path .MOVETO
160
161
162
+
161
163
@cleanup
162
- @raises (OverflowError )
163
164
def test_throw_rendering_complexity_exceeded ():
164
165
plt .rcParams ['path.simplify' ] = False
165
166
xx = np .arange (200000 )
166
167
yy = np .random .rand (200000 )
167
168
yy [1000 ] = np .nan
168
- fig = plt . figure ()
169
- ax = fig . add_subplot ( 111 )
169
+
170
+ fig , ax = plt . subplots ( )
170
171
ax .plot (xx , yy )
171
- try :
172
+ with pytest . raises ( OverflowError ) :
172
173
fig .savefig (io .BytesIO ())
173
- finally :
174
- plt .rcParams ['path.simplify' ] = True
174
+
175
175
176
176
@image_comparison (baseline_images = ['clipper_edge' ], remove_text = True )
177
177
def test_clipper ():
178
178
dat = (0 , 1 , 0 , 2 , 0 , 3 , 0 , 4 , 0 , 5 )
179
179
fig = plt .figure (figsize = (2 , 1 ))
180
- fig .subplots_adjust (left = 0 , bottom = 0 , wspace = 0 , hspace = 0 )
180
+ fig .subplots_adjust (left = 0 , bottom = 0 , wspace = 0 , hspace = 0 )
181
181
182
- ax = fig .add_axes ((0 , 0 , 1.0 , 1.0 ), ylim = (0 , 5 ), autoscale_on = False )
182
+ ax = fig .add_axes ((0 , 0 , 1.0 , 1.0 ), ylim = (0 , 5 ), autoscale_on = False )
183
183
ax .plot (dat )
184
184
ax .xaxis .set_major_locator (plt .MultipleLocator (1 ))
185
185
ax .yaxis .set_major_locator (plt .MultipleLocator (1 ))
@@ -188,44 +188,39 @@ def test_clipper():
188
188
189
189
ax .set_xlim (5 , 9 )
190
190
191
+
191
192
@image_comparison (baseline_images = ['para_equal_perp' ], remove_text = True )
192
193
def test_para_equal_perp ():
193
194
x = np .array ([0 , 1 , 2 , 1 , 0 , - 1 , 0 , 1 ] + [1 ] * 128 )
194
195
y = np .array ([1 , 1 , 2 , 1 , 0 , - 1 , 0 , 0 ] + [0 ] * 128 )
195
196
196
- fig = plt .figure ()
197
- ax = fig .add_subplot (111 )
197
+ fig , ax = plt .subplots ()
198
198
ax .plot (x + 1 , y + 1 )
199
199
ax .plot (x + 1 , y + 1 , 'ro' )
200
200
201
+
201
202
@image_comparison (baseline_images = ['clipping_with_nans' ])
202
203
def test_clipping_with_nans ():
203
204
x = np .linspace (0 , 3.14 * 2 , 3000 )
204
205
y = np .sin (x )
205
206
x [::100 ] = np .nan
206
207
207
- fig = plt .figure ()
208
- ax = fig .add_subplot (111 )
208
+ fig , ax = plt .subplots ()
209
209
ax .plot (x , y )
210
210
ax .set_ylim (- 0.25 , 0.25 )
211
211
212
212
213
213
def test_clipping_full ():
214
- p = path . Path ([[1e30 , 1e30 ]] * 5 )
214
+ p = Path ([[1e30 , 1e30 ]] * 5 )
215
215
simplified = list (p .iter_segments (clip = [0 , 0 , 100 , 100 ]))
216
216
assert simplified == []
217
217
218
- p = path . Path ([[50 , 40 ], [75 , 65 ]], [1 , 2 ])
218
+ p = Path ([[50 , 40 ], [75 , 65 ]], [1 , 2 ])
219
219
simplified = list (p .iter_segments (clip = [0 , 0 , 100 , 100 ]))
220
220
assert ([(list (x ), y ) for x , y in simplified ] ==
221
221
[([50 , 40 ], 1 ), ([75 , 65 ], 2 )])
222
222
223
- p = path . Path ([[50 , 40 ]], [1 ])
223
+ p = Path ([[50 , 40 ]], [1 ])
224
224
simplified = list (p .iter_segments (clip = [0 , 0 , 100 , 100 ]))
225
225
assert ([(list (x ), y ) for x , y in simplified ] ==
226
226
[([50 , 40 ], 1 )])
227
-
228
-
229
- if __name__ == '__main__' :
230
- import nose
231
- nose .runmodule (argv = ['-s' ,'--with-doctest' ], exit = False )
0 commit comments