Show Sidebar Hide Sidebar

# Cmocean Colorscales in Python

How to make Cmocean Colorscales in Python with Plotly.

#### New to Plotly?¶

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!

### cmocean¶

cmocean is a package containing colormaps for commonly-used oceanographic variables. Below we provide a function to convert a cmocean colormap to a Plotly colorscale. Check out all of the cmocean colormaps below!

### Imports¶

In [2]:
import plotly.plotly as py
import plotly.graph_objs as go
from plotly import tools

import cmocean

import numpy as np
import os


### Defining Colormaps¶

In [3]:
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


In [4]:
# Plotting the colorscale.

example_dir = os.path.join(os.path.dirname('__file__'), "examples")
"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


### Thermal¶

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

Out[5]:

### Haline¶

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

Out[8]:

### Solar¶

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

Out[9]:

### Ice¶

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

Out[10]:

### Gray¶

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

Out[11]:

### Oxy¶

In [12]:
oxy = cmocean_to_plotly(cmocean.cm.oxy, max_len)
py.iplot(colorscale_plot(colorscale=oxy, title='Oxy'))

Out[12]:

### Deep¶

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

Out[13]:

### Dense¶

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

Out[15]:

### Algae¶

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

Out[16]:

### Matter¶

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

Out[17]:

### Turbid¶

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

Out[18]:

### Speed¶

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

Out[20]:

### Amp¶

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

Out[21]:

### Tempo¶

In [22]:
tempo = cmocean_to_plotly(cmocean.cm.tempo, max_len)
py.iplot(colorscale_plot(colorscale=tempo, title='Tempo'))

Out[22]:

### Phase¶

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

Out[23]:

### Balance¶

In [24]:
balance = cmocean_to_plotly(cmocean.cm.balance, max_len)
py.iplot(colorscale_plot(colorscale=balance, title='Balance'))

Out[24]:

### Delta¶

In [25]:
delta = cmocean_to_plotly(cmocean.cm.delta, max_len)
py.iplot(colorscale_plot(colorscale=delta, title='Delta'))

Out[25]:

### Curl¶

In [26]:
curl = cmocean_to_plotly(cmocean.cm.curl, max_len)
py.iplot(colorscale_plot(colorscale=curl, title='Curl'))

Out[26]: