BUG: Fix text appearing far outside valid axis scale range#31061
BUG: Fix text appearing far outside valid axis scale range#31061scottshambaugh wants to merge 5 commits intomatplotlib:mainfrom
Conversation
0d3ef72 to
7ef9295
Compare
7ef9295 to
4ffb8d3
Compare
| return bbox | ||
|
|
||
| def get_tightbbox(self, renderer=None): | ||
| # Exclude text at data coordinates outside the valid domain of the axes |
There was a problem hiding this comment.
if not self.get_visible() or self.get_text() == "":
return Bbox.null()I had originally included these lines, but they were changing a half dozen baseline images as plots expanded to fill space where there was empty/invisible text. Removed for now, but I think that's probably the right behavior and we can discuss including it.
There was a problem hiding this comment.
If you think that might be important, you can target that change to the text-overhaul branch.
There was a problem hiding this comment.
Do you mean this PR or just that change?
There was a problem hiding this comment.
The additional change, unless it's easier for you to do the whole PR.
There was a problem hiding this comment.
Sure, I'll wait for this one to merge to avoid the def get_tightbbox conflict
b558152 to
496411d
Compare
496411d to
20103e7
Compare
|
Waiting on #30161. Per #31061 (comment) |
|
This one can merge as-is, then the change discussed above that would regenerate baseline images would be in a separate PR that targets #30161. |
lib/matplotlib/artist.py
Outdated
| vmin, vmax = axis.limit_range_for_scale(val, val) | ||
| if vmin != val or vmax != val: |
There was a problem hiding this comment.
Should we grow a method Scale.val_in_range(val). This seems like a logically meaningful API. ScaleBase could implement this as a generic fallback via limit_range_for_scale to support existing third-party scales. But specific scales could implement their logic much more efficiently; e.g. LinearScale would just return True, and LogScale would return val > 0`.
If we do this, probably better in a separate PR.
There was a problem hiding this comment.
Yeah, I agree with this follow-on
Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com>
lib/matplotlib/artist.py
Outdated
| """ | ||
| return Bbox([[0, 0], [0, 0]]) | ||
|
|
||
| def _outside_axes_domain(self, x, y): |
There was a problem hiding this comment.
Architectural reconsideration:
As written this is rather an Axes functionality than an Artist functionality.
The only self usage is self.axes, and we needed to explicitly inverted the semantics to _outside_axes_domain to reasonably cover the case of artists that are not attached to axes.
OTOH below we use this only in the context of an axes:
if (self.axes and self.get_transform() == self.axes.transData
and self._outside_axes_domain(*self.get_unitless_position())):So we could turn this into an Axes method
def _point_in_data_domain(self, x, y):and call it via
if (self.axes and self.get_transform() == self.axes.transData
and not self.axes._point_in_data_domain(*self.get_unitless_position())):There was a problem hiding this comment.
I don't think we'd ever have an artist without an axes, but this ownership boundary makes sense. Updated
| self.stale = True | ||
| return txt | ||
|
|
||
| def _point_in_data_domain(self, x, y): |
There was a problem hiding this comment.
Is this not going to be used in 3D at some point (i.e., should it be passing in a tuple instead of individual coordinates)?
There was a problem hiding this comment.
This isn't currently called in any 3d path, since the rendering pipeline is so different. We might want to use it for something else, but I'm not too worried about future-proofing internal private functions beforehand.
PR summary
Closes #31054
Textdidn't have aget_tightbboxoverride yet, so this adds that with some handling for no-show cases. The_in_axes_domaincheck is not exclusive to text and may be useful elsewhere, so was given toArtistmore broadly.The plot in the original issue renders correctly now, and I can confirm it was broken for me before:
PR checklist