#!/usr/bin/env python
# coding: utf-8
# #### New to Plotly?
# Plotly's Python library is free and open source! [Get started](https://plotly.com/python/getting-started/) by downloading the client and [reading the primer](https://plotly.com/python/getting-started/).
#
You can set up Plotly to work in [online](https://plotly.com/python/getting-started/#initialization-for-online-plotting) or [offline](https://plotly.com/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plotly.com/python/getting-started/#start-plotting-online).
#
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!
# ### cmocean
# [cmocean](https://github.com/matplotlib/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
# The examples data can be downloaded from [here.](https://github.com/plotly/documentation/blob/source-design-merge/_posts/python/style/cmocean/examples)
# In[4]:
# 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
# In[5]:
thermal = cmocean_to_plotly(cmocean.cm.thermal, max_len)
py.iplot(colorscale_plot(colorscale=thermal, title='Thermal'))
# ### Haline
# In[8]:
haline = cmocean_to_plotly(cmocean.cm.haline, max_len)
py.iplot(colorscale_plot(colorscale=haline, title='Haline'))
# ### Solar
# In[9]:
solar = cmocean_to_plotly(cmocean.cm.solar, max_len)
py.iplot(colorscale_plot(colorscale=solar, title='Solar'))
# ### Ice
# In[10]:
ice = cmocean_to_plotly(cmocean.cm.ice, max_len)
py.iplot(colorscale_plot(colorscale=ice, title='Ice'))
# ### Gray
# In[11]:
gray = cmocean_to_plotly(cmocean.cm.gray, max_len)
py.iplot(colorscale_plot(colorscale=gray, title='Gray'))
# ### Oxy
# In[12]:
oxy = cmocean_to_plotly(cmocean.cm.oxy, max_len)
py.iplot(colorscale_plot(colorscale=oxy, title='Oxy'))
# ### Deep
# In[13]:
deep = cmocean_to_plotly(cmocean.cm.deep, max_len)
py.iplot(colorscale_plot(colorscale=deep, title='Deep'))
# ### Dense
# In[15]:
dense = cmocean_to_plotly(cmocean.cm.dense, max_len)
py.iplot(colorscale_plot(colorscale=dense, title='Dense'))
# ### Algae
# In[16]:
algae = cmocean_to_plotly(cmocean.cm.algae, max_len)
py.iplot(colorscale_plot(colorscale=algae, title='Algae'))
# ### Matter
# In[17]:
matter = cmocean_to_plotly(cmocean.cm.matter, max_len)
py.iplot(colorscale_plot(colorscale=matter, title='Matter'))
# ### Turbid
# In[18]:
turbid = cmocean_to_plotly(cmocean.cm.turbid, max_len)
py.iplot(colorscale_plot(colorscale=turbid, title='Turbid'))
# ### Speed
# In[20]:
speed = cmocean_to_plotly(cmocean.cm.speed, max_len)
py.iplot(colorscale_plot(colorscale=speed, title='Speed'))
# ### Amp
# In[21]:
amp = cmocean_to_plotly(cmocean.cm.amp, max_len)
py.iplot(colorscale_plot(colorscale=amp, title='Amp'))
# ### Tempo
# In[22]:
tempo = cmocean_to_plotly(cmocean.cm.tempo, max_len)
py.iplot(colorscale_plot(colorscale=tempo, title='Tempo'))
# ### Phase
# In[23]:
phase = cmocean_to_plotly(cmocean.cm.phase, max_len)
py.iplot(colorscale_plot(colorscale=phase, title='Phase'))
# ### Balance
# In[24]:
balance = cmocean_to_plotly(cmocean.cm.balance, max_len)
py.iplot(colorscale_plot(colorscale=balance, title='Balance'))
# ### Delta
# In[25]:
delta = cmocean_to_plotly(cmocean.cm.delta, max_len)
py.iplot(colorscale_plot(colorscale=delta, title='Delta'))
# ### Curl
# In[26]:
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://plotly.com/python/colorscales/](https://plotly.com/python/colorscales/)
# ### Acknowledgment
# Special thanks to [Kristen Thyng](https://github.com/kthyng) for the statistics of colormaps.
# In[27]:
from IPython.display import display, HTML
display(HTML(''))
display(HTML(''))
get_ipython().system(' 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')
# In[ ]: