Skip to content
Open
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
15 changes: 15 additions & 0 deletions lib/matplotlib/scale.py
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,21 @@ def register_scale(scale_class):
)


def deregister_scale(name):
"""
Remove a custom scale from the registry.

Parameters
----------
name : str
The name of the scale to remove.
"""
if name not in _scale_mapping:
raise ValueError(f"Scale '{name}' is not registered.")
_scale_mapping.pop(name)
_scale_has_axis_parameter.pop(name, None)


def _get_scale_docs():
"""
Helper function for generating docstrings related to scales.
Expand Down
1 change: 1 addition & 0 deletions lib/matplotlib/scale.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,5 @@ class LogitScale(ScaleBase):
def get_scale_names() -> list[str]: ...
def scale_factory(scale: str, axis: Axis, **kwargs) -> ScaleBase: ...
def register_scale(scale_class: type[ScaleBase]) -> None: ...
def deregister_scale(name: str) -> None: ...
def _make_axis_parameter_optional(init_func: Callable[..., None]) -> Callable[..., None]: ...
39 changes: 33 additions & 6 deletions lib/matplotlib/tests/test_scale.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,7 @@ def set_default_locators_and_formatters(self, axis):
ax.set_xscale('custom')
assert isinstance(ax.xaxis.get_transform(), CustomTransform)
finally:
# cleanup - there's no public unregister_scale()
del mscale._scale_mapping["custom"]
del mscale._scale_has_axis_parameter["custom"]
mscale.deregister_scale("custom")


def test_custom_scale_with_axis():
Expand Down Expand Up @@ -368,9 +366,7 @@ def set_default_locators_and_formatters(self, axis):
ax.set_xscale('custom')
assert isinstance(ax.xaxis.get_transform(), CustomTransform)
finally:
# cleanup - there's no public unregister_scale()
del mscale._scale_mapping["custom"]
del mscale._scale_has_axis_parameter["custom"]
mscale.deregister_scale("custom")


def test_val_in_range():
Expand Down Expand Up @@ -433,3 +429,34 @@ def test_val_in_range_base_fallback():
assert s.val_in_range(np.nan) is False
assert s.val_in_range(np.inf) is False
assert s.val_in_range(-np.inf) is False


def test_deregister_scale():
"""Test that deregister_scale removes a scale correctly."""
# Register a temporary custom scale
class TempScale(mscale.ScaleBase):
name = 'temp_test_scale'

def __init__(self):
pass

def get_transform(self):
from matplotlib.transforms import IdentityTransform
return IdentityTransform()

def set_default_locators_and_formatters(
self, axis):
pass

mscale.register_scale(TempScale)
assert 'temp_test_scale' in mscale._scale_mapping

# Now unregister it
mscale.deregister_scale('temp_test_scale')
assert 'temp_test_scale' not in mscale._scale_mapping


def test_deregister_scale_invalid():
"""Test that deregister_scale raises ValueError for unknown scale."""
with pytest.raises(ValueError, match="not registered"):
mscale.deregister_scale('this_does_not_exist')
Loading