matplot の色をラベルで固定する方法

色の確認

import matplotlib as mpl
import matplotlib.pyplot as plt

def plot_colorMaps(cmap):

    fig, ax = plt.subplots(figsize=(4,0.4))
    col_map = plt.get_cmap(cmap)
    mpl.colorbar.ColorbarBase(ax, cmap=col_map, orientation = 'horizontal')

    plt.show()

for cmap_id in plt.colormaps():
    print(cmap_id)
    plot_colorMaps(cmap_id)

stackoverflow.com

色の固定

N = len(label)

cmap = plt.cm.spectral
# extract all colors from the .jet map
np.random.seed(seed=32)
clist = [cmap(np.random.randint(0, cmap.N)) for i in range(N)]

固定の色でplot

  • 平均の上位5項目をplot
for idx in range(len(X)):
    plt.figure(figsize=(12,4))
    for i in np.argsort(X[idx].mean(axis=0))[::-1][:5]:
        plt.plot(X[idx][:, i], label=label[i], color=clist[i])
    plt.legend(loc='upper right')
    plt.title(NAME_MAP[LABEL_MAP[idx]])
    plt.show()