How to create discrete colormap with n colors using Plotly
Asked Answered
S

3

10

I would like to create a colormap with n colors for a plotly express plot in Python, which should fade from one color into another. All the default colormaps only have 10 discrete values, but I am looking for a colormap with n > 10 discrete values.

>>> px.colors.sequential.Plasma_r

['#f0f921',
 '#fdca26',
 '#fb9f3a',
 '#ed7953',
 '#d8576b',
 '#bd3786',
 '#9c179e',
 '#7201a8',
 '#46039f',
 '#0d0887']

Is there a way to split a continuous map into n parts?

Stranger answered 22/6, 2021 at 9:57 Comment(5)
For the default colormap name, you can specify 10 colors or less by using slices. ex.px.colors.sequential.Plasma_r[:5]Huddle
But I am interested in more than 10 colors.Stranger
If you read carefully, it was more than ten. In that case, you can use color_dicreate_map() to specify more than 10 colors. Another approach would be to use seaborn's color palette to specify the desired number of colors. colors=set_palette("Reds", 24)Huddle
Here is an explanation of color_dicreate_map().Huddle
But if I am right you have to define the colors manually when using color_discrete_map and that's not option.Stranger
B
10

If you don't mind rgb colors, then n_colors is one way to go. Here's an example of 15 colors in a gray scale between 'rgb(0, 0, 0)' and 'rgb(255, 255, 255)':

 n_colors('rgb(0, 0, 0)', 'rgb(255, 255, 255)', 15, colortype='rgb')

enter image description here

And here's an example of 25 colors between blue 'rgb(0, 0, 255)'and red , 'rgb(255, 0, 0)' :

n_colors('rgb(0, 0, 255)', 'rgb(255, 0, 0)', 25, colortype = 'rgb')

enter image description here

Complete code:

import numpy as np
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px
import datetime
from plotly.colors import n_colors

pd.set_option('display.max_rows', None)
pd.options.plotting.backend = "plotly"


# greys15 = n_colors('rgb(0, 0, 0)', 'rgb(255, 255, 255)', 15, colortype='rgb')
redVSblue = n_colors('rgb(0, 0, 255)', 'rgb(255, 0, 0)', 25, colortype = 'rgb')

fig = go.Figure()

# for i, c in enumerate(greys15):
for i, c in enumerate(redVSblue):
    fig.add_bar(x=[i], y = [i+1], marker_color = c, showlegend = False)
f = fig.full_figure_for_development(warn=False)
fig.show()
Banquet answered 23/6, 2021 at 13:29 Comment(1)
This is nice solution if you looking for a scale. But what if you want for example 25 colors for lines in graph? It would be grate to have then distinguishable as possible. And it would be also nice to use buildin colorschemas for example continous "rainbow".Kure
K
12

Similar to accepted response but with ability to use builtin names that can be found in this tutorial

colors contains list of n_colors that can be trated as list.

import plotly.express as px

n_colors = 25
colors = px.colors.sample_colorscale("turbo", [n/(n_colors -1) for n in range(n_colors)])

Here is full example:

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

n_colors = 25
colors = px.colors.sample_colorscale("turbo", [n/(n_colors -1) for n in range(n_colors)])

fig = go.Figure()

# for i, c in enumerate(greys15):
for i, c in enumerate(colors):
    fig.add_bar(x=[i], y = [15], marker_color = c, showlegend = False, name=c)
f = fig.full_figure_for_development(warn=False)
fig.show()

colorscale

Kure answered 22/12, 2021 at 11:41 Comment(0)
B
10

If you don't mind rgb colors, then n_colors is one way to go. Here's an example of 15 colors in a gray scale between 'rgb(0, 0, 0)' and 'rgb(255, 255, 255)':

 n_colors('rgb(0, 0, 0)', 'rgb(255, 255, 255)', 15, colortype='rgb')

enter image description here

And here's an example of 25 colors between blue 'rgb(0, 0, 255)'and red , 'rgb(255, 0, 0)' :

n_colors('rgb(0, 0, 255)', 'rgb(255, 0, 0)', 25, colortype = 'rgb')

enter image description here

Complete code:

import numpy as np
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px
import datetime
from plotly.colors import n_colors

pd.set_option('display.max_rows', None)
pd.options.plotting.backend = "plotly"


# greys15 = n_colors('rgb(0, 0, 0)', 'rgb(255, 255, 255)', 15, colortype='rgb')
redVSblue = n_colors('rgb(0, 0, 255)', 'rgb(255, 0, 0)', 25, colortype = 'rgb')

fig = go.Figure()

# for i, c in enumerate(greys15):
for i, c in enumerate(redVSblue):
    fig.add_bar(x=[i], y = [i+1], marker_color = c, showlegend = False)
f = fig.full_figure_for_development(warn=False)
fig.show()
Banquet answered 23/6, 2021 at 13:29 Comment(1)
This is nice solution if you looking for a scale. But what if you want for example 25 colors for lines in graph? It would be grate to have then distinguishable as possible. And it would be also nice to use buildin colorschemas for example continous "rainbow".Kure
T
0

Even shorter:

import plotly.express as px

default_colorscale = px.colors.cyclical.Twilight

print(px.colors.sample_colorscale(default_colorscale, 20))


# output:
    # rgb(226, 217, 226)
    # rgb(194, 203, 214)
    # rgb(162, 189, 202)
    # rgb(135, 164, 196)
    # rgb(109, 139, 191)
    # rgb(100, 109, 181)
    # rgb(95, 77, 169)
    # rgb(85, 52, 140)
    # rgb(72, 28, 103)
    # rgb(67, 18, 81)
    # rgb(70, 19, 70)
    # rgb(86, 24, 67)
    # rgb(120, 36, 75)
    # rgb(149, 53, 81)
    # rgb(170, 81, 84)
    # rgb(188, 109, 93)
    # rgb(198, 143, 122)
    # rgb(207, 174, 152)
    # rgb(217, 196, 189)
    # rgb(226, 217, 226)
Tiruchirapalli answered 25/5 at 13:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.