# Fix range of colormap for hvplot.image with rasterize=True

**URL:** <https://discourse.holoviz.org/t/fix-range-of-colormap-for-hvplot-image-with-rasterize-true/1726>\
**Category:** Datashader\
**Created:** [January 15, 2021, 2:12am UTC](https://discourse.holoviz.org/t/fix-range-of-colormap-for-hvplot-image-with-rasterize-true/1726 "2021-01-15T02:12:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![TomAugspurger](https://discourse.holoviz.org/user_avatar/discourse.holoviz.org/tomaugspurger/32/1611_2.png) [@TomAugspurger](https://discourse.holoviz.org/u/TomAugspurger)\
**Post date:** [January 15, 2021, 2:12am UTC](https://discourse.holoviz.org/t/fix-range-of-colormap-for-hvplot-image-with-rasterize-true/1726/1 "2021-01-15T02:12:58Z")

</div>

Hi all,

I’m trying to display some discrete raster data. The values are 0 - 9, and represent the land use classification for that pixel.

For large datasets, I’m using `.hvplot.image(rasterize=True, aggregate="mode")`. But when I pan / zoom the output and (I assume datashader) recomputes the rasterized output, the colormap will be recomputed based on the values present in the selected region. This causes the color associated with a value `5`, say, to switch.

Here’s a dummy example that hopefully demonstrates the same problem:

```python
import xarray as xr
import numpy as np
import hvplot.xarray

x1 = np.random.randint(0, 5, size=(500, 1000))
x2 = np.random.randint(5, 10, size=(500, 1000))

ds = xr.DataArray(np.concatenate([x1, x2]), dims=['x', 'y'],
                  coords={"x": np.arange(1000), "y": np.arange(1000)})

ds.hvplot.image(cmap="inferno", rasterize=True, aggregator="mode")

```

 ![image](https://discourse.holoviz.org/uploads/default/original/2X/3/360e69671cae92de3553a3bd83365faed21658df.png)

The colorbar ranges from 0 - 9. But when I zoom in on just the top half (so the lowest value is now 5, not 0), the colors are reassigned

(image redacted since discourse only lets me have one per post. I’ll move it to a comment.)

So now a value of 5 is given the color that was previously 0.

Is there a way to “fix” the range of the colormap, so that the range is always 0 - 9? I _think_ I want something like the `span` argument to `tf.shade` mentioned at [Pipeline — Datashader 0.12.0 documentation](https://datashader.org/getting_started/Pipeline.html), but I don’t see a way to pass that through `.hvplot.image`.

---

<div class="post-metadata">

**Author:** ![TomAugspurger](https://discourse.holoviz.org/user_avatar/discourse.holoviz.org/tomaugspurger/32/1611_2.png) [@TomAugspurger](https://discourse.holoviz.org/u/TomAugspurger)\
**Post date:** [January 15, 2021, 2:13am UTC](https://discourse.holoviz.org/t/fix-range-of-colormap-for-hvplot-image-with-rasterize-true/1726/2 "2021-01-15T02:13:34Z")

</div>

Here’s the zoomed-in image that belonged in the first post:

 ![image](https://discourse.holoviz.org/uploads/default/original/2X/b/b45333ef735858134df7d90fd191fb1920377aef.png)

---

<div class="post-metadata">

**Author:** ![TomAugspurger](https://discourse.holoviz.org/user_avatar/discourse.holoviz.org/tomaugspurger/32/1611_2.png) [@TomAugspurger](https://discourse.holoviz.org/u/TomAugspurger)\
**Post date:** [January 15, 2021, 2:21am UTC](https://discourse.holoviz.org/t/fix-range-of-colormap-for-hvplot-image-with-rasterize-true/1726/3 "2021-01-15T02:21:10Z")

</div>

Hmm, I maybe should have searched a little harder. Philip’s answer at [python - How do I manually set the limits of a holoview’s colorbar? - Stack Overflow](https://stackoverflow.com/questions/48653508/how-do-i-manually-set-the-limits-of-a-holoviews-colorbar) suggests an option, which seems to work (at least on my dummy example. Trying it on the real example later).

```python
o = ds.hvplot.image(cmap="inferno", rasterize=True, aggregator="mode")
o.redim.range(value=(0, 9))

```

I think my earlier searches were too focused on hvplot and datashader, when holoviews was the layer I needed…

---

<div class="post-metadata">

**Author:** ![philippjfr](https://discourse.holoviz.org/user_avatar/discourse.holoviz.org/philippjfr/32/7_2.png) [@philippjfr](https://discourse.holoviz.org/u/philippjfr)\
**Post date:** [January 18, 2021, 4:19pm UTC](https://discourse.holoviz.org/t/fix-range-of-colormap-for-hvplot-image-with-rasterize-true/1726/4 "2021-01-18T16:19:49Z")

</div>

You should also be able to use the `clim` keyword argument in hvplot now.
