Yield artists that can be used as handles in a legend.
(axs, legend_handler_map=None)
| 1260 | # Helper functions to parse legend arguments for both `figure.legend` and |
| 1261 | # `axes.legend`: |
| 1262 | def _get_legend_handles(axs, legend_handler_map=None): |
| 1263 | """Yield artists that can be used as handles in a legend.""" |
| 1264 | handles_original = [] |
| 1265 | for ax in axs: |
| 1266 | handles_original += [ |
| 1267 | *(a for a in ax._children |
| 1268 | if isinstance(a, (Line2D, Patch, Collection, Text))), |
| 1269 | *ax.containers] |
| 1270 | # support parasite Axes: |
| 1271 | if hasattr(ax, 'parasites'): |
| 1272 | for axx in ax.parasites: |
| 1273 | handles_original += [ |
| 1274 | *(a for a in axx._children |
| 1275 | if isinstance(a, (Line2D, Patch, Collection, Text))), |
| 1276 | *axx.containers] |
| 1277 | |
| 1278 | handler_map = {**Legend.get_default_handler_map(), |
| 1279 | **(legend_handler_map or {})} |
| 1280 | has_handler = Legend.get_legend_handler |
| 1281 | for handle in handles_original: |
| 1282 | label = handle.get_label() |
| 1283 | if label != '_nolegend_' and has_handler(handler_map, handle): |
| 1284 | yield handle |
| 1285 | elif (label and not label.startswith('_') and |
| 1286 | not has_handler(handler_map, handle)): |
| 1287 | _api.warn_external( |
| 1288 | "Legend does not support handles for " |
| 1289 | f"{type(handle).__name__} " |
| 1290 | "instances.\nSee: https://matplotlib.org/stable/" |
| 1291 | "tutorials/intermediate/legend_guide.html" |
| 1292 | "#implementing-a-custom-legend-handler") |
| 1293 | continue |
| 1294 | |
| 1295 | |
| 1296 | def _get_legend_handles_labels(axs, legend_handler_map=None): |
no test coverage detected
searching dependent graphs…