Repository: arviz-devs/arviz

Issue:
ValueError: A colormap named "cet_gray" is already registered
#### Software version info

colorcet 3.0.1 arviz 0.12.1 python 3.10/8 matplotlib 3.6.0/1
afaik introduced with recent colorcet/matplotlib release

#### Description of expected behavior and the observed behavior

When multiple libraries use `mpl.colormaps.register(cmap, name=cmap_name)` with the same `cmap_name`, a `ValueError: A colormap named "cet_gray" is already registered.` is raised. This happens e.g. for colorcet + arviz, both of which define cet_gray (see [here](https://github.com/arviz-devs/arviz/blob/main/arviz/__init__.py#L321) for arviz, [here](https://github.com/holoviz/colorcet/blob/master/colorcet/__init__.py) for colorcet).

Arviz appears here to emulate the colorcet color.

Is this an issue/responsibility of colorcet/arviz/...?

#### Complete, minimal, self-contained example code that reproduces the issue

```python
import arviz
import colorcet
```

or
```python
import colorcet
import arviz
```

This prohibits the use of both simultaneously.

#### Stacktrace

 ```
 Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/yannik/env/env/lib/python3.10/site-packages/arviz/__init__.py", line 320, in <module>
    _mpl_cm("gray", _linear_grey_10_95_c0)
  File "/home/yannik/env/env/lib/python3.10/site-packages/arviz/__init__.py", line 317, in _mpl_cm
    register_cmap("cet_" + name, cmap=cmap)
  File "/home/yannik/env/env/lib/python3.10/site-packages/matplotlib/_api/deprecation.py", line 200, in wrapper
    return func(*args, **kwargs)
  File "/home/yannik/env/env/lib/python3.10/site-packages/matplotlib/cm.py", line 248, in register_cmap
    _colormaps.register(cmap, name=name, force=override_builtin)
  File "/home/yannik/env/env/lib/python3.10/site-packages/matplotlib/cm.py", line 149, in register
    raise ValueError(
ValueError: A colormap named "cet_gray" is already registered.
```


Discussion:
Maybe we can add a check and only register the cmap if colorcet is not installed? (so that both import orders work as opposed to checking if the cmap is registered and requiring arviz to be imported second) cc @aloctavodia 
Maybe wrapping in a `try/except ValueError` right [here](https://github.com/arviz-devs/arviz/blob/main/arviz/__init__.py#L318)? It doesn't "commute" with import order, but I'll pop over to the `colorcet` side and see if maybe that should just be a best practice.


Why do we use same name as colorcet?
It is the same colorscale, so it makes sense from that perspective. 
Sounds like they are suggesting something similar to you, @OriolAbril ! https://github.com/holoviz/colorcet/issues/96#issuecomment-1276576516

Resolving patch:
diff --git a/arviz/__init__.py b/arviz/__init__.py
--- a/arviz/__init__.py
+++ b/arviz/__init__.py
@@ -318,8 +318,11 @@ def _mpl_cm(name, colorlist):
     mpl.colormaps.register(cmap, name="cet_" + name)
 
 
-_mpl_cm("gray", _linear_grey_10_95_c0)
-_mpl_cm("gray_r", list(reversed(_linear_grey_10_95_c0)))
+try:
+    import colorcet
+except ModuleNotFoundError:
+    _mpl_cm("gray", _linear_grey_10_95_c0)
+    _mpl_cm("gray_r", list(reversed(_linear_grey_10_95_c0)))
 
 
 # clean namespace


Test patch: