{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Colormap Choices {#colormap_example}\n================\n\nUse a Matplotlib, Colorcet, cmocean, or custom colormap when plotting\nscalar values.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from matplotlib.colors import ListedColormap\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nimport pyvista as pv\nfrom pyvista import examples" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Any colormap built for `matplotlib`, `colorcet`, or `cmocean` is fully\ncompatible with PyVista. Colormaps are typically specified by passing\nthe string name of the colormap to the plotting routine via the `cmap`\nargument.\n\nSee [Matplotlib\\'s complete list of available\ncolormaps](https://matplotlib.org/tutorials/colors/colormaps.html),\n[Colorcet\\'s complete\nlist](https://colorcet.holoviz.org/user_guide/index.html), and\n[cmocean\\'s complete list](https://matplotlib.org/cmocean/).\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Custom Made Colormaps\n=====================\n\nTo get started using a custom colormap, download some data with scalar\nvalues to plot.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mesh = examples.download_st_helens().warp_by_scalar()\n# Add scalar array with range (0, 100) that correlates with elevation\nmesh['values'] = pv.plotting.tools.normalize(mesh['Elevation']) * 100" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Build a custom colormap - here we make a colormap with 5 discrete colors\nand we specify the ranges where those colors fall:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Define the colors we want to use\nblue = np.array([12 / 256, 238 / 256, 246 / 256, 1.0])\nblack = np.array([11 / 256, 11 / 256, 11 / 256, 1.0])\ngrey = np.array([189 / 256, 189 / 256, 189 / 256, 1.0])\nyellow = np.array([255 / 256, 247 / 256, 0 / 256, 1.0])\nred = np.array([1.0, 0.0, 0.0, 1.0])\n\nmapping = np.linspace(mesh['values'].min(), mesh['values'].max(), 256)\nnewcolors = np.empty((256, 4))\nnewcolors[mapping >= 80] = red\nnewcolors[mapping < 80] = grey\nnewcolors[mapping < 55] = yellow\nnewcolors[mapping < 30] = blue\nnewcolors[mapping < 1] = black\n\n# Make the colormap from the listed colors\nmy_colormap = ListedColormap(newcolors)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Simply pass the colormap to the plotting routine.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mesh.plot(scalars='values', cmap=my_colormap)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or you could make a simple colormap\\... any Matplotlib colormap can be\npassed to PyVista.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "boring_cmap = plt.cm.get_cmap(\"viridis\", 5)\nmesh.plot(scalars='values', cmap=boring_cmap)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also pass a list of color strings to the color map. This\napproach divides up the colormap into 5 equal parts.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mesh.plot(scalars=mesh['values'], cmap=['black', 'blue', 'yellow', 'grey', 'red'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you still wish to have control of the separation of values, you can\ndo this by creating a scalar array and passing that to the plotter along\nwith the colormap\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "scalars = np.empty(mesh.n_points)\nscalars[mesh['values'] >= 80] = 4 # red\nscalars[mesh['values'] < 80] = 3 # grey\nscalars[mesh['values'] < 55] = 2 # yellow\nscalars[mesh['values'] < 30] = 1 # blue\nscalars[mesh['values'] < 1] = 0 # black\n\nmesh.plot(scalars=scalars, cmap=['black', 'blue', 'yellow', 'grey', 'red'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Matplotlib vs. Colorcet\n=======================\n\nLet\\'s compare Colorcet\\'s perceptually uniform \\\"fire\\\" colormap to\nMatplotlib\\'s \\\"hot\\\" colormap much like the example on the [first page\nof Colorcet\\'s docs](https://colorcet.holoviz.org/index.html).\n\nThe \\\"hot\\\" version washes out detail at the high end, as if the image\nis overexposed, while \\\"fire\\\" makes detail visible throughout the data\nrange.\n\nPlease note that in order to use Colorcet\\'s colormaps including\n\\\"fire\\\", you must have Colorcet installed in your Python environment:\n`pip install colorcet`\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "p = pv.Plotter(shape=(2, 2), border=False)\np.subplot(0, 0)\np.add_mesh(\n mesh,\n scalars='Elevation',\n cmap=\"fire\",\n lighting=True,\n scalar_bar_args={'title': \"Colorcet Fire\"},\n)\n\np.subplot(0, 1)\np.add_mesh(\n mesh,\n scalars='Elevation',\n cmap=\"fire\",\n lighting=False,\n scalar_bar_args={'title': \"Colorcet Fire (No Lighting)\"},\n)\n\np.subplot(1, 0)\np.add_mesh(\n mesh,\n scalars='Elevation',\n cmap=\"hot\",\n lighting=True,\n scalar_bar_args={'title': \"Matplotlib Hot\"},\n)\n\np.subplot(1, 1)\np.add_mesh(\n mesh,\n scalars='Elevation',\n cmap=\"hot\",\n lighting=False,\n scalar_bar_args={'title': \"Matplotlib Hot (No Lighting)\"},\n)\n\np.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.2" } }, "nbformat": 4, "nbformat_minor": 0 }