Discrete colorbar with customized range

Hi @sry89 welcome to the forums.

What I came up with is the following: bin values into “color clusters” and use these “color clusters” afterwards to assign a color via the chosen colorscale. Maybe it’s not the most elegant way to do it, but this might work for you.

import plotly.graph_objects as go
import plotly.express as px
import numpy as np

# define discrete colorscale
colorscale = px.colors.qualitative.Dark2

# custom colorscale
#colorscale = ['red', 'green', 'pink', 'blue', 'black']

# set ranges
ranges = [
    [0, 10], 
    [11, 20], 
    [21, 50], 
    [51, 300], 
    [301, 1000]
]

# create dummy data
x = np.random.randint(0,50,500)
y = np.random.randint(0,999,500)

# bin values into color clusters
def bin_colors(value):
    for color, r in enumerate(ranges):
        if r[0] <= value <= r[1]:
            return color

color_cluster = list(map(bin_colors, y))

# create figure
fig = go.Figure()
fig.add_scatter(
    x=x, 
    y=y, 
    mode='markers', 
    marker={
        'color': color_cluster, 
        'colorscale': colorscale
    }
)
fig.update_yaxes(type="log")

Which produces: