@@ -107,7 +107,7 @@ def test_multiple_keys():
107107@image_comparison (['rgba_alpha.png' ], remove_text = True ,
108108 tol = 0 if platform .machine () == 'x86_64' else 0.01 )
109109def test_alpha_rgba ():
110- fig , ax = plt .subplots (1 , 1 )
110+ fig , ax = plt .subplots ()
111111 ax .plot (range (10 ), lw = 5 )
112112 leg = plt .legend (['Longlabel that will go away' ], loc = 'center' )
113113 leg .legendPatch .set_facecolor ([1 , 0 , 0 , 0.5 ])
@@ -116,7 +116,7 @@ def test_alpha_rgba():
116116@image_comparison (['rcparam_alpha.png' ], remove_text = True ,
117117 tol = 0 if platform .machine () == 'x86_64' else 0.01 )
118118def test_alpha_rcparam ():
119- fig , ax = plt .subplots (1 , 1 )
119+ fig , ax = plt .subplots ()
120120 ax .plot (range (10 ), lw = 5 )
121121 with mpl .rc_context (rc = {'legend.framealpha' : .75 }):
122122 leg = plt .legend (['Longlabel that will go away' ], loc = 'center' )
@@ -228,26 +228,26 @@ def test_legend_remove():
228228
229229class TestLegendFunction :
230230 # Tests the legend function on the Axes and pyplot.
231- def test_legend_handle_label (self ):
231+ def test_legend_no_args (self ):
232+ lines = plt .plot (range (10 ), label = 'hello world' )
233+ with mock .patch ('matplotlib.legend.Legend' ) as Legend :
234+ plt .legend ()
235+ Legend .assert_called_with (plt .gca (), lines , ['hello world' ])
236+
237+ def test_legend_positional_handles_labels (self ):
232238 lines = plt .plot (range (10 ))
233239 with mock .patch ('matplotlib.legend.Legend' ) as Legend :
234240 plt .legend (lines , ['hello world' ])
235241 Legend .assert_called_with (plt .gca (), lines , ['hello world' ])
236242
237- def test_legend_handles_only (self ):
243+ def test_legend_positional_handles_only (self ):
238244 lines = plt .plot (range (10 ))
239245 with pytest .raises (TypeError , match = 'but found an Artist' ):
240246 # a single arg is interpreted as labels
241247 # it's a common error to just pass handles
242248 plt .legend (lines )
243249
244- def test_legend_no_args (self ):
245- lines = plt .plot (range (10 ), label = 'hello world' )
246- with mock .patch ('matplotlib.legend.Legend' ) as Legend :
247- plt .legend ()
248- Legend .assert_called_with (plt .gca (), lines , ['hello world' ])
249-
250- def test_legend_label_args (self ):
250+ def test_legend_positional_labels_only (self ):
251251 lines = plt .plot (range (10 ), label = 'hello world' )
252252 with mock .patch ('matplotlib.legend.Legend' ) as Legend :
253253 plt .legend (['foobar' ])
@@ -267,20 +267,40 @@ def test_legend_handler_map(self):
267267 plt .legend (handler_map = {'1' : 2 })
268268 handles_labels .assert_called_with ([plt .gca ()], {'1' : 2 })
269269
270- def test_kwargs (self ):
271- fig , ax = plt .subplots (1 , 1 )
270+ def test_legend_kwargs_handles_only (self ):
271+ fig , ax = plt .subplots ()
272+ x = np .linspace (0 , 1 , 11 )
273+ ln1 , = ax .plot (x , x , label = 'x' )
274+ ln2 , = ax .plot (x , 2 * x , label = '2x' )
275+ ln3 , = ax .plot (x , 3 * x , label = '3x' )
276+ with mock .patch ('matplotlib.legend.Legend' ) as Legend :
277+ ax .legend (handles = [ln3 , ln2 ]) # reversed and not ln1
278+ Legend .assert_called_with (ax , [ln3 , ln2 ], ['3x' , '2x' ])
279+
280+ def test_legend_kwargs_labels_only (self ):
281+ fig , ax = plt .subplots ()
282+ x = np .linspace (0 , 1 , 11 )
283+ ln1 , = ax .plot (x , x )
284+ ln2 , = ax .plot (x , 2 * x )
285+ with mock .patch ('matplotlib.legend.Legend' ) as Legend :
286+ ax .legend (labels = ['x' , '2x' ])
287+ Legend .assert_called_with (ax , [ln1 , ln2 ], ['x' , '2x' ])
288+
289+ def test_legend_kwargs_handles_labels (self ):
290+ fig , ax = plt .subplots ()
272291 th = np .linspace (0 , 2 * np .pi , 1024 )
273- lns , = ax .plot (th , np .sin (th ), label = 'sin' , lw = 5 )
274- lnc , = ax .plot (th , np .cos (th ), label = 'cos' , lw = 5 )
292+ lns , = ax .plot (th , np .sin (th ), label = 'sin' )
293+ lnc , = ax .plot (th , np .cos (th ), label = 'cos' )
275294 with mock .patch ('matplotlib.legend.Legend' ) as Legend :
295+ # labels of lns, lnc are overwritten with explict ('a', 'b')
276296 ax .legend (labels = ('a' , 'b' ), handles = (lnc , lns ))
277297 Legend .assert_called_with (ax , (lnc , lns ), ('a' , 'b' ))
278298
279- def test_warn_args_kwargs (self ):
280- fig , ax = plt .subplots (1 , 1 )
299+ def test_warn_mixed_args_and_kwargs (self ):
300+ fig , ax = plt .subplots ()
281301 th = np .linspace (0 , 2 * np .pi , 1024 )
282- lns , = ax .plot (th , np .sin (th ), label = 'sin' , lw = 5 )
283- lnc , = ax .plot (th , np .cos (th ), label = 'cos' , lw = 5 )
302+ lns , = ax .plot (th , np .sin (th ), label = 'sin' )
303+ lnc , = ax .plot (th , np .cos (th ), label = 'cos' )
284304 with pytest .warns (UserWarning ) as record :
285305 ax .legend ((lnc , lns ), labels = ('a' , 'b' ))
286306 assert len (record ) == 1
0 commit comments