---
title: "Color Spectrum Plot"
---

### Description

Visualization of color palettes (given as hex codes) in HCL and/or
RGB coordinates.

As the hues for low-chroma colors are not (or poorly) identified, by
default a smoothing is applied to the hues (`fix = TRUE`). Also, to avoid
jumps from `0` to `360` or vice versa, the hue coordinates are shifted
suitably.

If argument `x` is a `maplotlib.colors.LinearSegmentedColormap` or
`matplotlib.colors.ListedColormap`, `256` distinct
colors across the color map are drawn and visualized.

### Usage

<pre><code class='language-python'>specplot(x, y=None, hcl=True, palette=True,<br/>         fix=True, rgb=False, title=None, fig=None,<br/>         **figargs)</code></pre>

### Arguments

<dl class="pyp-list param-list">
  <dt style = "white-space: nowrap; font-family: monospace; vertical-align: top">
   <code id="colorspace.specplot.specplot:x">x</code><code class="argument-class">list, LinearSegmentedColormap, ListedColormap</code>
  </dt>
 <dd>list of str (hex colors or
standard-names of colors) or a `matplotlib.colors.LinearSegmentedColormap`.</dd>
  <dt style = "white-space: nowrap; font-family: monospace; vertical-align: top">
   <code id="colorspace.specplot.specplot:y">y</code><code class="argument-class">None, list, LinearSegmentedColormap</code>
  </dt>
 <dd>if set it must be a list of
str (see `x`) with the very same length as the object provided on
argument `x` or a `maplotlib.colors.LinearSegmentedColormap`.
Allows to draw two sets of colors for comparison, defaults to `None`.</dd>
  <dt style = "white-space: nowrap; font-family: monospace; vertical-align: top">
   <code id="colorspace.specplot.specplot:hcl">hcl</code><code class="argument-class">bool</code>
  </dt>
 <dd>Whether or not to plot the HCL color spectrum.</dd>
  <dt style = "white-space: nowrap; font-family: monospace; vertical-align: top">
   <code id="colorspace.specplot.specplot:palette">palette</code><code class="argument-class">bool</code>
  </dt>
 <dd>Whether or not to plot the colors as a color map (color swatch).</dd>
  <dt style = "white-space: nowrap; font-family: monospace; vertical-align: top">
   <code id="colorspace.specplot.specplot:fix">fix</code><code class="argument-class">bool</code>
  </dt>
 <dd>Should the hues be fixed to be on a smooth(er) curve?
Details in the functions description.</dd>
  <dt style = "white-space: nowrap; font-family: monospace; vertical-align: top">
   <code id="colorspace.specplot.specplot:rgb">rgb</code><code class="argument-class">bool</code>
  </dt>
 <dd>Whether or not to plot the RGB color spectrum, defaults to `False`.</dd>
  <dt style = "white-space: nowrap; font-family: monospace; vertical-align: top">
   <code id="colorspace.specplot.specplot:title">title</code><code class="argument-class">None or str</code>
  </dt>
 <dd>title of the figure. Defaults to `None` (no title).</dd>
  <dt style = "white-space: nowrap; font-family: monospace; vertical-align: top">
   <code id="colorspace.specplot.specplot:fig">fig</code><code class="argument-class">None, matplotlib.figure.Figure</code>
  </dt>
 <dd>If `None`, a new
`matplotlib.figure.Figure` is created. </dd>
  <dt style = "white-space: nowrap; font-family: monospace; vertical-align: top">
   <code id="colorspace.specplot.specplot:**figargs">**figargs</code>
  </dt>
 <dd>forwarded to `matplotlib.pyplot.subplot`. Only has an effect
if `fig = None`.</dd>
</dl>

### Examples

```{python}
#| echo: true
#| warning: true
#| error: true
from colorspace import rainbow_hcl, diverging_hcl
from colorspace import specplot
pal = rainbow_hcl()
specplot(pal.colors(21));
```

```{python}
#| echo: true
#| warning: true
#| error: true
# Show spectrum in standard RGB space
specplot(pal.colors(21), rgb = True);
```

```{python}
#| echo: true
#| warning: true
#| error: true
# Reduced number of colors.
# Show sRGB spectrum, hide HCL spectrum
# and color palette swatch.
specplot(pal.colors(), rgb = True, hcl = False,
         palette = False, figsize = (8, 3));
```

```{python}
#| echo: true
#| warning: true
#| error: true
# Comparing full diverging_hcl() color spectrum to
# a LinearSegmentedColormap (cmap) with only 5 colors
# (an extreme example)
specplot(diverging_hcl("Green-Orange").colors(101),
         diverging_hcl("Green-Orange").cmap(5),
         rgb = True, figsize = (8, 3));
```

```{python}
#| echo: true
#| warning: true
#| error: true
# Same as above using .cmap() default with N = 256 colors
specplot(diverging_hcl("Green-Orange").colors(101),
         diverging_hcl("Green-Orange").cmap(),
         rgb = True, figsize = (8, 3));
```




### Raises

<ul class="python-raises">
<li><code class="text-warning">ImportError</code>: If `matplotlib` is not installed.
<li><code class="text-warning">TypeError</code>: If `x` is not list or `matplotlib.colors.LinearSegmentedColormap`.
<li><code class="text-warning">TypeError</code>: If `y` is neither a list nor `None`.
<li><code class="text-warning">ValueError</code>: If `x` contains str which can not be converted to hex colors.
<li><code class="text-warning">ValueError</code>: If `y` contains str which can not be converted to hex colors.
<li><code class="text-warning">ValueError</code>: If `y` is not the same length as `y`. Only checked if `y` is not `None`.
<li><code class="text-warning">TypeError</code>: If either `rgb`, `hcl`, or `palette` is not bool.
<li><code class="text-warning">ValueError</code>: If all, `rgb`, `hcl` and `palette` are set to `False` as this would
result in an empty plot.
<li><code class="text-warning">TypeError</code>: If &#x27;title&#x27; is neither `None` nor `str`.
</ul>

