Dave Green (University of Cambridge) made a perceptual colourmap algorithm called CubeHelix — read all about it. It makes nice colourmaps like so...
from IPython.display import Image
url = 'http://www.mrao.cam.ac.uk/~dag/CUBEHELIX/G35.6-0.4-vgps.png'
Image(url=url, width=300)
Reference
Green, D. A. (2011). A colour scheme for the display of astronomical intensity images. Bulletin of the Astromical Society of India 39, p 289-295. 2011BASI...39..289G and arXiv:1108.5083.
So says Dave Green, inventor of the CubeHelix scheme: "Ian Heywood (of the University of Oxford) has implemented this for python (with matplotlib)"
import matplotlib
import pylab as plt
from matplotlib.colors import ListedColormap
import numpy
import matplotlib.mlab as mlab
%matplotlib inline
# Generate the difference of a pair of Gaussians and store this in z
# Example image stolen from matplotlib example gallery
delta = 0.05
x = numpy.arange(-5.0, 5.0, delta)
y = numpy.arange(-5.0, 5.0, delta)
X, Y = numpy.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = 10.0 * (Z2 - Z1)
# RGB values for Cube Helix by Dave Green.
r_chan = [0.0, 0.0080199999999999994, 0.015779999999999999, 0.023269999999999999, 0.030460000000000001, 0.03737, 0.043959999999999999, 0.050229999999999997, 0.056180000000000001, 0.061789999999999998, 0.067059999999999995, 0.071980000000000002, 0.076550000000000007, 0.080769999999999995, 0.084620000000000001, 0.088120000000000004, 0.091249999999999998, 0.094020000000000006, 0.096439999999999998, 0.098500000000000004, 0.1002, 0.10156, 0.10256999999999999, 0.10324, 0.10359, 0.10360999999999999, 0.10332, 0.10273, 0.10184, 0.10067, 0.099239999999999995, 0.097549999999999998, 0.095610000000000001, 0.093450000000000005, 0.091079999999999994, 0.088510000000000005, 0.085760000000000003, 0.082839999999999997, 0.079780000000000004, 0.076590000000000005, 0.073289999999999994, 0.069889999999999994, 0.066420000000000007, 0.062890000000000001, 0.059330000000000001, 0.055750000000000001, 0.052170000000000001, 0.04861, 0.045100000000000001, 0.041640000000000003, 0.038260000000000002, 0.03499, 0.031829999999999997, 0.028809999999999999, 0.025940000000000001, 0.02325, 0.020760000000000001, 0.01847, 0.016410000000000001, 0.0146, 0.013050000000000001, 0.011769999999999999, 0.010789999999999999, 0.010120000000000001, 0.0097699999999999992, 0.00975, 0.01008, 0.01078, 0.01184, 0.01329, 0.015129999999999999, 0.01738, 0.020029999999999999, 0.023099999999999999, 0.026599999999999999, 0.030519999999999999, 0.034880000000000001, 0.03968, 0.044909999999999999, 0.050599999999999999, 0.05672, 0.063289999999999999, 0.070300000000000001, 0.077759999999999996, 0.085650000000000004, 0.093979999999999994, 0.10274, 0.11193, 0.12153, 0.13155, 0.14197000000000001, 0.15279000000000001, 0.16399, 0.17555999999999999, 0.1875, 0.19979, 0.21242, 0.22536999999999999, 0.23863999999999999, 0.25219000000000003, 0.26602999999999999, 0.28012999999999999, 0.29448000000000002, 0.30904999999999999, 0.32384000000000002, 0.33881, 0.35396, 0.36926999999999999, 0.38471, 0.40027000000000001, 0.41592000000000001, 0.43164999999999998, 0.44742999999999999, 0.46323999999999999, 0.47907, 0.49489, 0.51068000000000002, 0.52642, 0.54208999999999996, 0.55767, 0.57313999999999998, 0.58848, 0.60367000000000004, 0.61868000000000001, 0.63349999999999995, 0.64812000000000003, 0.66249999999999998, 0.67664000000000002, 0.69050999999999996, 0.70409999999999995, 0.71740000000000004, 0.73036999999999996, 0.74302000000000001, 0.75531999999999999, 0.76727000000000001, 0.77883999999999998, 0.79001999999999994, 0.80081000000000002, 0.81118000000000001, 0.82113999999999998, 0.83067000000000002, 0.83975999999999995, 0.84841, 0.85660000000000003, 0.86434, 0.87161, 0.87841000000000002, 0.88473999999999997, 0.89058999999999999, 0.89597000000000004, 0.90088000000000001, 0.90529999999999999, 0.90925999999999996, 0.91274, 0.91574999999999995, 0.91829000000000005, 0.92037999999999998, 0.92201, 0.92318999999999996, 0.92393000000000003, 0.92423999999999995, 0.92413000000000001, 0.92359999999999998, 0.92267999999999994, 0.92135999999999996, 0.91966999999999999, 0.91761000000000004, 0.91520000000000001, 0.91246000000000005, 0.90939000000000003, 0.90602000000000005, 0.90236000000000005, 0.89842999999999995, 0.89424000000000003, 0.88980999999999999, 0.88517000000000001, 0.88031999999999999, 0.87529999999999997, 0.87011000000000005, 0.86477000000000004, 0.85931999999999997, 0.85375999999999996, 0.84811000000000003, 0.84240000000000004, 0.83665, 0.83087, 0.82508000000000004, 0.81932000000000005, 0.81357999999999997, 0.80791000000000002, 0.80230000000000001, 0.79679, 0.79139000000000004, 0.78612000000000004, 0.78100000000000003, 0.77603999999999995, 0.77127000000000001, 0.76668999999999998, 0.76232999999999995, 0.75819999999999999, 0.75431000000000004, 0.75068999999999997, 0.74733000000000005, 0.74426999999999999, 0.74150000000000005, 0.73902999999999996, 0.73689000000000004, 0.73507999999999996, 0.73360000000000003, 0.73246999999999995, 0.73168999999999995, 0.73126999999999998, 0.73121999999999998, 0.73153000000000001, 0.73221999999999998, 0.73326999999999998, 0.73470999999999997, 0.73651999999999995, 0.73870000000000002, 0.74126000000000003, 0.74419000000000002, 0.74748999999999999, 0.75114999999999998, 0.75517999999999996, 0.75954999999999995, 0.76427, 0.76932999999999996, 0.77471000000000001, 0.78042, 0.78642999999999996, 0.79274, 0.79932999999999998, 0.80618999999999996, 0.81332000000000004, 0.82067999999999997, 0.82826999999999995, 0.83608000000000005, 0.84408000000000005, 0.85224999999999995, 0.86058999999999997, 0.86907000000000001, 0.87768000000000002, 0.88639000000000001, 0.89517999999999998, 0.90403999999999995, 0.91295000000000004, 0.92188000000000003, 0.93081000000000003, 0.93972999999999995, 0.94862000000000002, 0.95745000000000002, 0.96618999999999999, 0.97484000000000004, 0.98338000000000003, 0.99177000000000004, 1.0]
g_chan = [0.0, 0.0013600000000000001, 0.0027899999999999999, 0.0043200000000000001, 0.00594, 0.0076699999999999997, 0.0095099999999999994, 0.011469999999999999, 0.01357, 0.015800000000000002, 0.018180000000000002, 0.0207, 0.023390000000000001, 0.02623, 0.029250000000000002, 0.03243, 0.035799999999999998, 0.03934, 0.043060000000000001, 0.046969999999999998, 0.051060000000000001, 0.055350000000000003, 0.059819999999999998, 0.064479999999999996, 0.069330000000000003, 0.074359999999999996, 0.079589999999999994, 0.085000000000000006, 0.090590000000000004, 0.096360000000000001, 0.10231, 0.10843999999999999, 0.11473, 0.12118, 0.1278, 0.13457, 0.14149, 0.14854999999999999, 0.15573999999999999, 0.16306000000000001, 0.17050000000000001, 0.17806, 0.18572, 0.19347, 0.20132, 0.20924000000000001, 0.21723000000000001, 0.22528000000000001, 0.23338, 0.24152000000000001, 0.24969, 0.25788, 0.26607999999999998, 0.27428000000000002, 0.28248000000000001, 0.29065000000000002, 0.29879, 0.30689, 0.31495000000000001, 0.32294, 0.33085999999999999, 0.33868999999999999, 0.34644000000000003, 0.35409000000000002, 0.36163000000000001, 0.36903999999999998, 0.37633, 0.38349, 0.39049, 0.39734999999999998, 0.40404000000000001, 0.41055999999999998, 0.41689999999999999, 0.42305999999999999, 0.42903000000000002, 0.43480999999999997, 0.44037999999999999, 0.44574000000000003, 0.45089000000000001, 0.45582, 0.46051999999999998, 0.46500999999999998, 0.46926000000000001, 0.47327999999999998, 0.47706999999999999, 0.48061999999999999, 0.48393999999999998, 0.48701, 0.48985000000000001, 0.49245, 0.49480000000000002, 0.49692999999999998, 0.49880999999999998, 0.50046999999999997, 0.50188999999999995, 0.50309000000000004, 0.50405999999999995, 0.50480999999999998, 0.50534000000000001, 0.50566, 0.50578000000000001, 0.50568999999999997, 0.50541000000000003, 0.50495000000000001, 0.50429999999999997, 0.50348000000000004, 0.50248999999999999, 0.50134999999999996, 0.50004999999999999, 0.49862000000000001, 0.49704999999999999, 0.49536000000000002, 0.49354999999999999, 0.49164999999999998, 0.48964000000000002, 0.48755999999999999, 0.4854, 0.48318, 0.48089999999999999, 0.47859000000000002, 0.47624, 0.47387000000000001, 0.47149000000000002, 0.46911999999999998, 0.46676000000000001, 0.46442, 0.46211999999999998, 0.45985999999999999, 0.45766000000000001, 0.45551999999999998, 0.45345999999999997, 0.45149, 0.44962000000000002, 0.44785000000000003, 0.44619999999999999, 0.44468000000000002, 0.44330000000000003, 0.44206000000000001, 0.44096999999999997, 0.44005, 0.43930000000000002, 0.43873000000000001, 0.43834000000000001, 0.43813999999999997, 0.43813999999999997, 0.43835000000000002, 0.43876999999999999, 0.43941000000000002, 0.44026999999999999, 0.44135999999999997, 0.44267000000000001, 0.44422, 0.44601000000000002, 0.44802999999999998, 0.45029999999999998, 0.45280999999999999, 0.45556999999999997, 0.45856999999999998, 0.46182000000000001, 0.46531, 0.46905000000000002, 0.47303000000000001, 0.47725000000000001, 0.48171000000000003, 0.48641000000000001, 0.49135000000000001, 0.49651000000000001, 0.50190000000000001, 0.50751000000000002, 0.51334000000000002, 0.51937999999999995, 0.52561999999999998, 0.53207000000000004, 0.53871000000000002, 0.54552999999999996, 0.55252999999999997, 0.55969999999999998, 0.56703999999999999, 0.57452999999999999, 0.58216999999999997, 0.58994000000000002, 0.59784999999999999, 0.60587000000000002, 0.61400999999999994, 0.62224000000000002, 0.63056000000000001, 0.63897000000000004, 0.64744000000000002, 0.65597000000000005, 0.66456000000000004, 0.67318, 0.68184, 0.69050999999999996, 0.69918999999999998, 0.70786000000000004, 0.71653, 0.72516999999999998, 0.73377999999999999, 0.74234999999999995, 0.75087000000000004, 0.75931999999999999, 0.76771, 0.77600999999999998, 0.78422000000000003, 0.79234000000000004, 0.80035000000000001, 0.80823999999999996, 0.81601000000000001, 0.82364999999999999, 0.83116000000000001, 0.83852000000000004, 0.84572999999999998, 0.85277999999999998, 0.85968, 0.86639999999999995, 0.87295999999999996, 0.87932999999999995, 0.88553000000000004, 0.89154999999999995, 0.89737999999999996, 0.90302000000000004, 0.90847, 0.91373000000000004, 0.91879999999999995, 0.92366999999999999, 0.92835000000000001, 0.93284, 0.93715000000000004, 0.94125999999999999, 0.94518000000000002, 0.94891999999999999, 0.95248999999999995, 0.95587, 0.95908000000000004, 0.96213000000000004, 0.96501000000000003, 0.96772999999999998, 0.97030000000000005, 0.97272999999999998, 0.97502, 0.97718000000000005, 0.97921999999999998, 0.98114999999999997, 0.98295999999999994, 0.98468, 0.98631999999999997, 0.98787000000000003, 0.98936000000000002, 0.99077999999999999, 0.99216000000000004, 0.99350000000000005, 0.99480999999999997, 0.99611000000000005, 0.99739999999999995, 0.99868999999999997, 1.0]
b_chan = [0.0, 0.00662, 0.01353, 0.020719999999999999, 0.028170000000000001, 0.035869999999999999, 0.043790000000000003, 0.051920000000000001, 0.060240000000000002, 0.068729999999999999, 0.077380000000000004, 0.08616, 0.095049999999999996, 0.10403999999999999, 0.11311, 0.12223000000000001, 0.13139000000000001, 0.14055999999999999, 0.14973, 0.15887000000000001, 0.16797000000000001, 0.17701, 0.18595999999999999, 0.19481000000000001, 0.20354, 0.21212, 0.22055, 0.22881000000000001, 0.23687, 0.24471999999999999, 0.25234000000000001, 0.25972000000000001, 0.26684999999999998, 0.2737, 0.28027000000000002, 0.28655000000000003, 0.29250999999999999, 0.29815000000000003, 0.30347000000000002, 0.30843999999999999, 0.31306, 0.31731999999999999, 0.32122000000000001, 0.32474999999999998, 0.32790000000000002, 0.33067000000000002, 0.33306000000000002, 0.33506000000000002, 0.33667000000000002, 0.33789999999999998, 0.33872999999999998, 0.33917999999999998, 0.33923999999999999, 0.33892, 0.33822999999999998, 0.33716000000000002, 0.33572000000000002, 0.33393, 0.33178000000000002, 0.32929000000000003, 0.32646999999999998, 0.32332, 0.31985999999999998, 0.31609999999999999, 0.31204999999999999, 0.30771999999999999, 0.30314000000000002, 0.29830000000000001, 0.29324, 0.28795999999999999, 0.28248000000000001, 0.27682000000000001, 0.27100000000000002, 0.26502999999999999, 0.25892999999999999, 0.25272, 0.24642, 0.24005000000000001, 0.23361999999999999, 0.22717000000000001, 0.22070000000000001, 0.21424000000000001, 0.20780999999999999, 0.20143, 0.19511000000000001, 0.18889, 0.18276999999999999, 0.17677999999999999, 0.17094000000000001, 0.16527, 0.15978000000000001, 0.1545, 0.14943999999999999, 0.14463000000000001, 0.14008000000000001, 0.1358, 0.13181999999999999, 0.12814999999999999, 0.12479999999999999, 0.12180000000000001, 0.11915000000000001, 0.11687, 0.11497, 0.11347, 0.11237999999999999, 0.11169999999999999, 0.11146, 0.11164, 0.11228, 0.11337, 0.11491, 0.11692, 0.11941, 0.12237000000000001, 0.1258, 0.12970999999999999, 0.13411000000000001, 0.13899, 0.14434, 0.15018000000000001, 0.15648999999999999, 0.16328000000000001, 0.17054, 0.17826, 0.18643000000000001, 0.19506000000000001, 0.20413000000000001, 0.21362999999999999, 0.22356000000000001, 0.2339, 0.24464, 0.25578000000000001, 0.26729000000000003, 0.27916000000000002, 0.29138999999999998, 0.30395, 0.31683, 0.33001999999999998, 0.34349000000000002, 0.35722999999999999, 0.37123, 0.38546000000000002, 0.39989999999999998, 0.41454000000000002, 0.42936000000000002, 0.44433, 0.45945000000000003, 0.47467999999999999, 0.49001, 0.50541000000000003, 0.52087000000000006, 0.53637000000000001, 0.55188000000000004, 0.56738999999999995, 0.58287, 0.59830000000000005, 0.61367000000000005, 0.62895000000000001, 0.64412000000000003, 0.65915999999999997, 0.67405999999999999, 0.68879999999999997, 0.70335000000000003, 0.7177, 0.73182999999999998, 0.74573, 0.75936999999999999, 0.77275000000000005, 0.78583999999999998, 0.79862999999999995, 0.81111, 0.82325999999999999, 0.83508000000000004, 0.84653999999999996, 0.85765000000000002, 0.86838000000000004, 0.87873000000000001, 0.88868999999999998, 0.89824999999999999, 0.90741000000000005, 0.91615000000000002, 0.92447999999999997, 0.93240000000000001, 0.93988000000000005, 0.94694999999999996, 0.95357999999999998, 0.95979000000000003, 0.96557999999999999, 0.97092999999999996, 0.97587000000000002, 0.98038000000000003, 0.98448000000000002, 0.98816999999999999, 0.99146000000000001, 0.99434999999999996, 0.99683999999999995, 0.99895999999999996, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.99878999999999996, 0.99707000000000001, 0.99516000000000004, 0.99309000000000003, 0.99085999999999996, 0.98850000000000005, 0.98602999999999996, 0.98346999999999996, 0.98082999999999998, 0.97813000000000005, 0.97540000000000004, 0.97265000000000001, 0.96991000000000005, 0.96718000000000004, 0.96450000000000002, 0.96187999999999996, 0.95933999999999997, 0.95689000000000002, 0.95457000000000003, 0.95237000000000005, 0.95033000000000001, 0.94845999999999997, 0.94677, 0.94528000000000001, 0.94401999999999997, 0.94298000000000004, 0.94220000000000004, 0.94167000000000001, 0.94142000000000003, 0.94145000000000001, 0.94179000000000002, 0.94242999999999999, 0.94338999999999995, 0.94467999999999996, 0.94630999999999998, 0.94828000000000001, 0.9506, 0.95326999999999995, 0.95630000000000004, 0.9597, 0.96345999999999998, 0.96758999999999995, 0.97209000000000001, 0.97694999999999999, 0.98216999999999999, 0.98775999999999997, 0.99370000000000003, 1.0]
# Fill an array with 256 RGB tuples
ctab = []
for i in range(0,len(r_chan)):
ctab.append([r_chan[i], g_chan[i], b_chan[i]])
# Create a new colourmap from the ctab array
cubehelixcm = ListedColormap(ctab, name='cubehelix', N=None)
Example plot, compared with the built-in 'cubehelix' and (added later) the generator described below.
fig = plt.figure(figsize=(18,6))
ax = fig.add_subplot(131)
cax = ax.imshow(Z, cmap=cubehelixcm, interpolation='bicubic')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_title('Handmade cubehelix')
cbar = plt.colorbar(cax)
ax = fig.add_subplot(132)
cax = ax.imshow(Z, cmap='cubehelix', interpolation='bicubic')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_title('Builtin cubehelix')
cbar = plt.colorbar(cax)
ax = fig.add_subplot(133)
cax = ax.imshow(Z, cmap='viridis', interpolation='bicubic')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_title('Default function')
cbar = plt.colorbar(cax)
plt.show()
plt.close()
Fine, but this is just the one manifestation. We'd like to generate arbitrary CubeHelix colourmaps. On to the next one!
Dave Green: "James Davenport (of the University of Washington) has written a full implementation for python, available on github."
The parameters are all optional. All are scalars except reverse
, which is Boolean.
from matplotlib.colors import LinearSegmentedColormap as LSC
from math import pi
import numpy as np
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):
# 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.
if 'endHue' in kwargs:
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 + rot * fract + 1.)
fract = fract**gamma
satar = np.linspace(minSat, maxSat, nlev)
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))
# find where RBB are outside the range [0,1], clip
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.
blu[np.where((blu < 0.))] = 0.
# optional color reverse
if reverse is True:
red = red[::-1]
blu = blu[::-1]
grn = grn[::-1]
# put in to tuple & dictionary structures needed
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)
In a nutshell:
start
or 1st param is the starting colour: 0 is blue, 1 is red, 2 is green. Default 0.5.rot
or 2nd param is the numbers of rotations: 0 gives a monochrome ramp, -1 goes blue > green > red. Default -1.5.start
or 3rd param is gamma, default 1.0.Test this out...
fig = plt.figure(figsize=(18,6))
ax = fig.add_subplot(141)
cax = ax.imshow(Z, cmap=cmap(), interpolation='bicubic')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_title('Default')
cbar = plt.colorbar(cax, shrink=0.5)
ax = fig.add_subplot(142)
cax = ax.imshow(Z, cmap=cmap(0, 0), interpolation='bicubic')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_title('Start 2, Rot 0')
cbar = plt.colorbar(cax, shrink=0.5)
ax = fig.add_subplot(143)
cax = ax.imshow(Z, cmap=cmap(0.75, -0.75), interpolation='bicubic')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_title('Start 0.75, Rot -0.75')
cbar = plt.colorbar(cax, shrink=0.5)
ax = fig.add_subplot(144)
cax = ax.imshow(Z, cmap=cmap(0.75, -2), interpolation='bicubic')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_title('Start 0.75, Rot -2')
cbar = plt.colorbar(cax, shrink=0.5)
plt.show()
plt.close()
/Users/matt/anaconda/envs/recipes/lib/python3.6/site-packages/ipykernel/__main__.py:18: DeprecationWarning: object of type <class 'float'> cannot be safely interpreted as an integer. /Users/matt/anaconda/envs/recipes/lib/python3.6/site-packages/ipykernel/__main__.py:22: DeprecationWarning: object of type <class 'float'> cannot be safely interpreted as an integer.
Awesome!
Dave Green: "Mike Bostock has implemented and extended cubehelix in JavaScript as a plugin for the D3.js visualisation library (see examples)."
I want to generate something that looks like a colourmap just by combining R, G, and B channels. This is for our pick this project at the Geophysics Hackathon 2014.
x = np.linspace(0,1,900).reshape(30,30)
r = 2 * x
r = np.clip(r, 0, 1)
g = 3 * np.copy(x) - 1
g = np.clip(g, 0, 1)
b = 3 * np.copy(x) - 2
b = np.clip(b, 0, 1)
opacity = 1
a = opacity * np.ones_like(x)
im = np.dstack([r, g, b, a])
im.shape
(30, 30, 4)
plt.imshow(im)
<matplotlib.image.AxesImage at 0x402dad0>
Show the channels superimposed, to understand how they combine to make this 'colourmap'.
plt.figure(figsize=(6,6))
ax = plt.subplot(111)
plt.imshow(im, aspect="auto")
y = np.linspace(0, 29, 30)
ax.plot(r[:,0], y, 'r')
ax.plot(g[:,0], y, 'g')
ax.plot(b[:,0], y, 'b')
ax.set_xlim(-0.1, 1.1)
ax.set_ylim(30,0)
plt.show()