You may use the HSV system to obtain differently saturated and luminated colors for the same hue. Suppose you have at most 10 categories, then the tab10 map can be used to get a certain number of base colors. From those you can choose a couple of lighter shades for the subcategories.

The following would be a function categorical_cmap, which takes as input the number of categories (nc) and the number of subcategories (nsc) and returns a colormap with nc*nsc different colors, where for each category there are nsc colors of same hue.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors

def categorical_cmap(nc, nsc, cmap="tab10", continuous=False):
    if nc > plt.get_cmap(cmap).N:
        raise ValueError("Too many categories for colormap.")
    if continuous:
        ccolors = plt.get_cmap(cmap)(np.linspace(0,1,nc))
    else:
        ccolors = plt.get_cmap(cmap)(np.arange(nc, dtype=int))
    cols = np.zeros((nc*nsc, 3))
    for i, c in enumerate(ccolors):
        chsv = matplotlib.colors.rgb_to_hsv(c[:3])
        arhsv = np.tile(chsv,nsc).reshape(nsc,3)
        arhsv[:,1] = np.linspace(chsv[1],0.25,nsc)
        arhsv[:,2] = np.linspace(chsv[2],1,nsc)
        rgb = matplotlib.colors.hsv_to_rgb(arhsv)
        cols[i*nsc:(i+1)*nsc,:] = rgb       
    cmap = matplotlib.colors.ListedColormap(cols)
    return cmap

c1 = categorical_cmap(4, 3, cmap="tab10")
plt.scatter(np.arange(4*3),np.ones(4*3)+1, c=np.arange(4*3), s=180, cmap=c1)

c2 = categorical_cmap(2, 5, cmap="tab10")
plt.scatter(np.arange(10),np.ones(10), c=np.arange(10), s=180, cmap=c2)

c3 = categorical_cmap(5, 4, cmap="tab10")
plt.scatter(np.arange(20),np.ones(20)-1, c=np.arange(20), s=180, cmap=c3)    

plt.margins(y=0.3)
plt.xticks([])
plt.yticks([0,1,2],["(5, 4)", "(2, 5)", "(4, 3)"])
plt.show()

Answer from ImportanceOfBeingErnest on Stack Overflow
🌐
Matplotlib
matplotlib.org › stable › users › explain › colors › colormaps.html
Choosing Colormaps in Matplotlib — Matplotlib 3.11.2 documentation
plot_color_gradients('Qualitative', ['Pastel1', 'Pastel2', 'Paired', 'Accent', 'okabe_ito', 'Dark2', 'Set1', 'Set2', 'Set3', 'tab10', 'tab20', 'tab20b', 'tab20c'])
🌐
Matplotlib
matplotlib.org › stable › gallery › color › colormap_reference.html
Colormap reference — Matplotlib 3.11.2 documentation
import matplotlib.pyplot as plt ... 'hsv']), ('Qualitative', [ 'Pastel1', 'Pastel2', 'Paired', 'Accent', 'okabe_ito', 'Dark2', 'Set1', 'Set2', 'Set3', 'tab10', 'tab20', 'tab20b', 'tab20c']), ('Miscellaneous', [ 'flag', 'prism', 'ocean', 'gist_earth', 'terrain', 'gist_stern', ...
Top answer
1 of 1
33

You may use the HSV system to obtain differently saturated and luminated colors for the same hue. Suppose you have at most 10 categories, then the tab10 map can be used to get a certain number of base colors. From those you can choose a couple of lighter shades for the subcategories.

The following would be a function categorical_cmap, which takes as input the number of categories (nc) and the number of subcategories (nsc) and returns a colormap with nc*nsc different colors, where for each category there are nsc colors of same hue.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors

def categorical_cmap(nc, nsc, cmap="tab10", continuous=False):
    if nc > plt.get_cmap(cmap).N:
        raise ValueError("Too many categories for colormap.")
    if continuous:
        ccolors = plt.get_cmap(cmap)(np.linspace(0,1,nc))
    else:
        ccolors = plt.get_cmap(cmap)(np.arange(nc, dtype=int))
    cols = np.zeros((nc*nsc, 3))
    for i, c in enumerate(ccolors):
        chsv = matplotlib.colors.rgb_to_hsv(c[:3])
        arhsv = np.tile(chsv,nsc).reshape(nsc,3)
        arhsv[:,1] = np.linspace(chsv[1],0.25,nsc)
        arhsv[:,2] = np.linspace(chsv[2],1,nsc)
        rgb = matplotlib.colors.hsv_to_rgb(arhsv)
        cols[i*nsc:(i+1)*nsc,:] = rgb       
    cmap = matplotlib.colors.ListedColormap(cols)
    return cmap

c1 = categorical_cmap(4, 3, cmap="tab10")
plt.scatter(np.arange(4*3),np.ones(4*3)+1, c=np.arange(4*3), s=180, cmap=c1)

c2 = categorical_cmap(2, 5, cmap="tab10")
plt.scatter(np.arange(10),np.ones(10), c=np.arange(10), s=180, cmap=c2)

c3 = categorical_cmap(5, 4, cmap="tab10")
plt.scatter(np.arange(20),np.ones(20)-1, c=np.arange(20), s=180, cmap=c3)    

plt.margins(y=0.3)
plt.xticks([])
plt.yticks([0,1,2],["(5, 4)", "(2, 5)", "(4, 3)"])
plt.show()

🌐
Readthedocs
cmap-docs.readthedocs.io › catalog › qualitative › seaborn:tab10
seaborn:tab10 - cmap
from cmap import Colormap cm = Colormap('seaborn:tab10') # case insensitive · seaborn:tab10 · seaborn:tab10 ·
🌐
Matplotlib
matplotlib.org › stable › gallery › color › named_colors.html
List of named colors — Matplotlib 3.11.2 documentation
Go to the end to download the full example code · This plots a list of the named colors supported by Matplotlib. For more information on colors in matplotlib see
🌐
Julia Programming Language
discourse.julialang.org › specific domains › visualization
Issues setting palette to matlplotlib's "tab10" colors - Visualization - Julia Programming Language
May 3, 2020 - Hello, I’ve been trying to plot curves with the colors of matplotlib’s palette :tab10 but I can’t find the solution, even after removing and reinstalling Plots and ColorSchemes. Here are the desired colors : When I run palette(:auto) , I do obtain a 17-element Vector{RGBA{Float64}}, and it’s correctly plotted in the Plot panel of Juno.
🌐
GitHub
github.com › matplotlib › matplotlib › issues › 9460
Color blind-friendly default color cycle · Issue #9460 · matplotlib/matplotlib
October 17, 2017 - The default tab10 qualitative colormap is not color blind friendly, at least with my form of color blindness; I have difficulty telling the second and third colors apart. It would be nice to have a new default qualitative colormap that is color blind friendly, since it's the most commonly used. As Matplotlib ...
Author: matplotlib
🌐
Matplotlib
fr.matplotlib.net › stable › gallery › color › colormap_reference.html
Référence de la palette de couleurs_Matplotlib —— Python visualisation
import numpy as np import ... 'twilight_shifted', 'hsv']), ('Qualitative', [ 'Pastel1', 'Pastel2', 'Paired', 'Accent', 'Dark2', 'Set1', 'Set2', 'Set3', 'tab10', 'tab20', 'tab20b', 'tab20c']), ('Miscellaneous', [ 'flag', 'prism', 'ocean', 'gist_earth', 'terrain', 'gist_stern', ...
Find elsewhere
🌐
GitHub
github.com › kavli-ntnu › MINI2P_toolbox › blob › main › Analysis › +CMP_WJ › tab10.m
MINI2P_toolbox/Analysis/+CMP_WJ/tab10.m at main · kavli-ntnu/MINI2P_toolbox
% <http://matplotlib.org/2.0.0rc2/users/dflt_style_changes.html> % VEGA10/TAB10 is the default Line Color Order for MatPlotLib 2 and 3.
Author: kavli-ntnu
🌐
podhmo's diary
pod.hatenablog.com › entry › 2018 › 09 › 20 › 212527
color mapの一覧をheatmapで(160個くらい画像があるので注意) - podhmo's diary
September 20, 2018 - from nbreversible import code import pandas as pd import numpy as np import seaborn as sns # %matplotlib inline xs = np.arange(1, 10) ys = np.arange(1, 10).resh…
🌐
Bendalab
bendalab.github.io › plottools › colors
Colors - plottools
This is matplotlib's tableau palette, the default in newer matplotlib versions, also known as tab10.
🌐
Matplotlib
matplotlib.org › 3.2.2 › gallery › color › colormap_reference.html
Colormap reference — Matplotlib 3.2.2 documentation
June 17, 2020 - import numpy as np import ... 'twilight_shifted', 'hsv']), ('Qualitative', [ 'Pastel1', 'Pastel2', 'Paired', 'Accent', 'Dark2', 'Set1', 'Set2', 'Set3', 'tab10', 'tab20', 'tab20b', 'tab20c']), ('Miscellaneous', [ 'flag', 'prism', 'ocean', 'gist_earth', 'terrain', 'gist_stern', ...