@@ -1068,237 +1068,201 @@ def test_legend_labelcolor_rcparam_markerfacecolor_short():
10681068 assert mpl .colors .same_color (text .get_color (), color )
10691069
10701070
1071+ def assert_last_legend_patch_color (histogram , leg , expected_color ,
1072+ facecolor = False , edgecolor = False ):
1073+ """
1074+ Check that histogram color, legend handle color, and legend label color all
1075+ match the expected input. Provide facecolor and edgecolor flags to clarify
1076+ which feature to match.
1077+ """
1078+ label_color = leg .texts [- 1 ].get_color ()
1079+ patch = leg .get_patches ()[- 1 ]
1080+ histogram = histogram [- 1 ][0 ]
1081+ assert mpl .colors .same_color (label_color , expected_color )
1082+ if facecolor :
1083+ assert mpl .colors .same_color (label_color , patch .get_facecolor ())
1084+ assert mpl .colors .same_color (label_color , histogram .get_facecolor ())
1085+ if edgecolor :
1086+ assert mpl .colors .same_color (label_color , patch .get_edgecolor ())
1087+ assert mpl .colors .same_color (label_color , histogram .get_edgecolor ())
1088+
1089+
10711090def test_legend_labelcolor_linecolor_histograms ():
10721091 x = np .arange (10 )
10731092
10741093 # testing c kwarg for bar, step, and stepfilled histograms
10751094 fig , ax = plt .subplots ()
1076- _ , _ , h = ax .hist (x , histtype = 'bar' , color = 'r' ,
1077- label = "red bar hist with a red label" )
1095+ h = ax .hist (x , histtype = 'bar' , color = 'r' , label = "red bar hist with a red label" )
10781096 leg = ax .legend (labelcolor = 'linecolor' )
1079- tc = leg .texts [0 ].get_color ()
1080- assert mpl .colors .same_color (tc , 'r' )
1081- assert mpl .colors .same_color (tc , leg .get_patches ()[0 ].get_facecolor ())
1082- assert mpl .colors .same_color (tc , h [0 ].get_facecolor ())
1097+ assert_last_legend_patch_color (h , leg , 'r' , facecolor = True )
10831098
1084- fig , ax = plt .subplots ()
1085- _ , _ , h = ax .hist (x , histtype = 'step' , color = 'g' ,
1086- label = "green step hist with a green label" )
1099+ h = ax .hist (x , histtype = 'step' , color = 'g' , label = "green step hist, green label" )
10871100 leg = ax .legend (labelcolor = 'linecolor' )
1088- tc = leg .texts [0 ].get_color ()
1089- assert mpl .colors .same_color (tc , 'g' )
1090- assert mpl .colors .same_color (tc , leg .get_patches ()[0 ].get_edgecolor ())
1091- assert mpl .colors .same_color (tc , h [0 ].get_edgecolor ())
1101+ assert_last_legend_patch_color (h , leg , 'g' , edgecolor = True )
10921102
1093- fig , ax = plt .subplots ()
1094- _ , _ , h = ax .hist (x , histtype = 'stepfilled' , color = 'b' ,
1095- label = "blue stepfilled hist with a blue label" )
1103+ h = ax .hist (x , histtype = 'stepfilled' , color = 'b' ,
1104+ label = "blue stepfilled hist with a blue label" )
10961105 leg = ax .legend (labelcolor = 'linecolor' )
1097- tc = leg .texts [0 ].get_color ()
1098- assert mpl .colors .same_color (tc , 'b' )
1099- assert mpl .colors .same_color (tc , leg .get_patches ()[0 ].get_facecolor ())
1100- assert mpl .colors .same_color (tc , h [0 ].get_facecolor ())
1106+ assert_last_legend_patch_color (h , leg , 'b' , facecolor = True )
11011107
11021108 # testing c, fc, and ec combinations for bar histograms
1103- fig , ax = plt .subplots ()
1104- _ , _ , h = ax .hist (x , histtype = 'bar' , color = 'r' , ec = 'b' ,
1105- label = "red bar hist with blue edges and a red label" )
1109+ h = ax .hist (x , histtype = 'bar' , color = 'r' , ec = 'b' ,
1110+ label = "red bar hist with blue edges and a red label" )
11061111 leg = ax .legend (labelcolor = 'linecolor' )
1107- tc = leg .texts [0 ].get_color ()
1108- assert mpl .colors .same_color (tc , 'r' )
1109- assert mpl .colors .same_color (tc , leg .get_patches ()[0 ].get_facecolor ())
1110- assert mpl .colors .same_color (tc , h [0 ].get_facecolor ())
1112+ assert_last_legend_patch_color (h , leg , 'r' , facecolor = True )
11111113
1112- fig , ax = plt .subplots ()
1113- _ , _ , h = ax .hist (x , histtype = 'bar' , fc = 'r' , ec = 'b' ,
1114- label = "red bar hist with blue edges and a red label" )
1114+ h = ax .hist (x , histtype = 'bar' , fc = 'r' , ec = 'b' ,
1115+ label = "red bar hist with blue edges and a red label" )
11151116 leg = ax .legend (labelcolor = 'linecolor' )
1116- tc = leg .texts [0 ].get_color ()
1117- assert mpl .colors .same_color (tc , 'r' )
1118- assert mpl .colors .same_color (tc , leg .get_patches ()[0 ].get_facecolor ())
1119- assert mpl .colors .same_color (tc , h [0 ].get_facecolor ())
1117+ assert_last_legend_patch_color (h , leg , 'r' , facecolor = True )
11201118
1121- fig , ax = plt .subplots ()
1122- _ , _ , h = ax .hist (x , histtype = 'bar' , fc = 'none' , ec = 'b' ,
1123- label = "unfilled blue bar hist with a blue label" )
1119+ h = ax .hist (x , histtype = 'bar' , fc = 'none' , ec = 'b' ,
1120+ label = "unfilled blue bar hist with a blue label" )
11241121 leg = ax .legend (labelcolor = 'linecolor' )
1125- tc = leg .texts [0 ].get_color ()
1126- assert mpl .colors .same_color (tc , 'b' )
1127- assert mpl .colors .same_color (tc , leg .get_patches ()[0 ].get_edgecolor ())
1128- assert mpl .colors .same_color (tc , h [0 ].get_edgecolor ())
1122+ assert_last_legend_patch_color (h , leg , 'b' , edgecolor = True )
11291123
11301124 # testing c, and ec combinations for step histograms
1131- fig , ax = plt .subplots ()
1132- _ , _ , h = ax .hist (x , histtype = 'step' , color = 'r' , ec = 'b' ,
1133- label = "blue step hist with a blue label" )
1125+ h = ax .hist (x , histtype = 'step' , color = 'r' , ec = 'b' ,
1126+ label = "blue step hist with a blue label" )
11341127 leg = ax .legend (labelcolor = 'linecolor' )
1135- tc = leg .texts [0 ].get_color ()
1136- assert mpl .colors .same_color (tc , 'b' )
1137- assert mpl .colors .same_color (tc , leg .get_patches ()[0 ].get_edgecolor ())
1138- assert mpl .colors .same_color (tc , h [0 ].get_edgecolor ())
1128+ assert_last_legend_patch_color (h , leg , 'b' , edgecolor = True )
11391129
1140- fig , ax = plt .subplots ()
1141- _ , _ , h = ax .hist (x , histtype = 'step' , ec = 'b' ,
1142- label = "blue step hist with a blue label" )
1130+ h = ax .hist (x , histtype = 'step' , ec = 'b' ,
1131+ label = "blue step hist with a blue label" )
11431132 leg = ax .legend (labelcolor = 'linecolor' )
1144- tc = leg .texts [0 ].get_color ()
1145- assert mpl .colors .same_color (tc , 'b' )
1146- assert mpl .colors .same_color (tc , leg .get_patches ()[0 ].get_edgecolor ())
1147- assert mpl .colors .same_color (tc , h [0 ].get_edgecolor ())
1133+ assert_last_legend_patch_color (h , leg , 'b' , edgecolor = True )
11481134
11491135 # testing c, fc, and ec combinations for stepfilled histograms
1150- fig , ax = plt .subplots ()
1151- _ , _ , h = ax .hist (x , histtype = 'stepfilled' , color = 'r' , ec = 'b' ,
1152- label = "red stepfilled hist, blue edges, red label" )
1136+ h = ax .hist (x , histtype = 'stepfilled' , color = 'r' , ec = 'b' ,
1137+ label = "red stepfilled hist, blue edges, red label" )
11531138 leg = ax .legend (labelcolor = 'linecolor' )
1154- tc = leg .texts [0 ].get_color ()
1155- assert mpl .colors .same_color (tc , 'r' )
1156- assert mpl .colors .same_color (tc , leg .get_patches ()[0 ].get_facecolor ())
1157- assert mpl .colors .same_color (tc , h [0 ].get_facecolor ())
1139+ assert_last_legend_patch_color (h , leg , 'r' , facecolor = True )
11581140
1159- fig , ax = plt .subplots ()
1160- _ , _ , h = ax .hist (x , histtype = 'stepfilled' , fc = 'r' , ec = 'b' ,
1161- label = "red stepfilled hist, blue edges, red label" )
1141+ h = ax .hist (x , histtype = 'stepfilled' , fc = 'r' , ec = 'b' ,
1142+ label = "red stepfilled hist, blue edges, red label" )
11621143 leg = ax .legend (labelcolor = 'linecolor' )
1163- tc = leg .texts [0 ].get_color ()
1164- assert mpl .colors .same_color (tc , 'r' )
1165- assert mpl .colors .same_color (tc , leg .get_patches ()[0 ].get_facecolor ())
1166- assert mpl .colors .same_color (tc , h [0 ].get_facecolor ())
1144+ assert_last_legend_patch_color (h , leg , 'r' , facecolor = True )
11671145
1168- fig , ax = plt .subplots ()
1169- _ , _ , h = ax .hist (x , histtype = 'stepfilled' , fc = 'none' , ec = 'b' ,
1170- label = "unfilled blue stepfilled hist, blue label" )
1146+ h = ax .hist (x , histtype = 'stepfilled' , fc = 'none' , ec = 'b' ,
1147+ label = "unfilled blue stepfilled hist, blue label" )
11711148 leg = ax .legend (labelcolor = 'linecolor' )
1172- tc = leg .texts [0 ].get_color ()
1173- assert mpl .colors .same_color (tc , 'b' )
1174- assert mpl .colors .same_color (tc , leg .get_patches ()[0 ].get_edgecolor ())
1175- assert mpl .colors .same_color (tc , h [0 ].get_edgecolor ())
1149+ assert_last_legend_patch_color (h , leg , 'b' , edgecolor = True )
11761150
1177- fig , ax = plt .subplots ()
1178- _ , _ , h = ax .hist (x , histtype = 'stepfilled' , fc = 'r' , ec = 'none' ,
1179- label = "edgeless red stepfilled hist with a red label" )
1151+ h = ax .hist (x , histtype = 'stepfilled' , fc = 'r' , ec = 'none' ,
1152+ label = "edgeless red stepfilled hist with a red label" )
11801153 leg = ax .legend (labelcolor = 'linecolor' )
1181- tc = leg .texts [0 ].get_color ()
1182- assert mpl .colors .same_color (tc , 'r' )
1183- assert mpl .colors .same_color (tc , leg .get_patches ()[0 ].get_facecolor ())
1184- assert mpl .colors .same_color (tc , h [0 ].get_facecolor ())
1154+ assert_last_legend_patch_color (h , leg , 'r' , facecolor = True )
11851155 plt .close ('all' )
11861156
11871157
1158+ def assert_last_legend_linemarker_color (plot , leg , expected_color ,
1159+ color = False , facecolor = False , edgecolor = False ):
1160+ """
1161+ Check that line marker color, legend handle color, and legend label color all
1162+ match the expected input. Provide color, facecolor and edgecolor flags to clarify
1163+ which feature to match.
1164+ """
1165+ label_color = leg .texts [- 1 ].get_color ()
1166+ leg_marker = leg .get_lines ()[- 1 ]
1167+ plot_marker = plot [0 ]
1168+ assert mpl .colors .same_color (label_color , expected_color )
1169+ if color :
1170+ assert mpl .colors .same_color (label_color , leg_marker .get_color ())
1171+ assert mpl .colors .same_color (label_color , plot_marker .get_color ())
1172+ if facecolor :
1173+ assert mpl .colors .same_color (label_color , leg_marker .get_markerfacecolor ())
1174+ assert mpl .colors .same_color (label_color , plot_marker .get_markerfacecolor ())
1175+ if edgecolor :
1176+ assert mpl .colors .same_color (label_color , leg_marker .get_markeredgecolor ())
1177+ assert mpl .colors .same_color (label_color , plot_marker .get_markeredgecolor ())
1178+
1179+
11881180def test_legend_labelcolor_linecolor_plot ():
11891181 x = np .arange (5 )
11901182
11911183 # testing line plot
11921184 fig , ax = plt .subplots ()
11931185 p = ax .plot (x , c = 'r' , label = "red line with a red label" )
11941186 leg = ax .legend (labelcolor = 'linecolor' )
1195- tc = leg .texts [0 ].get_color ()
1196- assert mpl .colors .same_color (tc , 'r' )
1197- assert mpl .colors .same_color (tc , leg .get_lines ()[0 ].get_color ())
1198- assert mpl .colors .same_color (tc , p [0 ].get_color ())
1187+ assert_last_legend_linemarker_color (p , leg , 'r' , color = True )
11991188
12001189 # testing c, fc, and ec combinations for maker plots
1201- fig , ax = plt .subplots ()
12021190 p = ax .plot (x , 'o' , c = 'r' , label = "red circles with a red label" )
12031191 leg = ax .legend (labelcolor = 'linecolor' )
1204- tc = leg .texts [0 ].get_color ()
1205- assert mpl .colors .same_color (tc , 'r' )
1206- assert mpl .colors .same_color (tc , leg .get_lines ()[0 ].get_color ())
1207- assert mpl .colors .same_color (tc , p [0 ].get_color ())
1192+ assert_last_legend_linemarker_color (p , leg , 'r' , color = True )
12081193
1209- fig , ax = plt .subplots ()
12101194 p = ax .plot (x , 'o' , c = 'r' , mec = 'b' , label = "red circles, blue edges, red label" )
12111195 leg = ax .legend (labelcolor = 'linecolor' )
1212- tc = leg .texts [0 ].get_color ()
1213- assert mpl .colors .same_color (tc , 'r' )
1214- assert mpl .colors .same_color (tc , leg .get_lines ()[0 ].get_color ())
1215- assert mpl .colors .same_color (tc , p [0 ].get_color ())
1196+ assert_last_legend_linemarker_color (p , leg , 'r' , color = True )
12161197
1217- fig , ax = plt .subplots ()
12181198 p = ax .plot (x , 'o' , mfc = 'r' , mec = 'b' , label = "red circles, blue edges, red label" )
12191199 leg = ax .legend (labelcolor = 'linecolor' )
1220- tc = leg .texts [0 ].get_color ()
1221- assert mpl .colors .same_color (tc , 'r' )
1222- assert mpl .colors .same_color (tc , leg .get_lines ()[0 ].get_markerfacecolor ())
1223- assert mpl .colors .same_color (tc , p [0 ].get_markerfacecolor ())
1200+ assert_last_legend_linemarker_color (p , leg , 'r' , facecolor = True )
12241201
12251202 # 'none' cases
1226- fig , ax = plt .subplots ()
12271203 p = ax .plot (x , 'o' , mfc = 'none' , mec = 'b' , label = "blue unfilled circles, blue label" )
12281204 leg = ax .legend (labelcolor = 'linecolor' )
1229- tc = leg .texts [0 ].get_color ()
1230- assert mpl .colors .same_color (tc , 'b' )
1231- assert mpl .colors .same_color (tc , leg .get_lines ()[0 ].get_markeredgecolor ())
1232- assert mpl .colors .same_color (tc , p [0 ].get_markeredgecolor ())
1205+ assert_last_legend_linemarker_color (p , leg , 'b' , edgecolor = True )
12331206
1234- fig , ax = plt .subplots ()
12351207 p = ax .plot (x , 'o' , mfc = 'r' , mec = 'none' , label = "red edgeless circles, red label" )
12361208 leg = ax .legend (labelcolor = 'linecolor' )
1237- tc = leg .texts [0 ].get_color ()
1238- assert mpl .colors .same_color (tc , 'r' )
1239- assert mpl .colors .same_color (tc , leg .get_lines ()[0 ].get_markerfacecolor ())
1240- assert mpl .colors .same_color (tc , p [0 ].get_markerfacecolor ())
1209+ assert_last_legend_linemarker_color (p , leg , 'r' , facecolor = True )
12411210
1242- fig , ax = plt .subplots ()
12431211 p = ax .plot (x , 'o' , c = 'none' , mec = 'none' ,
12441212 label = "black label despite invisible circles for dummy entries" )
12451213 leg = ax .legend (labelcolor = 'linecolor' )
1246- tc = leg .texts [0 ].get_color ()
1247- assert mpl .colors .same_color (tc , 'k' )
1214+ assert_last_legend_linemarker_color (p , leg , 'k' )
12481215 plt .close ('all' )
12491216
12501217
1218+ def assert_last_legend_scattermarker_color (scatter_marker , leg , expected_color ,
1219+ facecolor = False , edgecolor = False ):
1220+ """
1221+ Check that scatter marker color, legend handle color, and legend label color all
1222+ match the expected input. Provide facecolor and edgecolor flags to clarify
1223+ which feature to match.
1224+ """
1225+ label_color = leg .texts [- 1 ].get_color ()
1226+ leg_handle = leg .legend_handles [- 1 ]
1227+ assert mpl .colors .same_color (label_color , expected_color )
1228+ if facecolor :
1229+ assert mpl .colors .same_color (label_color , leg_handle .get_facecolor ())
1230+ assert mpl .colors .same_color (label_color , scatter_marker .get_facecolor ())
1231+ if edgecolor :
1232+ assert mpl .colors .same_color (label_color , leg_handle .get_edgecolor ())
1233+ assert mpl .colors .same_color (label_color , scatter_marker .get_edgecolor ())
1234+
1235+
12511236def test_legend_labelcolor_linecolor_scatter ():
12521237 x = np .arange (5 )
12531238
12541239 # testing c, fc, and ec combinations for scatter plots
12551240 fig , ax = plt .subplots ()
12561241 p = ax .scatter (x , x , c = 'r' , label = "red circles with a red label" )
12571242 leg = ax .legend (labelcolor = 'linecolor' )
1258- tc = leg .texts [0 ].get_color ()
1259- assert mpl .colors .same_color (tc , 'r' )
1260- assert mpl .colors .same_color (tc , leg .legend_handles [0 ].get_facecolor ())
1261- assert mpl .colors .same_color (tc , p .get_facecolor ())
1243+ assert_last_legend_scattermarker_color (p , leg , 'r' , facecolor = True )
12621244
1263- fig , ax = plt .subplots ()
12641245 p = ax .scatter (x , x , c = 'r' , ec = 'b' , label = "red circles, blue edges, red label" )
12651246 leg = ax .legend (labelcolor = 'linecolor' )
1266- tc = leg .texts [0 ].get_color ()
1267- assert mpl .colors .same_color (tc , 'r' )
1268- assert mpl .colors .same_color (tc , leg .legend_handles [0 ].get_facecolor ())
1269- assert mpl .colors .same_color (tc , p .get_facecolor ())
1247+ assert_last_legend_scattermarker_color (p , leg , 'r' , facecolor = True )
12701248
1271- fig , ax = plt .subplots ()
12721249 p = ax .scatter (x , x , fc = 'r' , ec = 'b' , label = "red circles, blue edges, red label" )
12731250 leg = ax .legend (labelcolor = 'linecolor' )
1274- tc = leg .texts [0 ].get_color ()
1275- assert mpl .colors .same_color (tc , 'r' )
1276- assert mpl .colors .same_color (tc , leg .legend_handles [0 ].get_facecolor ())
1277- assert mpl .colors .same_color (tc , p .get_facecolor ())
1251+ assert_last_legend_scattermarker_color (p , leg , 'r' , facecolor = True )
12781252
12791253 # 'none' cases
1280- fig , ax = plt .subplots ()
12811254 p = ax .scatter (x , x , fc = 'none' , ec = 'b' , label = "blue unfilled circles, blue label" )
12821255 leg = ax .legend (labelcolor = 'linecolor' )
1283- tc = leg .texts [0 ].get_color ()
1284- assert mpl .colors .same_color (tc , 'b' )
1285- assert mpl .colors .same_color (tc , leg .legend_handles [0 ].get_edgecolor ())
1286- assert mpl .colors .same_color (tc , p .get_edgecolor ())
1256+ assert_last_legend_scattermarker_color (p , leg , 'b' , edgecolor = True )
12871257
1288- fig , ax = plt .subplots ()
12891258 p = ax .scatter (x , x , fc = 'r' , ec = 'none' , label = "red edgeless circles, red label" )
12901259 leg = ax .legend (labelcolor = 'linecolor' )
1291- tc = leg .texts [0 ].get_color ()
1292- assert mpl .colors .same_color (tc , 'r' )
1293- assert mpl .colors .same_color (tc , leg .legend_handles [0 ].get_facecolor ())
1294- assert mpl .colors .same_color (tc , p .get_facecolor ())
1260+ assert_last_legend_scattermarker_color (p , leg , 'r' , facecolor = True )
12951261
1296- fig , ax = plt .subplots ()
12971262 p = ax .scatter (x , x , c = 'none' , ec = 'none' ,
12981263 label = "black label despite invisible circles for dummy entries" )
12991264 leg = ax .legend (labelcolor = 'linecolor' )
1300- tc = leg .texts [0 ].get_color ()
1301- assert mpl .colors .same_color (tc , 'k' )
1265+ assert_last_legend_scattermarker_color (p , leg , 'k' )
13021266 plt .close ('all' )
13031267
13041268
0 commit comments