Custom Discrete Colormaps

Just wanted to share this too for MPL

import holoviews as hv
import pandas as pd
import matplotlib as mpl

df = pd.DataFrame(
    {
        "x": range(1, 9),
        "y": [1] * 8,
        "z": range(1, 9),
    }
)

hv.extension("matplotlib")

bounds = [1, 6, 7, 9]
cmap = mpl.colors.ListedColormap(["red", "green", "blue"])
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)

hv.Scatter(
    df,
    kdims=["x", "y"],
    vdims=["z"],
).opts(
    colorbar=True,
    cmap=["red", "green", "blue"],
    norm=norm,
    colorbar_opts={
        "ticks": bounds,
        "fraction": 0.6,
    },
    s=120,
    c="z",
)

Shameless plug and thank you very much Mr. @ahuang11

1 Like

hi, is there still no official support?

This issue should have been fixed already by Plotting with a discrete nonlinear colorbar assigns values to colors incorrectly · Issue #5966 · holoviz/holoviews · GitHub . But I did not try it myself with the bokeh backend. You are welcome to try it out and let us know if it works :slight_smile: .