Skip to content

Backport PR #25920 on branch v3.7.x (Rewrite offset_copy for better error message) #25928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions lib/matplotlib/tests/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,3 +744,14 @@ def test_scale_swapping(fig_test, fig_ref):
ax.plot(x, np.exp(-(x**2) / 2) / np.sqrt(2 * np.pi))
fig.canvas.draw()
ax.set_yscale('linear')


def test_offset_copy_errors():
with pytest.raises(ValueError,
match="'fontsize' is not a valid value for units;"
" supported values are 'dots', 'points', 'inches'"):
mtransforms.offset_copy(None, units='fontsize')

with pytest.raises(ValueError,
match='For units of inches or points a fig kwarg is needed'):
mtransforms.offset_copy(None, units='inches')
6 changes: 2 additions & 4 deletions lib/matplotlib/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2968,15 +2968,13 @@ def offset_copy(trans, fig=None, x=0.0, y=0.0, units='inches'):
`Transform` subclass
Transform with applied offset.
"""
_api.check_in_list(['dots', 'points', 'inches'], units=units)
if units == 'dots':
return trans + Affine2D().translate(x, y)
if fig is None:
raise ValueError('For units of inches or points a fig kwarg is needed')
if units == 'points':
x /= 72.0
y /= 72.0
elif units == 'inches':
pass
else:
_api.check_in_list(['dots', 'points', 'inches'], units=units)
# Default units are 'inches'
return trans + ScaledTranslation(x, y, fig.dpi_scale_trans)