Skip to content

Latest commit

 

History

History
252 lines (187 loc) · 6.7 KB

cmocean-colorscales.md

File metadata and controls

252 lines (187 loc) · 6.7 KB
jupyter
jupytext kernelspec plotly
notebook_metadata_filter text_representation
all
extension format_name format_version jupytext_version
.md
markdown
1.1
1.1.1
display_name language name
Python 2
python
python2
description display_as language layout name order page_type permalink thumbnail
How to make Cmocean Colorscales in Python with Plotly.
advanced_opt
python
base
Cmocean Colorscales
22
example_index
python/cmocean-colorscales/
thumbnail/colorbars.jpg

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!

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

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

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.

# Plotting the colorscale.

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

Thermal

thermal = cmocean_to_plotly(cmocean.cm.thermal, max_len)
py.iplot(colorscale_plot(colorscale=thermal, title='Thermal'))

Haline

haline = cmocean_to_plotly(cmocean.cm.haline, max_len)
py.iplot(colorscale_plot(colorscale=haline, title='Haline'))

Solar

solar = cmocean_to_plotly(cmocean.cm.solar, max_len)
py.iplot(colorscale_plot(colorscale=solar, title='Solar'))

Ice

ice = cmocean_to_plotly(cmocean.cm.ice, max_len)
py.iplot(colorscale_plot(colorscale=ice, title='Ice'))

Gray

gray = cmocean_to_plotly(cmocean.cm.gray, max_len)
py.iplot(colorscale_plot(colorscale=gray, title='Gray'))

Oxy

oxy = cmocean_to_plotly(cmocean.cm.oxy, max_len)
py.iplot(colorscale_plot(colorscale=oxy, title='Oxy'))

Deep

deep = cmocean_to_plotly(cmocean.cm.deep, max_len)
py.iplot(colorscale_plot(colorscale=deep, title='Deep'))

Dense

dense = cmocean_to_plotly(cmocean.cm.dense, max_len)
py.iplot(colorscale_plot(colorscale=dense, title='Dense'))

Algae

algae = cmocean_to_plotly(cmocean.cm.algae, max_len)
py.iplot(colorscale_plot(colorscale=algae, title='Algae'))

Matter

matter = cmocean_to_plotly(cmocean.cm.matter, max_len)
py.iplot(colorscale_plot(colorscale=matter, title='Matter'))

Turbid

turbid = cmocean_to_plotly(cmocean.cm.turbid, max_len)
py.iplot(colorscale_plot(colorscale=turbid, title='Turbid'))

Speed

speed = cmocean_to_plotly(cmocean.cm.speed, max_len)
py.iplot(colorscale_plot(colorscale=speed, title='Speed'))

Amp

amp = cmocean_to_plotly(cmocean.cm.amp, max_len)
py.iplot(colorscale_plot(colorscale=amp, title='Amp'))

Tempo

tempo = cmocean_to_plotly(cmocean.cm.tempo, max_len)
py.iplot(colorscale_plot(colorscale=tempo, title='Tempo'))

Phase

phase = cmocean_to_plotly(cmocean.cm.phase, max_len)
py.iplot(colorscale_plot(colorscale=phase, title='Phase'))

Balance

balance = cmocean_to_plotly(cmocean.cm.balance, max_len)
py.iplot(colorscale_plot(colorscale=balance, title='Balance'))

Delta

delta = cmocean_to_plotly(cmocean.cm.delta, max_len)
py.iplot(colorscale_plot(colorscale=delta, title='Delta'))

Curl

curl = cmocean_to_plotly(cmocean.cm.curl, max_len)
py.iplot(colorscale_plot(colorscale=curl, title='Curl'))

Reference

Learn more about Plotly colorscales here: https://plot.ly/python/colorscales/

Acknowledgment

Special thanks to Kristen Thyng for the statistics of colormaps.

from IPython.display import display, HTML

display(HTML('<link href="//fonts.googleapis.com/css?family=Open+Sans:600,400,300,200|Inconsolata|Ubuntu+Mono:400,700" rel="stylesheet" type="text/css" />'))
display(HTML('<link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">'))

! pip install git+https://github.com/plotly/publisher.git --upgrade
import publisher
publisher.publish(
    'cmocean.ipynb', 'python/cmocean-colorscales/', 'Cmocean Colorscales | plotly',
    'How to make Cmocean Colorscales in Python with Plotly.',
    title = 'Cmocean Colorscales | plotly',
    name = 'Cmocean Colorscales',
    has_thumbnail='true', thumbnail='thumbnail/colorbars.jpg',
    language='python', page_type='example_index',
    display_as='style_opt', order=22,
    ipynb= '~notebook_demo/52')