{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Collection of user-contributed colormaps\n", "\n", "[contrib_colormaps](https://github.com/pyviz/contrib_colormaps) is a collection of \n", "user-contributed colormaps for use with Python plotting programs like\n", "[Bokeh](https://bokeh.org),\n", "[Matplotlib](https://matplotlib.org),\n", "[HoloViews](https://holoviews.org), and\n", "[Datashader](https://datashader.org). " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## View Colormap Swatches \n", "\n", "For ease of use, we provide minimal plotting commands for use with contrib_colormaps. To use these plotting commands, you'll need to install HoloViews.\n", "\n", "**NOTE**: HoloViews is not required to install or use contrib_colormaps, it is only required when using the plotting helper functions." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from contrib_colormaps.plotting import swatch\n", "import holoviews as hv" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "hv.extension('matplotlib', logo=False)\n", "swatch('sample')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "hv.extension('bokeh', logo=False)\n", "swatch('sample')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Accessing the colormaps\n", "\n", "After importing `contrib_colormaps` as `cc`, the colormaps will be available for use in different forms. This library should have at least one such form convenient for any particular application. This library generates two versions of each colormap:\n", "\n", "1. A Bokeh-style palette, i.e., a Python list of RGB colors as hex strings, like ``['#000000', ..., '#ffffff']``\n", "2. A Matplotlib ``LinearSegmentedColormap``, i.e., a list of RGBA tuples, like ``[[0.0,0.0,0.0], ..., [1.0,1.0,1.0]])``" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Matplotlib" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import contrib_colormaps as cc\n", "\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "\n", "x = np.arange(-10,11)\n", "fig, ax = plt.subplots(figsize=(5,2.5))\n", "im = ax.scatter(x, -(x**2), c=(x**2), s=40, cmap=cc.cm.sample)\n", "fig.colorbar(im)\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Bokeh" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import contrib_colormaps as cc\n", "\n", "from bokeh.plotting import output_notebook, figure, show\n", "from bokeh.transform import linear_cmap\n", "from bokeh.models import ColorBar, LinearColorMapper\n", "\n", "output_notebook()\n", "p = figure(plot_width=300,plot_height=150)\n", "x = np.arange(-10, 11)\n", "y = -(x**2)\n", "source = {'x': x, 'y': y, 'c': y}\n", "\n", "color_mapper = LinearColorMapper(palette=cc.b_sample, low=-100, high=0)\n", "color_bar = ColorBar(color_mapper=color_mapper, width=10, location=(0, 0))\n", "\n", "p.circle(source=source, x='x', y='y', color=linear_cmap('c', cc.b_sample, -100, 0), size=10)\n", "p.add_layout(color_bar, 'right')\n", "\n", "show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The Bokeh-compatible palettes are provided as attributes in the ``contrib_colormaps`` namespace, with long names prefixed with ``b_``. E.g. ``rainforest`` can be accessed as ``cc.b_sample``. These names should tab complete once ``cc`` has been imported. Because Bokeh palettes are just Python lists, you can always reverse them using normal Python syntax, e.g. ``list(reversed(cc.b_sample))``, or use subsets of them with slice notation, e.g. ``cc.b_sample[25:]``. If you want to access the palettes by string name, they are also collected into a dictionary named ``palette``, so you can use ``cc.palette[\"sample\"]`` or ``cc.palette.sample``; whichever is more convenient.\n", "\n", "The Matplotlib colormaps are also provided as tab-completable attributes, but consistently with a prefix ``m_``, e.g. ``cc.m_sample``. Already reversed versions are also available, as ``cc.m_sample_r``. The same colormaps are also registered with matplotlib's string-based dictionary with the prefix ``cc_``, making them available by name within various matplotlib functions (e.g. ``cc_sample``, ``cc_sample_r``). Finally, if you want to access the colormaps by string name without using Matplotlib's registry, they are also stored in the ``cc.cm`` dictionary, e.g. ``cc.cm[\"sample\"]`` or ``cc.cm[\"sample_r\"]``." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we show importing sample and printing the first 5 colors. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cc.b_sample[:5]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "[cc.m_sample(i) for i in range(5)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Contributing\n", "\n", "To contribute a new colormap, see the contributing section of the [README](https://github.com/pyviz/contrib_colormaps#contributing). To learn more about how to use the various colormaps and what each is for, see the [colormaps section](colormaps)." ] } ], "metadata": { "language_info": { "name": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 4 }