Skip to content

Unregister_scale() function to matplotlib.scale#31547

Open
dikshajangra12918-oss wants to merge 20 commits intomatplotlib:mainfrom
dikshajangra12918-oss:add-unregister-scale
Open

Unregister_scale() function to matplotlib.scale#31547
dikshajangra12918-oss wants to merge 20 commits intomatplotlib:mainfrom
dikshajangra12918-oss:add-unregister-scale

Conversation

@dikshajangra12918-oss
Copy link
Copy Markdown
Contributor

PR Summary

Currently matplotlib has register_scale() but no way to remove a registered scale. This is a problem when a custom scale is created for just one plot — there is no way to clean up after that plot is drawn.

This PR adds unregister_scale(name) to fix that gap. Uses pop() for idiomatic dict cleanup.

Changes made:
~ Added unregister_scale() in lib/matplotlib/scale.py
~ Raises ValueError if scale name is not registered
~ Added tests in lib/matplotlib/tests/test_scale.py

Closes #5791

AI Disclosure

I used Claude (AI) for guidance on implementation approach. Code was written and verified by me.

PR Checklist

~ [x] Closes #5791 is in the PR description
~ [x] New code is tested

dikshajangra12918-oss and others added 3 commits April 21, 2026 15:25
Added unregister_scale(name) function that allows users 
to remove previously registered custom scales from the 
registry.

Raises ValueError if the scale name is not found.
Added two test cases in test_scale.py:

1. test_unregister_scale: registers a temporary custom scale and verifies it is removed correctly.

2. test_unregister_scale_invalid: verifies that ValueError is raised when trying to unregister a scale that doesn't exist.
@dikshajangra12918-oss dikshajangra12918-oss marked this pull request as ready for review April 22, 2026 18:05
Comment thread lib/matplotlib/scale.py
)


def unregister_scale(name):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def unregister_scale(name):
def deregister_scale(name):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I am late to this. While I agree that “deregister” seems like the better term in isolation, I noticed that the color map and color sequence registries both use “unregister”. So should we be consistent with those?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consistency is important. As non-native speaker I have no clear feeling what is more natural, but this is what AI says

Both are used in practice, but they carry slightly different connotations:

  • “unregister” is the more common choice in most APIs and codebases. It reads naturally as the inverse of register, and is widely used in standard libraries, frameworks, and event systems.
  • “deregister” is also correct English, but it’s less common in programming contexts and can feel a bit more formal or domain-specific (you’ll see it more in networking, telecom, or standards documents).

From a naming consistency and developer expectation standpoint, unregister is usually the better choice.

So it seems, we should go with unregister, which is also in line with ColormapRegistry.

Copy link
Copy Markdown
Member

@story645 story645 Apr 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I'll third this even though I'm the one who requested the change to deregister. Though really I think it'd be nicer to have an API more like the colormap registery where it's just register and unregister - ETA: which is a follow up discussion.
the action here is to change it back to unregister_scale.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While you could have a ScaleRegistry, it's much less useful than a ColormapRegistry. The primary (public) use of the colormap registry is for users to be able to get a colormap by name. But nobody needs to get a scale class from a name. So the only public API at all is register_scale, unregister_scale, and that's primarily for some third party libs. I wouldn't bother moving away from functions here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But nobody needs to get a scale class from a name

set_{x/y}scale but also I think norm_from_scale should support strings (am surprised it doesn't - and yes I'll open an issue if there isn't one already opened).

dikshajangra12918-oss and others added 2 commits April 23, 2026 05:10
@dikshajangra12918-oss
Copy link
Copy Markdown
Contributor Author

@story645,

The remaining failures are pre-existing issues with Qt backends and are unrelated to this PR.
All checks related to our changes are passing!

assert 'temp_test_scale' not in mscale._scale_mapping


def test_unregister_scale_invalid():
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deregister

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @story645!

I've updated the function and test names to use deregister_scale. Please taek a look.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still didn't catch everything,

Comment thread lib/matplotlib/tests/test_scale.py Outdated
assert isinstance(ax.xaxis.get_transform(), CustomTransform)
finally:
# cleanup - there's no public unregister_scale()
# cleanup - there's no public deregister_scale()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just use the method you just wrote?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@story645,

Could you help me understand what exactly still needs to be fixed? I've updated the function name, tests, and stub file. The remaining failures seem to be pre-existing issues but I want to make sure I haven't missed anything on my end.

Any guidance would be really helpful!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dikshajangra12918-oss the test that you added is failing
https://github.com/matplotlib/matplotlib/actions/runs/24874799863/job/72828932746?pr=31547#step:13:179

You should also consider the logic of having a comment that states "there's no public deregister_scale" when that is the very function that you are adding.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hii,

All requested changes are done:

  • Fixed TempScale to use IdentityTransfor
  • Fixed whitespace issues
  • Removed outdated comment

Our test_deregister_scale is passing.
The remaining failures are pre-existing platform issues unrelated to this PR.
Please let me know if anything else needs fixing!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our test_deregister_scale is passing.

That test is still not passing. Please read our guide on testing. In particular, it shows how to run a single test. I suggest you run your single test that way because you will then get an immediate result that is just about that test and not be confused whether other test failures are involved.
https://matplotlib.org/devdocs/devel/testing.html

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, please read through your change using the "files changed" tab above. The comments about there not being a deregister_scale are still there.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not able to understand why these 2 checks are failing- 'test_backened_getattr[backend_template]" and 'test_backend_getattr[backend_tkagg]'. Could you help me understand what's wrong here?

All checks related to our 'deregister_scale' changes are passing. Please let me know if anything else needs to be fixed!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rcomer @story645 , I did research on the failing tests on Windows Python 3.13, which seems to be a pre-existing platform issue unrelated to this PR. Could you please confirm if that's the case?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dikshajangra12918-oss yes that one is a known flaky test so not caused by this PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rcomer Thankyou for confirming! Is this ready for merging, orr are there any other changes needed?

Comment thread lib/matplotlib/scale.py

def deregister_scale(name):
"""
Remove a custom scale from the registry.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if someone tries to deregister a built in scale? What should happen?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tangently I'm wondering if we should add a deregister function to the colormap registry (or is .pop good enough since the registry itself is public?)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rcomer I think it should raise a "ValueError" to prevent accidental removal of built-in scales. I'll add that check to 'deregister_scale()'. Does that sound right?

@story645 That's good point! Should I address the colormap registry in this PR as well, or would it be better as a separate issue?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@story645 the colormap registry has https://matplotlib.org/stable/api/cm_api.html#matplotlib.cm.ColormapRegistry.unregister

@rcomer Thanks for the reference!
Following the same pattern as 'ColormapRegistry.unregister', I'll update 'deregister_scale()" to raise a 'ValueError' if someone tries to deregister a built-in scale like 'log', 'linear', etc.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's good point! Should I address the colormap registry in this PR as well, or would it be better as a separate issue?

As @rcomer pointed out, the function already exists for colormaps. I just missed it in the docs.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's good point! Should I address the colormap registry in this PR as well, or would it be better as a separate issue?

As @rcomer pointed out, the function already exists for colormaps. I just missed it in the docs.

Got it, thanks for clarifying!

@dikshajangra12918-oss
Copy link
Copy Markdown
Contributor Author

@story645 @rcomer AppyVeyor failure is unrelated to this PR - result_images directory not found error in visualize_tests.py on Windows CI

Comment thread lib/matplotlib/scale.py
name : str
The name of the scale to remove.
"""
_builtin_scales = {'linear', 'log', 'symlog', 'logit',
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this isn't defined anywhere else, I think this should be defined up top and then added to the scale registery. Use the pattern from

if name in self._builtin_cmaps:
raise ValueError(f"cannot unregister {name!r} which is a builtin "
"colormap.")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

No way to unregister a custom scale

4 participants