---
title: "Create Matplotlib Compatible Color Map"
---

### Description

Allows to retrieve a matplotlib LinearSegmentedColormap color map.
Clasically LinearSegmentedColormaps allow to retrieve a set of `N`
colors from a set of `n` colors where `N >> n`. The matplotlib
simply linearely interpolates between all `n` colors to extend
the number of colors to `N`.

In case of `hclpalette` objects this is not necessary as
`hclpalette` objects allow to retrieve `N` colors directly
along well-specified Hue-Chroma-Luminance paths. Thus, this method
returns a matplotlib color map with `n = N` colors. The linear 
interpolation between the colors (as typically done by
LinearSegmentedColormap) is not necessary. However, for convenience
cmaps have been implemented such that you can easily use hcl based
palettes in your existing workflow.

### Usage

<pre><code class='language-python'>rainbow_hcl.cmap(n=256,<br/>                 name=&#x27;custom_hcl_cmap&#x27;)</code></pre>

### Arguments

<dl class="pyp-list param-list">
  <dt style = "white-space: nowrap; font-family: monospace; vertical-align: top">
   <code id="colorspace.palettes.rainbow_hcl.cmap:n">n</code><code class="argument-class">int</code>
  </dt>
 <dd>Number of colors the cmap should be based on; default is `n = 256`</dd>
  <dt style = "white-space: nowrap; font-family: monospace; vertical-align: top">
   <code id="colorspace.palettes.rainbow_hcl.cmap:name">name</code><code class="argument-class">str</code>
  </dt>
 <dd>Name of the custom color map. Default is `custom_hcl_cmap`</dd>
</dl>

### Return

Returns a `LinearSegmentedColormap` (cmap) to be used
with the matplotlib library.

### Examples

```{python}
#| echo: true
#| warning: true
#| error: true
# Create LinearSegmentedColormap from diverging_hcl() palette.
# By default, 256 distinct colors are used across the palette.
from colorspace import diverging_hcl, specplot
pal = diverging_hcl()
cmap1 = pal.cmap()
cmap1.N
```

```{python}
#| echo: true
#| warning: true
#| error: true
# Same as above, but only using 5 distinct colors.
cmap2 = pal.cmap(n = 5)
cmap2.N
```

```{python}
#| echo: true
#| warning: true
#| error: true
# Plotting HCL and sRGB spectrum for both cmaps
specplot(cmap1, rgb = True, figsize = (8, 6));
```

```{python}
#| echo: true
#| warning: true
#| error: true
specplot(cmap2, rgb = True, figsize = (8, 6));
```




### Raises

<ul class="python-raises">
<li><code class="text-warning">TypeError</code>: If `n` is not int
<li><code class="text-warning">ValueError</code>: If `n` is lower than 2
</ul>

