|
46 | 46 | 'blue': lambda x: -1.1 * np.sin((x * 20.9) * np.pi), |
47 | 47 | } |
48 | 48 |
|
| 49 | +def cubehelix(gamma=1.0, s=0.5, r=-1.5, h=1.0): |
| 50 | + """Return custom data dictionary of (r,g,b) conversion functions, which |
| 51 | + can be used with :func:`register_cmap`, for the cubehelix color scheme. |
| 52 | +
|
| 53 | + Unlike most other color schemes cubehelix was designed by D.A. Green to |
| 54 | + be monotonically increasing in terms of perceived brightness. |
| 55 | + Also, when printed on a black and white postscript printer, the scheme |
| 56 | + results in a greyscale with monotonically increasing brightness. |
| 57 | + This color scheme is named cubehelix because the r,g,b values produced |
| 58 | + can be visualised as a squashed helix around the diagonal in the |
| 59 | + r,g,b color cube. |
| 60 | +
|
| 61 | + For a unit color cube (i.e. 3-D coordinates for r,g,b each in the |
| 62 | + range 0 to 1) the color scheme starts at (r,g,b) = (0,0,0), i.e. black, |
| 63 | + and finishes at (r,g,b) = (1,1,1), i.e. white. For some fraction *x*, |
| 64 | + between 0 and 1, the color is the corresponding grey value at that |
| 65 | + fraction along the black to white diagonal (x,x,x) plus a color |
| 66 | + element. This color element is calculated in a plane of constant |
| 67 | + perceived intensity and controlled by the following parameters. |
| 68 | +
|
| 69 | + Optional keyword arguments: |
| 70 | +
|
| 71 | + ========= ======================================================= |
| 72 | + Keyword Description |
| 73 | + ========= ======================================================= |
| 74 | + gamma gamma factor to emphasise either low intensity values |
| 75 | + (gamma < 1), or high intensity values (gamma > 1); |
| 76 | + defaults to 1.0. |
| 77 | + s the start color; defaults to 0.5 (i.e. purple). |
| 78 | + r the number of r,g,b rotations in color that are made |
| 79 | + from the start to the end of the color scheme; defaults |
| 80 | + to -1.5 (i.e. -> B -> G -> R -> B). |
| 81 | + h the hue parameter which controls how saturated the |
| 82 | + colors are. If this parameter is zero then the color |
| 83 | + scheme is purely a greyscale; defaults to 1.0. |
| 84 | + ========= ======================================================= |
| 85 | +
|
| 86 | + """ |
| 87 | + |
| 88 | + def get_color_function(p0, p1): |
| 89 | + def color(x): |
| 90 | + # Apply gamma factor to emphasise low or high intensity values |
| 91 | + xg = x**gamma |
| 92 | + |
| 93 | + # Calculate amplitude and angle of deviation from the black |
| 94 | + # to white diagonal in the plane of constant |
| 95 | + # perceived intensity. |
| 96 | + a = h * xg * (1 - xg) / 2 |
| 97 | + |
| 98 | + phi = 2 * np.pi * (s / 3 + r * x) |
| 99 | + |
| 100 | + return xg + a * (p0 * np.cos(phi) + p1 * np.sin(phi)) |
| 101 | + return color |
| 102 | + |
| 103 | + return { |
| 104 | + 'red': get_color_function(-0.14861, 1.78277), |
| 105 | + 'green': get_color_function(-0.29227, -0.90649), |
| 106 | + 'blue': get_color_function(1.97294, 0.0), |
| 107 | + } |
| 108 | + |
| 109 | +_cubehelix_data = cubehelix() |
| 110 | + |
49 | 111 | _bwr_data = ((0.0, 0.0, 1.0), (1.0, 1.0, 1.0), (1.0, 0.0, 0.0)) |
50 | 112 | _brg_data = ((0.0, 0.0, 1.0), (1.0, 0.0, 0.0), (0.0, 1.0, 0.0)) |
51 | 113 |
|
@@ -1575,6 +1637,7 @@ def gfunc32(x): |
1575 | 1637 | 'brg': _brg_data, |
1576 | 1638 | 'cool': _cool_data, |
1577 | 1639 | 'copper': _copper_data, |
| 1640 | + 'cubehelix': _cubehelix_data, |
1578 | 1641 | 'flag': _flag_data, |
1579 | 1642 | 'gnuplot': _gnuplot_data, |
1580 | 1643 | 'gnuplot2': _gnuplot2_data, |
|
0 commit comments