11from __future__ import (absolute_import , division , print_function ,
22 unicode_literals )
33
4- import six
4+ import io
55
66import numpy as np
7- import matplotlib
8- from matplotlib .testing .decorators import image_comparison , knownfailureif , cleanup
9- import matplotlib .pyplot as plt
7+ import pytest
108
11- from matplotlib import patches , path , transforms
9+ from matplotlib .testing .decorators import image_comparison , cleanup
10+ import matplotlib .pyplot as plt
1211
13- import pytest
14- import io
12+ from matplotlib import patches , transforms
13+ from matplotlib . path import Path
1514
16- nan = np .nan
17- Path = path .Path
1815
1916# NOTE: All of these tests assume that path.simplify is set to True
2017# (the default)
@@ -24,39 +21,38 @@ def test_clipping():
2421 t = np .arange (0.0 , 2.0 , 0.01 )
2522 s = np .sin (2 * np .pi * t )
2623
27- fig = plt .figure ()
28- ax = fig .add_subplot (111 )
24+ fig , ax = plt .subplots ()
2925 ax .plot (t , s , linewidth = 1.0 )
3026 ax .set_ylim ((- 0.20 , - 0.28 ))
3127
28+
3229@image_comparison (baseline_images = ['overflow' ], remove_text = True )
3330def 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 ])
3532 y = np .arange (len (x ))
3633
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+
4138
4239@image_comparison (baseline_images = ['clipping_diamond' ], remove_text = True )
4340def test_diamond ():
4441 x = np .array ([0.0 , 1.0 , 0.0 , - 1.0 , 0.0 ])
4542 y = np .array ([1.0 , 0.0 , - 1.0 , 0.0 , 1.0 ])
4643
47- fig = plt .figure ()
48- ax = fig .add_subplot (111 )
44+ fig , ax = plt .subplots ()
4945 ax .plot (x , y )
5046 ax .set_xlim (xmin = - 0.6 , xmax = 0.6 )
5147 ax .set_ylim (ymin = - 0.6 , ymax = 0.6 )
5248
49+
5350@cleanup
5451def test_noise ():
5552 np .random .seed (0 )
5653 x = np .random .uniform (size = (5000 ,)) * 50
5754
58- fig = plt .figure ()
59- ax = fig .add_subplot (111 )
55+ fig , ax = plt .subplots ()
6056 p1 = ax .plot (x , solid_joinstyle = 'round' , linewidth = 2.0 )
6157
6258 path = p1 [0 ].get_path ()
@@ -66,13 +62,14 @@ def test_noise():
6662
6763 assert len (simplified ) == 3884
6864
65+
6966@cleanup
7067def test_sine_plus_noise ():
7168 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 )
7371
74- fig = plt .figure ()
75- ax = fig .add_subplot (111 )
72+ fig , ax = plt .subplots ()
7673 p1 = ax .plot (x , solid_joinstyle = 'round' , linewidth = 2.0 )
7774
7875 path = p1 [0 ].get_path ()
@@ -82,32 +79,34 @@ def test_sine_plus_noise():
8279
8380 assert len (simplified ) == 876
8481
82+
8583@image_comparison (baseline_images = ['simplify_curve' ], remove_text = True )
8684def test_simplify_curve ():
8785 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 ]),
9090 fc = "none" )
9191
92- fig = plt .figure ()
93- ax = fig .add_subplot (111 )
92+ fig , ax = plt .subplots ()
9493 ax .add_patch (pp1 )
9594 ax .set_xlim ((0 , 2 ))
9695 ax .set_ylim ((0 , 2 ))
9796
97+
9898@image_comparison (baseline_images = ['hatch_simplify' ], remove_text = True )
9999def test_hatch ():
100- fig = plt .figure ()
101- ax = fig .add_subplot (111 )
100+ fig , ax = plt .subplots ()
102101 ax .add_patch (plt .Rectangle ((0 , 0 ), 1 , 1 , fill = False , hatch = "/" ))
103102 ax .set_xlim ((0.45 , 0.55 ))
104103 ax .set_ylim ((0.45 , 0.55 ))
105104
105+
106106@image_comparison (baseline_images = ['fft_peaks' ], remove_text = True )
107107def test_fft_peaks ():
108- fig = plt .figure ()
108+ fig , ax = plt .subplots ()
109109 t = np .arange (65536 )
110- ax = fig .add_subplot (111 )
111110 p1 = ax .plot (abs (np .fft .fft (np .sin (2 * np .pi * .01 * t )* np .blackman (len (t )))))
112111
113112 path = p1 [0 ].get_path ()
@@ -117,6 +116,7 @@ def test_fft_peaks():
117116
118117 assert len (simplified ) == 20
119118
119+
120120@cleanup
121121def test_start_with_moveto ():
122122 # Should be entirely clipped away to a single MOVETO
@@ -153,19 +153,21 @@ def test_start_with_moveto():
153153 verts = np .fromstring (decodebytes (data ), dtype = '<i4' )
154154 verts = verts .reshape ((len (verts ) // 2 , 2 ))
155155 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 ))
157158 segs = list (segs )
158159 assert len (segs ) == 1
159160 assert segs [0 ][1 ] == Path .MOVETO
160161
162+
161163@cleanup
162164def test_throw_rendering_complexity_exceeded ():
163165 plt .rcParams ['path.simplify' ] = False
164166 xx = np .arange (200000 )
165167 yy = np .random .rand (200000 )
166168 yy [1000 ] = np .nan
167- fig = plt . figure ()
168- ax = fig . add_subplot ( 111 )
169+
170+ fig , ax = plt . subplots ( )
169171 ax .plot (xx , yy )
170172 with pytest .raises (OverflowError ):
171173 fig .savefig (io .BytesIO ())
@@ -175,9 +177,9 @@ def test_throw_rendering_complexity_exceeded():
175177def test_clipper ():
176178 dat = (0 , 1 , 0 , 2 , 0 , 3 , 0 , 4 , 0 , 5 )
177179 fig = plt .figure (figsize = (2 , 1 ))
178- fig .subplots_adjust (left = 0 , bottom = 0 , wspace = 0 , hspace = 0 )
180+ fig .subplots_adjust (left = 0 , bottom = 0 , wspace = 0 , hspace = 0 )
179181
180- 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 )
181183 ax .plot (dat )
182184 ax .xaxis .set_major_locator (plt .MultipleLocator (1 ))
183185 ax .yaxis .set_major_locator (plt .MultipleLocator (1 ))
@@ -186,39 +188,39 @@ def test_clipper():
186188
187189 ax .set_xlim (5 , 9 )
188190
191+
189192@image_comparison (baseline_images = ['para_equal_perp' ], remove_text = True )
190193def test_para_equal_perp ():
191194 x = np .array ([0 , 1 , 2 , 1 , 0 , - 1 , 0 , 1 ] + [1 ] * 128 )
192195 y = np .array ([1 , 1 , 2 , 1 , 0 , - 1 , 0 , 0 ] + [0 ] * 128 )
193196
194- fig = plt .figure ()
195- ax = fig .add_subplot (111 )
197+ fig , ax = plt .subplots ()
196198 ax .plot (x + 1 , y + 1 )
197199 ax .plot (x + 1 , y + 1 , 'ro' )
198200
201+
199202@image_comparison (baseline_images = ['clipping_with_nans' ])
200203def test_clipping_with_nans ():
201204 x = np .linspace (0 , 3.14 * 2 , 3000 )
202205 y = np .sin (x )
203206 x [::100 ] = np .nan
204207
205- fig = plt .figure ()
206- ax = fig .add_subplot (111 )
208+ fig , ax = plt .subplots ()
207209 ax .plot (x , y )
208210 ax .set_ylim (- 0.25 , 0.25 )
209211
210212
211213def test_clipping_full ():
212- p = path . Path ([[1e30 , 1e30 ]] * 5 )
214+ p = Path ([[1e30 , 1e30 ]] * 5 )
213215 simplified = list (p .iter_segments (clip = [0 , 0 , 100 , 100 ]))
214216 assert simplified == []
215217
216- p = path . Path ([[50 , 40 ], [75 , 65 ]], [1 , 2 ])
218+ p = Path ([[50 , 40 ], [75 , 65 ]], [1 , 2 ])
217219 simplified = list (p .iter_segments (clip = [0 , 0 , 100 , 100 ]))
218220 assert ([(list (x ), y ) for x , y in simplified ] ==
219221 [([50 , 40 ], 1 ), ([75 , 65 ], 2 )])
220222
221- p = path . Path ([[50 , 40 ]], [1 ])
223+ p = Path ([[50 , 40 ]], [1 ])
222224 simplified = list (p .iter_segments (clip = [0 , 0 , 100 , 100 ]))
223225 assert ([(list (x ), y ) for x , y in simplified ] ==
224226 [([50 , 40 ], 1 )])
0 commit comments