iMind Developers Blog

iMind開発者ブログ

matplotlibで色をたくさん使う

概要

matplotlibで色分けしてグラフを表示したい時に、色をたくさん取ってくる方法について調べる。

バージョン情報

  • matplotlib==3.0.3

8色まで

matplotlib.colors.BASE_COLORSで8色が出せる。用意されている色はb, g, r, c, m, y, k, w

import matplotlib
from matplotlib import pylab as plt

matplotlib.colors.BASE_COLORS

    #=> {'b': (0, 0, 1),
    #=>  'g': (0, 0.5, 0),
    #=>  'r': (1, 0, 0),
    #=>  'c': (0, 0.75, 0.75),
    #=>  'm': (0.75, 0, 0.75),
    #=>  'y': (0.75, 0.75, 0),
    #=>  'k': (0, 0, 0),
    #=>  'w': (1, 1, 1)}
f, ax = plt.subplots(1, 1)
for idx, (color, rgb) in enumerate(matplotlib.colors.BASE_COLORS.items()):
    x = idx
    y = 1
    ax.scatter(x, y, s=10**2, color=rgb, label=color)
plt.legend(bbox_to_anchor=(1.02, 1), loc='upper left')

f:id:imind:20190509001421p:plain

白が見えんね。

10色

matplotlib.colors.TABLEAU_COLORSでほどよく色分けされた10色が入っている。

matplotlib.colors.TABLEAU_COLORS

    #=> OrderedDict([('tab:blue', '#1f77b4'),
    #=>              ('tab:orange', '#ff7f0e'),
    #=>              ('tab:green', '#2ca02c'),
    #=>              ('tab:red', '#d62728'),
    #=>              ('tab:purple', '#9467bd'),
    #=>              ('tab:brown', '#8c564b'),
    #=>              ('tab:pink', '#e377c2'),
    #=>              ('tab:gray', '#7f7f7f'),
    #=>              ('tab:olive', '#bcbd22'),
    #=>              ('tab:cyan', '#17becf')])
f, ax = plt.subplots(1, 1)
for idx, (color, rgb) in enumerate(matplotlib.colors.TABLEAU_COLORS.items()):
    x = idx
    y = 1
    ax.scatter(x, y, s=10**2, color=rgb, label=color)
plt.legend(bbox_to_anchor=(1.02, 1), loc='upper left')

f:id:imind:20190509001551p:plain

10色以下ならこれを使うと楽。

20色

color mapを利用して複数の色を取得する。tab20, tab20_r, tab20b, tab20b_r, tab20c, tab20c_rの6つを試してみる。rはreverseの略で逆順になる。

cmap_names = ['tab20', 'tab20_r', 'tab20b', 'tab20b_r', 'tab20c', 'tab20c_r']
for cmap_name in cmap_names:
    cm = plt.cm.get_cmap(cmap_name)
    f, ax = plt.subplots(1, 1)
    for idx, rgb in enumerate(cm.colors):
        x = idx % 10
        y = -int(idx / 10)
        ax.scatter(x, y, s=10**2, color=rgb)

f:id:imind:20190509001804p:plainf:id:imind:20190509001810p:plain

f:id:imind:20190509001822p:plainf:id:imind:20190509001831p:plain

11〜20色の場合はこれを使うと楽。

148色

matplotlib.colors.CSS4_COLORSには148色が入っている。

f, ax = plt.subplots(1, 1)
for idx, (color, rgb) in enumerate(matplotlib.colors.CSS4_COLORS.items()):
    x = idx % 10
    y = -int(idx / 10)
    ax.scatter(x, y, s=10**2, color=rgb, label=color)

f:id:imind:20190509001935p:plain

色分けできることはできるけど似た色も多く見分けづらい。

949色

matplotlib.colors.XKCD_COLORSには949色入っている。

f, ax = plt.subplots(1, 1, figsize=(10, 12))
for idx, (color, rgb) in enumerate(matplotlib.colors.XKCD_COLORS.items()):
    x = idx % 20
    y = -int(idx / 20)
    ax.scatter(x, y, s=10**2, color=rgb, label=color)

f:id:imind:20190509002041p:plain

もう何が何やら。

似た色があっても問題がないのであれば、下記のように欲しい色数を取得すればそれなりの色分けにはなる。

color100 = list(matplotlib.colors.XKCD_COLORS.items())[:100]

f, ax = plt.subplots(1, 1, figsize=(5, 4))
for idx, (color, rgb) in enumerate(color100):
    x = idx % 10
    y = -int(idx / 10)
    ax.scatter(x, y, s=10**2, color=rgb, label=color)

f:id:imind:20190509002033p:plain

LinearSegmentedColormapを使う

LinearSegmentedColormapは0〜1までの値を渡すと値に対応した色を返してくれる。

虹色が出せるhsv, spectral, 白から黒へグラデーションするgray、0〜1の間に同じ周期を何周かするprismなどがある。

下記はそれぞれ1/50刻みでcolormapに引数を渡して50色出力させた例。

cmap_names = ['hsv', 'Spectral', 'gray', 'prism']
for cmap_name in cmap_names:
    cm = plt.cm.get_cmap(cmap_name)
    f, ax = plt.subplots(1, 1, figsize=(10, 1))
    for idx in range(50):
        x = idx % 25
        y = -int(idx / 25)
        rgb = cm(idx / 50)
        ax.scatter(x, y, s=10**2, color=rgb)
    ax.set_title(cmap_name)

f:id:imind:20190510023047p:plain

f:id:imind:20190510023108p:plain

f:id:imind:20190510023122p:plain

f:id:imind:20190510023139p:plain

改定履歴

Author: Masato Watanabe, Date: 2019-05-10, 記事投稿