Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lib/matplotlib/artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,23 @@ def get_window_extent(self, renderer=None):
"""
return Bbox([[0, 0], [0, 0]])

def _outside_axes_domain(self, x, y):
Comment thread
scottshambaugh marked this conversation as resolved.
Outdated
"""
Check if the data point (x, y) is outside the valid domain of the axes
scales.

Returns True if the artist is in an Axes but the point is outside its
data range (eg. negative coordinates with a log scale).
"""
ax = self.axes
if ax is None:
return False
for val, axis in [(x, ax.xaxis), (y, ax.yaxis)]:
vmin, vmax = axis.limit_range_for_scale(val, val)
if vmin != val or vmax != val:
Comment thread
scottshambaugh marked this conversation as resolved.
Outdated
return True
return False

def get_tightbbox(self, renderer=None):
"""
Get the artist's bounding box in display space, taking clipping into account.
Expand Down
12 changes: 12 additions & 0 deletions lib/matplotlib/tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -1226,3 +1226,15 @@ def test_ytick_rotation_mode():
tick.set_rotation(angle)

plt.subplots_adjust(left=0.4, right=0.6, top=.99, bottom=.01)


def test_text_tightbbox_outside_scale_domain():
# Test that text at positions outside the valid domain of axes scales
# (e.g., negative coordinates with log scale) returns a null bbox.
fig, ax = plt.subplots()
ax.set_yscale('log')
ax.set_ylim(1, 100)

invalid_text = ax.text(0, -5, 'invalid')
invalid_bbox = invalid_text.get_tightbbox(fig.canvas.get_renderer())
assert not np.isfinite(invalid_bbox.width)
8 changes: 8 additions & 0 deletions lib/matplotlib/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,14 @@ def get_window_extent(self, renderer=None, dpi=None):
bbox = bbox.translated(x, y)
return bbox

def get_tightbbox(self, renderer=None):
# Exclude text at data coordinates outside the valid domain of the axes
Comment thread
scottshambaugh marked this conversation as resolved.
# scales (e.g., negative coordinates with a log scale).
if (self._outside_axes_domain(*self.get_unitless_position())
and self.get_transform() == self.axes.transData):
return Bbox.null()
Comment thread
scottshambaugh marked this conversation as resolved.
Outdated
return super().get_tightbbox(renderer)

def set_backgroundcolor(self, color):
"""
Set the background color of the text.
Expand Down
Loading