Skip to content

Commit

Permalink
pep8-itfy
Browse files Browse the repository at this point in the history
  • Loading branch information
jradavenport committed Apr 30, 2014
1 parent be16d25 commit dfc0d05
Showing 1 changed file with 35 additions and 35 deletions.
70 changes: 35 additions & 35 deletions cubehelix.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,40 @@
from math import pi
import numpy as np

def cmap(start=0.5, rot=-1.5, gamma=1.0,reverse=False, nlev=256.,

def cmap(start=0.5, rot=-1.5, gamma=1.0, reverse=False, nlev=256.,
minSat=1.2, maxSat=1.2, minLight=0., maxLight=1.,
**kwargs):
"""
A full implementation of Dave Green's "cubehelix" for Matplotlib.
Based on the FORTRAN 77 code provided in
D.A. Green, 2011, BASI, 39, 289.
Based on the FORTRAN 77 code provided in
D.A. Green, 2011, BASI, 39, 289.
http://adsabs.harvard.edu/abs/2011arXiv1108.5083G
User can adjust all parameters of the cubehelix algorithm.
This enables much greater flexibility in choosing color maps, while
always ensuring the color map scales in intensity from black
User can adjust all parameters of the cubehelix algorithm.
This enables much greater flexibility in choosing color maps, while
always ensuring the color map scales in intensity from black
to white. A few simple examples:
Default color map settings produce the standard "cubehelix".
Create color map in only blues by setting rot=0 and start=0.
Create reverse (white to black) backwards through the rainbow once
by setting rot=1 and reverse=True.
Parameters
----------
start : scalar, optional
Sets the starting position in the color space. 0=blue, 1=red,
Sets the starting position in the color space. 0=blue, 1=red,
2=green. Defaults to 0.5.
rot : scalar, optional
The number of rotations through the rainbow. Can be positive
The number of rotations through the rainbow. Can be positive
or negative, indicating direction of rainbow. Negative values
correspond to Blue->Red direction. Defaults to -1.5
gamma : scalar, optional
The gamma correction for intensity. Defaults to 1.0
The gamma correction for intensity. Defaults to 1.0
reverse : boolean, optional
Set to True to reverse the color map. Will go from black to
white. Good for density plots where shade~density. Defaults to False
Expand All @@ -51,11 +51,11 @@ def cmap(start=0.5, rot=-1.5, gamma=1.0,reverse=False, nlev=256.,
maxSat : scalar, optional
Sets the maximum-level saturation. Defaults to 1.2
startHue : scalar, optional
Sets the starting color, ranging from [0,360], as in
Sets the starting color, ranging from [0, 360], as in
D3 version by @mbostock
NOTE: overrides values in start= parameter
endHue : scalar, optional
Sets the ending color, ranging from [0,360], as in
Sets the ending color, ranging from [0, 360], as in
D3 version by @mbostock
NOTE: overrides values in rot= parameter
minLight : scalar, optional
Expand All @@ -71,49 +71,49 @@ def cmap(start=0.5, rot=-1.5, gamma=1.0,reverse=False, nlev=256.,
-------
>>> import cubehelix
>>> cx = cubehelix.cmap(start=0., rot=-0.5)
>>> plot(x,cmap=cx)
>>> plot(x, cmap=cx)
Revisions
---------
2014-04 (@jradavenport) Ported from IDL version
2014-04 (@jradavenport) Added kwargs to enable similar to D3 version,
2014-04 (@jradavenport) Added kwargs to enable similar to D3 version,
changed name of "hue" parameter to "sat"
"""

#-- override start and rot if startHue and endHue are set
if kwargs is not None:
if 'startHue' in kwargs:
start = (kwargs.get('startHue')/360. -1.)*3.
start = (kwargs.get('startHue')/360. - 1.)*3.
if 'endHue' in kwargs:
rot = kwargs.get('endHue')/360. -1. -start/3.
rot = kwargs.get('endHue')/360. - start/3. - 1.
if 'sat' in kwargs:
minSat = kwargs.get('sat')
maxSat = kwargs.get('sat')

#-- set up the parameters
fract = np.linspace(minLight, maxLight, nlev)
angle = 2.0*pi * (start/3.0 + 1.0 + rot*fract)
angle = 2.0*pi*(start/3.0 + rot*fract + 1.)
fract = fract**gamma

satar = np.linspace(minSat, maxSat, nlev)
amp = satar*fract*(1.-fract)/2.
amp = satar*fract*(1. - fract)/2.

#-- compute the RGB vectors according to main equations
red = fract+amp*(-0.14861*np.cos(angle)+1.78277*np.sin(angle))
grn = fract+amp*(-0.29227*np.cos(angle)-0.90649*np.sin(angle))
blu = fract+amp*(1.97294*np.cos(angle))
red = fract + amp*(-0.14861*np.cos(angle) + 1.78277*np.sin(angle))
grn = fract + amp*(-0.29227*np.cos(angle) - 0.90649*np.sin(angle))
blu = fract + amp*(1.97294*np.cos(angle))

#-- find where RBB are outside the range [0,1], clip
red[np.where((red > 1.))] = 1.
grn[np.where((grn > 1.))] = 1.
red[np.where((red > 1.))] = 1.
grn[np.where((grn > 1.))] = 1.
blu[np.where((blu > 1.))] = 1.

red[np.where((red < 0.))] = 0.
grn[np.where((grn < 0.))] = 0.
red[np.where((red < 0.))] = 0.
grn[np.where((grn < 0.))] = 0.
blu[np.where((blu < 0.))] = 0.

#-- optional color reverse
if reverse==True:
if reverse is True:
red = red[::-1]
blu = blu[::-1]
grn = grn[::-1]
Expand All @@ -122,10 +122,10 @@ def cmap(start=0.5, rot=-1.5, gamma=1.0,reverse=False, nlev=256.,
rr = []
bb = []
gg = []
for k in range(0,int(nlev)):
rr.append((float(k)/(nlev-1.), red[k], red[k]))
bb.append((float(k)/(nlev-1.), blu[k], blu[k]))
gg.append((float(k)/(nlev-1.), grn[k], grn[k]))
cdict = {'red':rr, 'blue':bb, 'green':gg}
return LSC('cubehelix_map',cdict)
for k in range(0, int(nlev)):
rr.append((float(k) / (nlev - 1.), red[k], red[k]))
bb.append((float(k) / (nlev - 1.), blu[k], blu[k]))
gg.append((float(k) / (nlev - 1.), grn[k], grn[k]))

cdict = {'red': rr, 'blue': bb, 'green': gg}
return LSC('cubehelix_map', cdict)

0 comments on commit dfc0d05

Please sign in to comment.