From 2dc70020dc8a9b69e5b280e356ce3a4d4c5332c0 Mon Sep 17 00:00:00 2001 From: Pim Schellart Date: Tue, 13 Sep 2011 13:58:13 +0200 Subject: [PATCH 1/2] Added cubehelix monotonically increasing colorscheme (developed by D.A. Green) to matplotlib colorscheme list. --- lib/matplotlib/_cm.py | 52 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/lib/matplotlib/_cm.py b/lib/matplotlib/_cm.py index 5bea5199cf4b..fb54e35cc86a 100644 --- a/lib/matplotlib/_cm.py +++ b/lib/matplotlib/_cm.py @@ -46,6 +46,57 @@ 'blue': lambda x: -1.1 * np.sin((x * 20.9) * np.pi), } +def cubehelix(gamma = 1.0, s = 0.5, r = -1.5, h = 1.0): + """Return dictionary of (r,g,b) conversion functions for the cubehelix + color scheme. + + Unlike most other color schemes cubehelix was designed by D.A. Green to + be monotonically increasing in terms of perceived brightness. + Also, when printed on a black and white postscript printer, the scheme + results in a greyscale with monotonically increasing brightness. + + Optional keyword arguments: + + ========= ======================================================= + Keyword Description + ========= ======================================================= + gamma gamma factor to emphasise either low intensity values + (gamma < 1), or high intensity values (gamma > 1); + defaults to 1.0. + s the start color; defaults to 0.5. + r the number of r,g,b rotations in color that are made + from the start to the end of the color scheme; defaults + to -1.5. + h the hue parameter which controls how saturated the + colors are. If this parameter is zero then the color + scheme is purely a greyscale. + ========= ======================================================= + + """ + + def get_color_function(p0, p1): + def color(x): + # Apply gamma factor to emphasise low or high intensity values + xg = x**gamma + + # Calculate amplitude and angle of deviation from the black + # to white diagonal in the plane of constant + # perceived intensity. + a = h * xg * (1 - xg) / 2 + + phi = 2 * np.pi * (s / 3 + r * x) + + return xg + a * (p0 * np.cos(phi) + p1 * np.sin(phi)) + return color + + return { + 'red': get_color_function(-0.14861, 1.78277), + 'green': get_color_function(-0.29227, -0.90649), + 'blue': get_color_function(1.97294, 0.0), + } + +_cubehelix_data = cubehelix() + _bwr_data = ((0.0, 0.0, 1.0), (1.0, 1.0, 1.0), (1.0, 0.0, 0.0)) _brg_data = ((0.0, 0.0, 1.0), (1.0, 0.0, 0.0), (0.0, 1.0, 0.0)) @@ -1575,6 +1626,7 @@ def gfunc32(x): 'brg': _brg_data, 'cool': _cool_data, 'copper': _copper_data, + 'cubehelix': _cubehelix_data, 'flag': _flag_data, 'gnuplot': _gnuplot_data, 'gnuplot2': _gnuplot2_data, From 87321b7aec7c1ea104cd75290d6f0963359e611a Mon Sep 17 00:00:00 2001 From: Pim Schellart Date: Fri, 16 Sep 2011 14:54:36 +0200 Subject: [PATCH 2/2] Updates to cubehelix color scheme. Expanded docstring of cubehelix color scheme generation function, fixed function definition to comply with PEP8 and added cubehelix to cm namespace so users can create a custom variation of cubehelix for use in register_cmap. --- lib/matplotlib/_cm.py | 23 +++++++++++++++++------ lib/matplotlib/cm.py | 1 + 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/_cm.py b/lib/matplotlib/_cm.py index fb54e35cc86a..ceeff9c644bd 100644 --- a/lib/matplotlib/_cm.py +++ b/lib/matplotlib/_cm.py @@ -46,14 +46,25 @@ 'blue': lambda x: -1.1 * np.sin((x * 20.9) * np.pi), } -def cubehelix(gamma = 1.0, s = 0.5, r = -1.5, h = 1.0): - """Return dictionary of (r,g,b) conversion functions for the cubehelix - color scheme. +def cubehelix(gamma=1.0, s=0.5, r=-1.5, h=1.0): + """Return custom data dictionary of (r,g,b) conversion functions, which + can be used with :func:`register_cmap`, for the cubehelix color scheme. Unlike most other color schemes cubehelix was designed by D.A. Green to be monotonically increasing in terms of perceived brightness. Also, when printed on a black and white postscript printer, the scheme results in a greyscale with monotonically increasing brightness. + This color scheme is named cubehelix because the r,g,b values produced + can be visualised as a squashed helix around the diagonal in the + r,g,b color cube. + + For a unit color cube (i.e. 3-D coordinates for r,g,b each in the + range 0 to 1) the color scheme starts at (r,g,b) = (0,0,0), i.e. black, + and finishes at (r,g,b) = (1,1,1), i.e. white. For some fraction *x*, + between 0 and 1, the color is the corresponding grey value at that + fraction along the black to white diagonal (x,x,x) plus a color + element. This color element is calculated in a plane of constant + perceived intensity and controlled by the following parameters. Optional keyword arguments: @@ -63,13 +74,13 @@ def cubehelix(gamma = 1.0, s = 0.5, r = -1.5, h = 1.0): gamma gamma factor to emphasise either low intensity values (gamma < 1), or high intensity values (gamma > 1); defaults to 1.0. - s the start color; defaults to 0.5. + s the start color; defaults to 0.5 (i.e. purple). r the number of r,g,b rotations in color that are made from the start to the end of the color scheme; defaults - to -1.5. + to -1.5 (i.e. -> B -> G -> R -> B). h the hue parameter which controls how saturated the colors are. If this parameter is zero then the color - scheme is purely a greyscale. + scheme is purely a greyscale; defaults to 1.0. ========= ======================================================= """ diff --git a/lib/matplotlib/cm.py b/lib/matplotlib/cm.py index 9621d345bfb4..87386138954d 100644 --- a/lib/matplotlib/cm.py +++ b/lib/matplotlib/cm.py @@ -13,6 +13,7 @@ import matplotlib.colors as colors import matplotlib.cbook as cbook from matplotlib._cm import datad +from matplotlib._cm import cubehelix cmap_d = dict()