@@ -107,7 +107,7 @@ def test_multiple_keys():
107
107
@image_comparison (['rgba_alpha.png' ], remove_text = True ,
108
108
tol = 0 if platform .machine () == 'x86_64' else 0.01 )
109
109
def test_alpha_rgba ():
110
- fig , ax = plt .subplots (1 , 1 )
110
+ fig , ax = plt .subplots ()
111
111
ax .plot (range (10 ), lw = 5 )
112
112
leg = plt .legend (['Longlabel that will go away' ], loc = 'center' )
113
113
leg .legendPatch .set_facecolor ([1 , 0 , 0 , 0.5 ])
@@ -116,7 +116,7 @@ def test_alpha_rgba():
116
116
@image_comparison (['rcparam_alpha.png' ], remove_text = True ,
117
117
tol = 0 if platform .machine () == 'x86_64' else 0.01 )
118
118
def test_alpha_rcparam ():
119
- fig , ax = plt .subplots (1 , 1 )
119
+ fig , ax = plt .subplots ()
120
120
ax .plot (range (10 ), lw = 5 )
121
121
with mpl .rc_context (rc = {'legend.framealpha' : .75 }):
122
122
leg = plt .legend (['Longlabel that will go away' ], loc = 'center' )
@@ -228,26 +228,26 @@ def test_legend_remove():
228
228
229
229
class TestLegendFunction :
230
230
# 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 ):
232
238
lines = plt .plot (range (10 ))
233
239
with mock .patch ('matplotlib.legend.Legend' ) as Legend :
234
240
plt .legend (lines , ['hello world' ])
235
241
Legend .assert_called_with (plt .gca (), lines , ['hello world' ])
236
242
237
- def test_legend_handles_only (self ):
243
+ def test_legend_positional_handles_only (self ):
238
244
lines = plt .plot (range (10 ))
239
245
with pytest .raises (TypeError , match = 'but found an Artist' ):
240
246
# a single arg is interpreted as labels
241
247
# it's a common error to just pass handles
242
248
plt .legend (lines )
243
249
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 ):
251
251
lines = plt .plot (range (10 ), label = 'hello world' )
252
252
with mock .patch ('matplotlib.legend.Legend' ) as Legend :
253
253
plt .legend (['foobar' ])
@@ -267,20 +267,40 @@ def test_legend_handler_map(self):
267
267
plt .legend (handler_map = {'1' : 2 })
268
268
handles_labels .assert_called_with ([plt .gca ()], {'1' : 2 })
269
269
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 ()
272
291
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' )
275
294
with mock .patch ('matplotlib.legend.Legend' ) as Legend :
295
+ # labels of lns, lnc are overwritten with explict ('a', 'b')
276
296
ax .legend (labels = ('a' , 'b' ), handles = (lnc , lns ))
277
297
Legend .assert_called_with (ax , (lnc , lns ), ('a' , 'b' ))
278
298
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 ()
281
301
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' )
284
304
with pytest .warns (UserWarning ) as record :
285
305
ax .legend ((lnc , lns ), labels = ('a' , 'b' ))
286
306
assert len (record ) == 1
0 commit comments