FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

UserWarning: "Trying to register the cmap '...' which already exists" is not very helpful. · Issue #19842 · matplotlib/matplotlib · GitHub

Repository navigation

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

Closed

Description

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!

Activity

  1. timhoffm commented on Apr 2, 2021

    Member

    There's currently no recommended public way to check if a colormap name exists. A better interface to colormaps is on it's way; see #18503.

    For now you can only use your workarounds. Note that both versions are slightly different in that the first only checks for the builtlin colormaps, whereas the second would also include additional registered colormaps.

  2. tacaswell commented on Apr 2, 2021

    Member

    Did you mean to register them color maps multiple times? One issue with having a "register a string with us to mean something" is that if more than one package tries to register the same name the net result can be dependent on import order! e.g. if modules a and b both do mpl.register_cmap('the_best_cmap', ....) then

    import a
    import b
    ...
    plt.imshow(..., cmap='the_best_cmap')

    and

    import b
    import a
    ...
    plt.imshow(..., cmap='the_best_cmap')
    

    would do different things! It is even a bit worse than that because Python modules are process global, if other imports imported a and/or b then it might not matter what order you imported things in.

    Which is all a long way of saying we put the user warning in to surface exactly this conversation as to if you intended to register a cmap more than once! If so, why? If you find the name is already registered how can you be sure it is your version of the cmap not someone else?


    While there is value in being humble about your code, "lame" working code that ... works is better than "ideal code" which does not exist!

  3. added this to the v3.5.0 milestone on Apr 2, 2021
  4. akrherz commented on Apr 2, 2021

    ContributorAuthor

    Thanks to both of you for your thoughtful and helpful responses. My usage case is long running mod_python instances whereby my custom colormap registration methods could be called multiple times over the lifecycle of the process. I just was not keeping track of if I had already registered the cmap each time it was called. I am fine with closing this, if there's not action to take here with the upcoming new colormap interface changes.

  5. tacaswell commented on Apr 2, 2021

    Member

    You can also do things like

    from functools import lru_cache
    
    @lru_cache(1)
    def make_the_colormap():
        ...

    which will make use of the built in LRU cache to ensure that your functions are only ever run once per process even if they are called multiple times.

  6. timhoffm commented on Apr 2, 2021

    Member

    I'm closing this as we can't help other than with the new colormap registry (#18503), which is under it's way anyway.

    Also there are workarounds available for now.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL