Limits of matplotlib's 'best' legend location for reproducible figures
0 reputation · 30 Jul 2020, 22:01 UTC
Goal
Ensure that a legend placed with loc='best' remains in the same corner after interactive resizing or when saving to different file formats, so that figures are reproducible across environments.
Constraints and uncertainty
The scoring algorithm that determines the 'best' location resides in the private _legend_box.py module and is not documented in the public API. As a result, its exact overlap metrics and decision thresholds are unknown, and the behavior may change between minor releases or differ with various backends (e.g., GTK, TkAgg, notebook). This opacity makes it impossible to guarantee deterministic placement without manually fixing the location.
Specific questions:
- What quantitative overlap measures (e.g., axis‑data bounding box intersection, whitespace penalty) does the 'best' location scoring function evaluate in matplotlib 3.x?
- Is there a public way to query or reproduce the internal score for a given axes configuration so that users can enforce deterministic placement?
- Does explicitly setting
bbox_to_anchorto a fixed normalized coordinate guarantee identical legend positions across different figure sizes, backends, and output formats?