{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### New to Plotly?\n",
"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/).\n",
"
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).\n",
"
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### cmocean\n",
"[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!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Imports"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"from plotly import tools\n",
"\n",
"import cmocean\n",
"\n",
"import numpy as np\n",
"import os"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Defining Colormaps"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import cmocean\n",
"\n",
"def cmocean_to_plotly(cmap, pl_entries):\n",
" h = 1.0/(pl_entries-1)\n",
" pl_colorscale = []\n",
" \n",
" for k in range(pl_entries):\n",
" C = map(np.uint8, np.array(cmap(k*h)[:3])*255)\n",
" pl_colorscale.append([k*h, 'rgb'+str((C[0], C[1], C[2]))])\n",
" \n",
" return pl_colorscale"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The examples data can be downloaded from [here.](https://github.com/plotly/documentation/blob/source-design-merge/_posts/python/style/cmocean/examples)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# Plotting the colorscale.\n",
"\n",
"example_dir = os.path.join(os.path.dirname('__file__'), \"examples\")\n",
"hist2d = np.loadtxt(os.path.join(example_dir, \"hist2d.txt\"))\n",
"st_helens = np.loadtxt(os.path.join(example_dir,\n",
" \"st-helens_before-modified.txt.gz\")).T \n",
"dx = dy = 0.05 \n",
"y, x = np.mgrid[-5 : 5 + dy : dy, -5 : 10 + dx : dx]\n",
"z = np.sin(x)**10 + np.cos(10 + y*x) + np.cos(x) + 0.2*y + 0.1*x\n",
"\n",
"elem_len = [len(hist2d), len(st_helens), len(z)]\n",
"max_len = max(elem_len)\n",
" \n",
"def colorscale_plot(colorscale, title): \n",
" trace1 = go.Heatmap(z=hist2d, colorscale=colorscale, showscale=False)\n",
" trace2 = go.Heatmap(z=st_helens, colorscale=colorscale, y0=-5, x0=-5)\n",
" trace3 = go.Heatmap(z=z,colorscale=colorscale, showscale=False)\n",
" \n",
" fig = tools.make_subplots(rows=1, cols=3, print_grid=False)\n",
" fig.append_trace(trace1, 1, 1)\n",
" fig.append_trace(trace2, 1, 2)\n",
" fig.append_trace(trace3, 1, 3)\n",
" \n",
" fig['layout'].update(title=title)\n",
" fig['layout']['xaxis2'].update(range=[0, 450])\n",
" fig['layout']['yaxis2'].update(range=[0, 270])\n",
" \n",
" return fig"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Thermal"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"thermal = cmocean_to_plotly(cmocean.cm.thermal, max_len)\n",
"py.iplot(colorscale_plot(colorscale=thermal, title='Thermal'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Haline"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"haline = cmocean_to_plotly(cmocean.cm.haline, max_len)\n",
"py.iplot(colorscale_plot(colorscale=haline, title='Haline'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Solar"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solar = cmocean_to_plotly(cmocean.cm.solar, max_len)\n",
"py.iplot(colorscale_plot(colorscale=solar, title='Solar'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Ice"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ice = cmocean_to_plotly(cmocean.cm.ice, max_len)\n",
"py.iplot(colorscale_plot(colorscale=ice, title='Ice'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Gray"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gray = cmocean_to_plotly(cmocean.cm.gray, max_len)\n",
"py.iplot(colorscale_plot(colorscale=gray, title='Gray'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Oxy"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"oxy = cmocean_to_plotly(cmocean.cm.oxy, max_len)\n",
"py.iplot(colorscale_plot(colorscale=oxy, title='Oxy'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Deep"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"deep = cmocean_to_plotly(cmocean.cm.deep, max_len)\n",
"py.iplot(colorscale_plot(colorscale=deep, title='Deep'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dense"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dense = cmocean_to_plotly(cmocean.cm.dense, max_len)\n",
"py.iplot(colorscale_plot(colorscale=dense, title='Dense'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Algae"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"algae = cmocean_to_plotly(cmocean.cm.algae, max_len)\n",
"py.iplot(colorscale_plot(colorscale=algae, title='Algae'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Matter"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"matter = cmocean_to_plotly(cmocean.cm.matter, max_len)\n",
"py.iplot(colorscale_plot(colorscale=matter, title='Matter'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Turbid"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"turbid = cmocean_to_plotly(cmocean.cm.turbid, max_len)\n",
"py.iplot(colorscale_plot(colorscale=turbid, title='Turbid'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Speed"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"speed = cmocean_to_plotly(cmocean.cm.speed, max_len)\n",
"py.iplot(colorscale_plot(colorscale=speed, title='Speed'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Amp"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"amp = cmocean_to_plotly(cmocean.cm.amp, max_len)\n",
"py.iplot(colorscale_plot(colorscale=amp, title='Amp'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Tempo"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tempo = cmocean_to_plotly(cmocean.cm.tempo, max_len)\n",
"py.iplot(colorscale_plot(colorscale=tempo, title='Tempo'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Phase"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"phase = cmocean_to_plotly(cmocean.cm.phase, max_len)\n",
"py.iplot(colorscale_plot(colorscale=phase, title='Phase'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Balance"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"balance = cmocean_to_plotly(cmocean.cm.balance, max_len)\n",
"py.iplot(colorscale_plot(colorscale=balance, title='Balance'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Delta"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"delta = cmocean_to_plotly(cmocean.cm.delta, max_len)\n",
"py.iplot(colorscale_plot(colorscale=delta, title='Delta'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Curl"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"curl = cmocean_to_plotly(cmocean.cm.curl, max_len)\n",
"py.iplot(colorscale_plot(colorscale=curl, title='Curl'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Reference\n",
"Learn more about Plotly colorscales here: [https://plotly.com/python/colorscales/](https://plotly.com/python/colorscales/)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Acknowledgment\n",
"Special thanks to [Kristen Thyng](https://github.com/kthyng) for the statistics of colormaps."
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting git+https://github.com/plotly/publisher.git\n",
" Cloning https://github.com/plotly/publisher.git to /private/var/folders/k_/zf24qrfn2kg710j9pdrxzrz40000gn/T/pip-04nSTE-build\n",
"Installing collected packages: publisher\n",
" Found existing installation: publisher 0.11\n",
" Uninstalling publisher-0.11:\n",
" Successfully uninstalled publisher-0.11\n",
" Running setup.py install for publisher ... \u001b[?25ldone\n",
"\u001b[?25hSuccessfully installed publisher-0.11\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/chelsea/venv/venv2.7/lib/python2.7/site-packages/IPython/nbconvert.py:13: ShimWarning:\n",
"\n",
"The `IPython.nbconvert` package has been deprecated since IPython 4.0. You should import from nbconvert instead.\n",
"\n",
"/Users/chelsea/venv/venv2.7/lib/python2.7/site-packages/publisher/publisher.py:53: UserWarning:\n",
"\n",
"Did you \"Save\" this notebook before running this command? Remember to save, always save.\n",
"\n"
]
}
],
"source": [
"from IPython.display import display, HTML\n",
"\n",
"display(HTML(''))\n",
"display(HTML(''))\n",
"\n",
"! pip install git+https://github.com/plotly/publisher.git --upgrade\n",
"import publisher\n",
"publisher.publish(\n",
" 'cmocean.ipynb', 'python/cmocean-colorscales/', 'Cmocean Colorscales | plotly',\n",
" 'How to make Cmocean Colorscales in Python with Plotly.',\n",
" title = 'Cmocean Colorscales | plotly',\n",
" name = 'Cmocean Colorscales',\n",
" has_thumbnail='true', thumbnail='thumbnail/colorbars.jpg',\n",
" language='python', page_type='example_index',\n",
" display_as='style_opt', order=22,\n",
" ipynb= '~notebook_demo/52')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 1
}