【3.2】matplotlib-colors

matplotlib识别如下形式的颜色:

  • an RGB or RGBA tuple of float values in 0, 1;
  • a hex RGB or RGBA string (e.g., ‘#0F0F0F’ or ‘#0F0F0F0F’);
  • a string representation of a float value in [0, 1] inclusive for gray level (e.g., ‘0.5’);
  • one of {‘b’, ‘g’, ‘r’, ‘c’, ’m’, ‘y’, ‘k’, ‘w’};
  • a X11/CSS4 color name;
  • a name from the xkcd color survey; prefixed with ‘xkcd:’ (e.g., ‘xkcd:sky blue’);
  • one of {‘tab:blue’, ‘tab:orange’, ‘tab:green’, ‘tab:red’, ‘tab:purple’, ‘tab:brown’, ‘tab:pink’, ‘tab:gray’, ‘tab:olive’, ‘tab:cyan’} which are the Tableau Colors from the ‘T10’ categorical palette (which is the default color cycle);
  • a “CN” color spec, i.e. ‘C’ followed by a single digit, which is an index into the default property cycle (matplotlib.rcParams[‘axes.prop_cycle’]); the indexing occurs at artist creation time and defaults to black if the cycle does not include color.

二、案例

20种颜色

import matplotlib.pyplot as plt
import numpy as np

NUM_COLORS = 20
LINE_STYLES = ['solid', 'dashed', 'dashdot', 'dotted']
NUM_STYLES = len(LINE_STYLES)

cm = plt.get_cmap('gist_rainbow')
fig = plt.figure()
ax = fig.add_subplot(111)
for i in range(NUM_COLORS):
    lines = ax.plot(np.arange(10)*(i+1))
    lines[0].set_color(cm(i//NUM_STYLES*float(NUM_STYLES)/NUM_COLORS))
    lines[0].set_linestyle(LINE_STYLES[i%NUM_STYLES])

fig.savefig('moreColors.png')
plt.show()

参考资料

个人公众号,比较懒,很少更新,可以在上面提问题:

更多精彩,请移步公众号阅读:

Sam avatar
About Sam
专注生物信息 专注转化医学