7

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?

vestland
  • 55,229
  • 37
  • 187
  • 305
N8_Coder
  • 713
  • 3
  • 10
  • 20
  • For the default colormap name, you can specify 10 colors or less by using slices. ex.`px.colors.sequential.Plasma_r[:5]` – r-beginners Jun 22 '21 at 11:18
  • But I am interested in more than 10 colors. – N8_Coder Jun 22 '21 at 12:27
  • 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)` – r-beginners Jun 22 '21 at 12:53
  • [Here](https://plotly.com/python/discrete-color/#controlling-discrete-color-order) is an explanation of color_dicreate_map(). – r-beginners Jun 22 '21 at 12:55
  • But if I am right you have to define the colors manually when using `color_discrete_map` and that's not option. – N8_Coder Jun 22 '21 at 14:57

2 Answers2

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()
vestland
  • 55,229
  • 37
  • 187
  • 305
  • 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". – S.R Dec 22 '21 at 11:09
7

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

S.R
  • 2,411
  • 1
  • 22
  • 33