🌐 AI搜索 & 代理 主页
Skip to content

UserWarning: "Trying to register the cmap '...' which already exists" is not very helpful. #19842

@akrherz

Description

@akrherz

Describe the issue

Matplotlib 3.4 introduces a UserWarning with 1a9dacf when attempting to register a color map by the same name twice. So for a lame luser like myself, how am I to know if my colormap name has already been registered? Is there public API to check if a given cmap name exists?

Proposed fix

Presently, I only see this option via "private" API to check this situation

from matplotlib import cm

def cmap_exists(name):
     return name in cm.__builtin_cmaps

Otherwise, there would be this lame workaround

from matplotlib import cm

def cmap_exists(name):
    try:
         cm.get_cmap(name)
         return True
    except ValueError:
         pass
    return False

Hopefully I am not missing something obvious. If something exists, it would be nice to enhance the UserWarning by saying "Trying to register the cmap {name!r} which already exists. Please first check presence of cmap by calling cm.cmap_exists(name)"

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions