Show Sidebar Hide Sidebar

Cmocean Colorscales in Python

How to make Cmocean Colorscales in Python with Plotly.

New to Plotly?

Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!

Defining Colormaps

In [1]:
import cmocean

def cmocean_to_plotly(cmap, pl_entries):
    h = 1.0/(pl_entries-1)
    pl_colorscale = []
    
    for k in range(pl_entries):
        C = map(np.uint8, np.array(cmap(k*h)[:3])*255)
        pl_colorscale.append([k*h, 'rgb'+str((C[0], C[1], C[2]))])
        
    return pl_colorscale

The examples data can be downloaded from here.

In [2]:
# Plotting the colorscale.

import plotly.plotly as py
import numpy as np
import os
import plotly.graph_objs as go
from plotly import tools

example_dir = os.path.join(os.path.dirname('__file__'), "examples")
hist2d = np.loadtxt(os.path.join(example_dir, "hist2d.txt"))
st_helens = np.loadtxt(os.path.join(example_dir,
                                        "st-helens_before-modified.txt.gz")).T   
dx = dy = 0.05  
y, x = np.mgrid[-5 : 5 + dy : dy, -5 : 10 + dx : dx]
z = np.sin(x)**10 + np.cos(10 + y*x) + np.cos(x) + 0.2*y + 0.1*x

elem_len = [len(hist2d), len(st_helens), len(z)]
max_len = max(elem_len)
    
def colorscale_plot(colorscale, title):    
    trace1 = go.Heatmap(z=hist2d, colorscale=colorscale, showscale=False)
    trace2 = go.Heatmap(z=st_helens, colorscale=colorscale, y0=-5, x0=-5)
    trace3 = go.Heatmap(z=z,colorscale=colorscale, showscale=False)
    
    fig = tools.make_subplots(rows=1, cols=3, print_grid=False)
    fig.append_trace(trace1, 1, 1)
    fig.append_trace(trace2, 1, 2)
    fig.append_trace(trace3, 1, 3)
    
    fig['layout'].update(title=title)
    fig['layout']['xaxis2'].update(range=[0, 450])
    fig['layout']['yaxis2'].update(range=[0, 270])
    
    return fig

Bathymetry

In [3]:
bathy = cmocean_to_plotly(cmocean.cm.bathy, max_len)
py.iplot(colorscale_plot(colorscale=bathy, title='Bathymetry'))
Out[3]:

CDOM

In [4]:
cdom = cmocean_to_plotly(cmocean.cm.CDOM, max_len)
py.iplot(colorscale_plot(colorscale=cdom, title='CDOM'))
Out[4]:

Chlorophyll

In [5]:
chlorophyll = cmocean_to_plotly(cmocean.cm.chlorophyll, max_len)
py.iplot(colorscale_plot(colorscale=chlorophyll, title='Chlorophyll'))
Out[5]:

Density

In [6]:
density = cmocean_to_plotly(cmocean.cm.density, max_len)
py.iplot(colorscale_plot(colorscale=density, title='Density'))
Out[6]:

Free Surface

In [7]:
freesurface = cmocean_to_plotly(cmocean.cm.freesurface, max_len)
py.iplot(colorscale_plot(colorscale=freesurface, title='Free Surface'))
Out[7]:

Gray

In [8]:
gray = cmocean_to_plotly(cmocean.cm.gray, max_len)
py.iplot(colorscale_plot(colorscale=gray, title='Gray'))
Out[8]:

Oxygen

In [9]:
oxygen = cmocean_to_plotly(cmocean.cm.oxygen, max_len)
py.iplot(colorscale_plot(colorscale=oxygen, title='Oxygen'))
Out[9]:

PAR

In [10]:
par = cmocean_to_plotly(cmocean.cm.par, max_len)
py.iplot(colorscale_plot(colorscale=par, title='PAR'))
Out[10]:

Phase

In [13]:
phase = cmocean_to_plotly(cmocean.cm.phase, max_len)
py.iplot(colorscale_plot(colorscale=phase, title='Phase'))
Out[13]:

Salinty

In [14]:
salinty = cmocean_to_plotly(cmocean.cm.salt, max_len)
py.iplot(colorscale_plot(colorscale=salinty, title='Salinty'))
Out[14]:

Speed

In [15]:
speed = cmocean_to_plotly(cmocean.cm.speed, max_len)
py.iplot(colorscale_plot(colorscale=speed, title='Speed'))
Out[15]:

Temperature

In [16]:
temperature = cmocean_to_plotly(cmocean.cm.temperature, max_len)
py.iplot(colorscale_plot(colorscale=temperature, title='Temperature'))
Out[16]:

Turbidity

In [17]:
turbidity = cmocean_to_plotly(cmocean.cm.turbidity, max_len)
py.iplot(colorscale_plot(colorscale=turbidity, title='Turbidity'))
Out[17]:

Velocity

In [18]:
velocity = cmocean_to_plotly(cmocean.cm.velocity, max_len)
py.iplot(colorscale_plot(colorscale=velocity, title='Velocity'))
Out[18]:

Wave height

In [19]:
waveheight = cmocean_to_plotly(cmocean.cm.waveheight, max_len)
py.iplot(colorscale_plot(colorscale=waveheight, title='Wave Height'))
Out[19]:

Wave Period

In [20]:
waveperiod = cmocean_to_plotly(cmocean.cm.waveperiod, max_len)
py.iplot(colorscale_plot(colorscale=waveperiod, title='Wave Period'))
Out[20]:

Vorticity

In [21]:
vorticity = cmocean_to_plotly(cmocean.cm.vorticity, max_len)
py.iplot(colorscale_plot(colorscale=vorticity, title='Vorticity'))
Out[21]:

Acknowledgment

Special thanks to Kristen Thyng for the statistics of colormaps.

Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.