Skip to content

Allow user to not interpolate colours #207

Description

@gerritholl

Feature Request

Is your feature request related to a problem? Please describe.

In data visualisation, it is often appropriate to limit the number of data classes. This makes it easier for the user to read data values. For example, colorbrewer recommends to use between 3 and 9 data classes. But in trollimage, if we apply a colormap with a small number of data classes, it interpolates between the colours:

import numpy as np
import matplotlib.colors
import matplotlib.pyplot as plt
import xarray as xr
from trollimage.colormap import Colormap
from trollimage.xrimage import XRImage
my_cmap = matplotlib.colors.ListedColormap([[1, 1, .8], [.8, .9, .75], [.75, .9, .5], [.5, .85, .3], [0, 0.5, 0.2]])
data = np.linspace(0.4, 1, 500*500).reshape(500, 500)
ax = plt.axes()
plt.imshow(data, cmap=my_cmap)
plt.savefig("image-mpl.png")

tcm_rh=Colormap(colors=my_cmap.colors, values=np.linspace(0.4, 1, len(my_cmap.colors)))
im = XRImage(xr.DataArray(data, dims=("y", "x")))
im.colorize(tcm_rh)
im.rio_save("image-trollimage.png")

Output matplotlib:

Image

Output trollimage:

Image

The decision to interpolate is hardcoded in trollimage and cannot currently be disabled:

def _colorize(arr, colors, values):
"""Colorize the array."""
channels = _interpolate_rgb_colors(arr, colors, values)
alpha = _interpolate_alpha(arr, colors, values)
channels.extend(alpha)
channels = _mask_channels(channels, arr)
return np.stack(channels, axis=0)

Describe the solution you'd like

I would like that trollimage allows to not interpolate the colors in a colormap.

Describe any changes to existing user workflow

The default would be the status quo.

Additional context

A workaround would be to create a colormap where colours are repeated for different values.

Activity

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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions