|
| 1 | +# -*- coding: iso-8859-1 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +Colour-blind proof distinct colours module, based on work by Paul Tol |
| 5 | +Pieter van der Meer, 2011 |
| 6 | +SRON - Netherlands Institute for Space Research |
| 7 | +""" |
| 8 | + |
| 9 | +# colour table in HTML hex format |
| 10 | +hexcols = ['#332288', '#88CCEE', '#44AA99', '#117733', '#999933', '#DDCC77', |
| 11 | + '#CC6677', '#882255', '#AA4499', '#661100', '#6699CC', '#AA4466', |
| 12 | + '#4477AA'] |
| 13 | + |
| 14 | +greysafecols = ['#809BC8', '#FF6666', '#FFCC66', '#64C204'] |
| 15 | + |
| 16 | +xarr = [[12], |
| 17 | + [12, 6], |
| 18 | + [12, 6, 5], |
| 19 | + [12, 6, 5, 3], |
| 20 | + [0, 1, 3, 5, 6], |
| 21 | + [0, 1, 3, 5, 6, 8], |
| 22 | + [0, 1, 2, 3, 5, 6, 8], |
| 23 | + [0, 1, 2, 3, 4, 5, 6, 8], |
| 24 | + [0, 1, 2, 3, 4, 5, 6, 7, 8], |
| 25 | + [0, 1, 2, 3, 4, 5, 9, 6, 7, 8], |
| 26 | + [0, 10, 1, 2, 3, 4, 5, 9, 6, 7, 8], |
| 27 | + [0, 10, 1, 2, 3, 4, 5, 9, 6, 11, 7, 8]] |
| 28 | + |
| 29 | +# get specified nr of distinct colours in HTML hex format. |
| 30 | +# in: nr - number of colours [1..12] |
| 31 | +# returns: list of distinct colours in HTML hex |
| 32 | + |
| 33 | +def get_distinct(nr): |
| 34 | + |
| 35 | + # check if nr is in correct range |
| 36 | + |
| 37 | + if nr < 1 or nr > 12: |
| 38 | + print("wrong nr of distinct colours!") |
| 39 | + return |
| 40 | + |
| 41 | + # get list of indices |
| 42 | + |
| 43 | + lst = xarr[nr-1] |
| 44 | + |
| 45 | + # generate colour list by stepping through indices and looking them up |
| 46 | + # in the colour table |
| 47 | + |
| 48 | + i_col = 0 |
| 49 | + col = [0] * nr |
| 50 | + for idx in lst: |
| 51 | + col[i_col] = hexcols[idx] |
| 52 | + i_col+=1 |
| 53 | + return col |
| 54 | + |
| 55 | +# gets 4 colours, which also look distinct in black&white |
| 56 | +# returns: list of 4 colours in |
| 57 | +#def get_distinct_grey(): |
| 58 | + |
| 59 | +import sys |
| 60 | + |
| 61 | +# display usage information and produce example plot. |
| 62 | +if __name__ == '__main__': |
| 63 | + import numpy as np |
| 64 | + import matplotlib.mlab as mlab |
| 65 | + import matplotlib.pyplot as plt |
| 66 | + |
| 67 | + print(__doc__) |
| 68 | + print("usage examples: ") |
| 69 | + print("print distinct_colours.get_distinct(2)") |
| 70 | + print(get_distinct(2)) |
| 71 | + print("print distinct_colours.greysafecols") |
| 72 | + print(greysafecols) |
| 73 | + |
| 74 | + print("generating example plot: distinct_colours_example.png") |
| 75 | + plt.close() |
| 76 | + t = np.arange(0.0, 2.0, 0.01) |
| 77 | + n = 12 |
| 78 | + if len(sys.argv) > 1: n = int(sys.argv[1]) |
| 79 | + cols = get_distinct(n) |
| 80 | + d = 2./n |
| 81 | + for i in range(n): |
| 82 | + s = np.sin(i*d*np.pi*t) |
| 83 | + plt.plot(t, s, linewidth=2.0, c=cols[i], label=str(i)) |
| 84 | + |
| 85 | + plt.xlabel('time (s)') |
| 86 | + plt.ylabel('voltage (mV)') |
| 87 | + plt.title('Distinct colours example (n = '+str(n)+')') |
| 88 | + plt.grid(True) |
| 89 | + plt.legend(loc=3, ncol=2) |
| 90 | + |
| 91 | + plt.savefig("distinct_colours_example.png") |
| 92 | + plt.show() |
0 commit comments