mirror of
https://github.com/donnemartin/data-science-ipython-notebooks.git
synced 2024-03-22 13:30:56 +08:00
570 lines
425 KiB
Python
570 lines
425 KiB
Python
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"<!--BOOK_INFORMATION-->\n",
|
||
|
"<img align=\"left\" style=\"padding-right:10px;\" src=\"figures/PDSH-cover-small.png\">\n",
|
||
|
"*This notebook contains an excerpt from the [Python Data Science Handbook](http://shop.oreilly.com/product/0636920034919.do) by Jake VanderPlas; the content is available [on GitHub](https://github.com/jakevdp/PythonDataScienceHandbook).*\n",
|
||
|
"\n",
|
||
|
"*The text is released under the [CC-BY-NC-ND license](https://creativecommons.org/licenses/by-nc-nd/3.0/us/legalcode), and code is released under the [MIT license](https://opensource.org/licenses/MIT). If you find this content useful, please consider supporting the work by [buying the book](http://shop.oreilly.com/product/0636920034919.do)!*\n",
|
||
|
"\n",
|
||
|
"*No changes were made to the contents of this notebook from the original.*"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"<!--NAVIGATION-->\n",
|
||
|
"< [Customizing Plot Legends](04.06-Customizing-Legends.ipynb) | [Contents](Index.ipynb) | [Multiple Subplots](04.08-Multiple-Subplots.ipynb) >"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# Customizing Colorbars"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Plot legends identify discrete labels of discrete points.\n",
|
||
|
"For continuous labels based on the color of points, lines, or regions, a labeled colorbar can be a great tool.\n",
|
||
|
"In Matplotlib, a colorbar is a separate axes that can provide a key for the meaning of colors in a plot.\n",
|
||
|
"Because the book is printed in black-and-white, this section has an accompanying online supplement where you can view the figures in full color (https://github.com/jakevdp/PythonDataScienceHandbook).\n",
|
||
|
"We'll start by setting up the notebook for plotting and importing the functions we will use:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 1,
|
||
|
"metadata": {
|
||
|
"collapsed": true
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"import matplotlib.pyplot as plt\n",
|
||
|
"plt.style.use('classic')"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 2,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"%matplotlib inline\n",
|
||
|
"import numpy as np"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"As we have seen several times throughout this section, the simplest colorbar can be created with the ``plt.colorbar`` function:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 3,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAUMAAAEACAYAAAAp/xTFAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzsvX2sfdta1/cZc4z5svbvnHs55yCXFBTToq3GWGIQSLDl\nQGmlmkD/wpfGqDeYprYNiU3LS2wKCS0vscWqNSlWq0UTJNoE+keVknhsS0HEYjRcUaqFlAtcuPcc\nrvf8fnvNOceYo3+M8czxzLHm2nv/9l733H3T/SRzz7XWXmu+PPMZ3+d1PMPEGHmiJ3qiJ/r/OzWf\n6gt4oid6oid6DPQEhk/0RE/0RDyB4RM90RM9EfAEhk/0RE/0RMATGD7REz3REwFPYPhET/RETwR8\nCsDQGPNVxpifNsb8E2PMN7zX53+iJ3qiJ9oj817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eZWi2QFgDIlSK\nM1uIwZlUh1k/h/qZTGoT9yAkHfMsH7+ZoI2JXRJS2eMKFDDU+rkFDk1SnFcDGK0YhC/62mprUStP\nl2LMNRDWFmLilSRRMg9tcpWXLivQJzB8zGDIGgTOw51Ig7eWYOek6bUhUgu1AKGeUiIa3sCVTUHr\n6yPYKcWEJBomgChDTWBBx4B6UoZ61e4ykGXixVC910K9HoCNxg/OlAF8g6avs4OQLSdxe7TFI9ag\n7A+KJ4vaR+AomQ1xjzuSuS3oIPaPJFckMqaHvB65MvzZgt8zTq3Dg/q67NfNY5plBX6595p0eEG7\njsE1xDakJEp9mXK+ma28VDVX1sCrTUp2uBE6nzgjgQURsVpm6hB216VQSj/kcIo2ntd4CyXtrAHR\nsVEe0aWwiscqIDw/pCW
|
||
|
"text/plain": [
|
||
|
"<matplotlib.figure.Figure at 0x10c21c630>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"x = np.linspace(0, 10, 1000)\n",
|
||
|
"I = np.sin(x) * np.cos(x[:, np.newaxis])\n",
|
||
|
"\n",
|
||
|
"plt.imshow(I)\n",
|
||
|
"plt.colorbar();"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"We'll now discuss a few ideas for customizing these colorbars and using them effectively in various situations."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Customizing Colorbars\n",
|
||
|
"\n",
|
||
|
"The colormap can be specified using the ``cmap`` argument to the plotting function that is creating the visualization:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 4,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBOR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VsOmbIjkSEb9\n52gbQyM2VSpujkWjIZcX8OpFVUscy5RLa+3SEEkJxt3T6wgZZrg8lcJVzERdKFHOCUYON3OGR9V2\nhZcKK6p0r0K1ABsml7BRtVGBTYGq0efatYtPmoA8CqlPTp7GvptvqYglUywZscSah0FMNHqvmg6F\nRsjFqSkFMHCZXBzJOCJRgsmCQRVpM1Kp7tfy8fCSMVcpqJ5yie0ML1lActsVZrjtnF+OZCrFs9Q2\nTS6ug2XDmOz+iOAKtBGw8rDBpT2qXipwKLhiSNVrcOd7dAj1LysTKxcFegZaLj/XVxYhXdTvEUum\nWnQ4wvkqTlxAYpJw12ftWvmg+Uf9K2ZU/TncjA6RHN5GCCIjWCaYNW3T5AJcVi4ALEjiWh0G6eRm\nRio9iRvp67qnXuaSjKbL+YU51RLXqGKJYQCTSiyZWnHKxfmR+RRtlBGMUzLV5wGjyiVTLFGObDjE\n91bD5zmqJUvfLT28VGq3p2DCN0cogQHg6bfU17RNk0vVGMC8iVsdTo2MnWNbO5U2ohKDUwIMkmmq\nP3aco1x4m8uWXZ+BzJGLdgxX11pflU8ZyehbvCOkkhGLw42SjN7H9yheIn/AP/53daB1VPkV+BlR\nMUuxkrU/k8n169fTN5SX2qbJBcClhgAukwovDphV9NHO4PLgvHRdEeBcwGQdXDuzIxL2yXX4jFCY\nGDNicZ0yM+6Mzk/XLo5QquFS1VZZB3ZtGXiJOnOfIIyq3FHl4nzs4UWDk8ONazu1CjdVGZbaZsml\nF4l693HUcRIdwGygRPq
|
||
|
"text/plain": [
|
||
|
"<matplotlib.figure.Figure at 0x10d902588>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"plt.imshow(I, cmap='gray');"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"All the available colormaps are in the ``plt.cm`` namespace; using IPython's tab-completion will give you a full list of built-in possibilities:\n",
|
||
|
"```\n",
|
||
|
"plt.cm.<TAB>\n",
|
||
|
"```\n",
|
||
|
"But being *able* to choose a colormap is just the first step: more important is how to *decide* among the possibilities!\n",
|
||
|
"The choice turns out to be much more subtle than you might initially expect."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"### Choosing the Colormap\n",
|
||
|
"\n",
|
||
|
"A full treatment of color choice within visualization is beyond the scope of this book, but for entertaining reading on this subject and others, see the article [\"Ten Simple Rules for Better Figures\"](http://journals.plos.org/ploscompbiol/article?id=10.1371/journal.pcbi.1003833).\n",
|
||
|
"Matplotlib's online documentation also has an [interesting discussion](http://Matplotlib.org/1.4.1/users/colormaps.html) of colormap choice.\n",
|
||
|
"\n",
|
||
|
"Broadly, you should be aware of three different categories of colormaps:\n",
|
||
|
"\n",
|
||
|
"- *Sequential colormaps*: These are made up of one continuous sequence of colors (e.g., ``binary`` or ``viridis``).\n",
|
||
|
"- *Divergent colormaps*: These usually contain two distinct colors, which show positive and negative deviations from a mean (e.g., ``RdBu`` or ``PuOr``).\n",
|
||
|
"- *Qualitative colormaps*: these mix colors with no particular sequence (e.g., ``rainbow`` or ``jet``).\n",
|
||
|
"\n",
|
||
|
"The ``jet`` colormap, which was the default in Matplotlib prior to version 2.0, is an example of a qualitative colormap.\n",
|
||
|
"Its status as the default was quite unfortunate, because qualitative maps are often a poor choice for representing quantitative data.\n",
|
||
|
"Among the problems is the fact that qualitative maps usually do not display any uniform progression in brightness as the scale increases.\n",
|
||
|
"\n",
|
||
|
"We can see this by converting the ``jet`` colorbar into black and white:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 5,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"from matplotlib.colors import LinearSegmentedColormap\n",
|
||
|
"\n",
|
||
|
"def grayscale_cmap(cmap):\n",
|
||
|
" \"\"\"Return a grayscale version of the given colormap\"\"\"\n",
|
||
|
" cmap = plt.cm.get_cmap(cmap)\n",
|
||
|
" colors = cmap(np.arange(cmap.N))\n",
|
||
|
" \n",
|
||
|
" # convert RGBA to perceived grayscale luminance\n",
|
||
|
" # cf. http://alienryderflex.com/hsp.html\n",
|
||
|
" RGB_weight = [0.299, 0.587, 0.114]\n",
|
||
|
" luminance = np.sqrt(np.dot(colors[:, :3] ** 2, RGB_weight))\n",
|
||
|
" colors[:, :3] = luminance[:, np.newaxis]\n",
|
||
|
" \n",
|
||
|
" return LinearSegmentedColormap.from_list(cmap.name + \"_gray\", colors, cmap.N)\n",
|
||
|
" \n",
|
||
|
"\n",
|
||
|
"def view_colormap(cmap):\n",
|
||
|
" \"\"\"Plot a colormap with its grayscale equivalent\"\"\"\n",
|
||
|
" cmap = plt.cm.get_cmap(cmap)\n",
|
||
|
" colors = cmap(np.arange(cmap.N))\n",
|
||
|
" \n",
|
||
|
" cmap = grayscale_cmap(cmap)\n",
|
||
|
" grayscale = cmap(np.arange(cmap.N))\n",
|
||
|
" \n",
|
||
|
" fig, ax = plt.subplots(2, figsize=(6, 2),\n",
|
||
|
" subplot_kw=dict(xticks=[], yticks=[]))\n",
|
||
|
" ax[0].imshow([colors], extent=[0, 10, 0, 1])\n",
|
||
|
" ax[1].imshow([grayscale], extent=[0, 10, 0, 1])"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 6,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAV0AAABsCAYAAADJ2WELAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAABCBJREFUeJzt2lFyozgUBdAnyG6yx/noZWVtEZoP29MOAWzS5KUZnVNF\nARIQQK5bKfR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HQBUgkdAESCV3Y4WyTSCbSjmUiDeBkhC7scLaZe9ULx1K9\nAHAyQhcgkdAFSCR0YYezzdyrXjiW6gWAkxG6sMPZZu5VLxxL9QLAyQhdgERCFyCR0IUdzjZzr3rh\nWKoXAE5G6MIOZ5u5V71wLNULACcjdAESCV2AREIXdjjbzL3qhWOpXoBkZ5tEMpF2LBNpACcjdAES\nCV2ARGXrG0Uppd+PNwB/oLW2OOu2GboAHMvnBYBEQhcgkdAFSCR0ARIJXYBE/wLuUZ/r1NIE0gAA\nAABJRU5ErkJggg==\n",
|
||
|
"text/plain": [
|
||
|
"<matplotlib.figure.Figure at 0x11656ceb8>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"view_colormap('jet')"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Notice the bright stripes in the grayscale image.\n",
|
||
|
"Even in full color, this uneven brightness means that the eye will be drawn to certain portions of the color range, which will potentially emphasize unimportant parts of the dataset.\n",
|
||
|
"It's better to use a colormap such as ``viridis`` (the default as of Matplotlib 2.0), which is specifically constructed to have an even brightness variation across the range.\n",
|
||
|
"Thus it not only plays well with our color perception, but also will translate well to grayscale printing:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 7,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAV0AAABsCAYAAADJ2WELAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAABBlJREFUeJzt2lGSokgQBuBMevYS+9Qn6FPMEeb8exRrH1QERxBUcmT6\n+yIMyaIqwY6O/wEqW2sBQI3uT98AwHcidAEKCV2AQkIXoJDQBSj0Y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f0G1l55CF6CQ0AUWefddAXvp\nKXQBCgldgEJCF6CQ0AUWefddAXvpKXQBCgldYJF33xWwl55CF6CQ0AUoJHQBCgldYJF33xWwl55C\nF6CQ0AUWefddAXvpKXQBCgldgEJCF6CQ0AUWefddAXvpKXSBRd79BdVeegpdgEJCF6CQ0AUolHPP\nKjLz9Q9HAL6B1trNt2+zoQvAa3m8AFBI6AIUEroAhYQuQCGhC1Dof8izEtxDEuLdAAAAAElFTkSu\nQmCC\n",
|
||
|
"text/plain": [
|
||
|
"<matplotlib.figure.Figure at 0x1165767f0>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"view_colormap('viridis')"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"If you favor rainbow schemes, another good option for continuous data is the ``cubehelix`` colormap:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 8,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAV0AAABsCAYAAADJ2WELAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAABDNJREFUeJzt3OtyokAQBtDuMe//yrM/GECNcSUFLe6ek7KkGex4gW9T\nztRm7z0AqNHe/QQA/idCF6CQ0AUoJHQBCgldgEJfzwYz09IGgF/oveej/U9Dd5aR0XL6o7hFi8yM\nNv5IzmzRskVGRmabjh51u65zHY8Hx0eu95HtZnv+PZEZ0dYeU51T3XI9PiMiW/RlPKbHZa77Mr7X\n7WrfTR03dYynMO/ry1hEZF+e3nUdrS+PyeyP64jI1iNz3Ma+yD5e+jgm+8NbG/9GttGzZZ9+9byd\nPVqs+6aX0qMtx8TV+BgbL/c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NEGsDHEroAhYQuQKGvVw7qvR/yPQnA/yaFKUAdXy8AFBK6AIWELkAh\noQtQSOgCFPoDAVPczXaO6jAAAAAASUVORK5CYII=\n",
|
||
|
"text/plain": [
|
||
|
"<matplotlib.figure.Figure at 0x1165caf60>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"view_colormap('cubehelix')"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"For other situations, such as showing positive and negative deviations from some mean, dual-color colorbars such as ``RdBu`` (*Red-Blue*) can be useful. However, as you can see in the following figure, it's important to note that the positive-negative information will be lost upon translation to grayscale!"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 9,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAV0AAABsCAYAAADJ2WELAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAABXVJREFUeJzt3GuO2zYUBtBLOfvoOrKjrqpLShdUWOwPPaz4NZmMfENa\n5wCGTIm6pungw0AkUmqtAUCO4U8PAOBIhC5AIqELkEjoAiQSugCJvj27WEqxtQHgN9Ray73zT0M3\nIuLv+CuGiBjK9GfxqZTpfSlze35f5mtL31Kma7F5f3V9iLipcf0ZpxJRSsQwDFGGEuVUIpb2qUzn\nhogyt4dhul5OU/9hLjrM7VLK5r7pNUwDuLSnD49huO47rMdY2/PnnIaIsulz2vYpMQxDxKb/UmM4\nDdOYlpqnuU8p63fYtpcaUab3Ue61T5t+JWJpb/rcbc/95gmPUrbt06X/cq5sjlGiXrWjDFHL/ANO\nP1pEmfvF7f0/9V/ODcu5n++ZakSMNaJGRF2PNep8frpe1+v3zl2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AImELkAioQuQSOgCJBK6b267KHW0mn/iM76il3ltfR4XrY5T\n6L657Sr90Wr+ic/4il7mtfV5XLQ6TqELkEjoAiQSugCJhO6b62UhxUJaP/Pa+jwuWh2n0AVIJHTf\nXC+r13Yv9DOvrc/jotVxCl2AREIXIJHQBUgkdN9cL6vXdi/0M6+tz+Oi1XEKXYBEQvfN9bJ6bfdC\nP/Pa+jwuWh2n0AVIJHQBEgldgERC9831snpt90I/89r6PC5aHafQBUgkdN9cL6vXdi/0M6+tz+Oi\n1XEKXYBEQhcgkdAFSCR031wvq9d2L/Qzr63P46LVcQrdN9fLQoqFtH7mtfV5XLQ6TqELkEjoAiQS\nugCJyrPnHqWUNh+KADSu1np3Je9p6AKwL48XABIJXYBEQhcgkdAFSCR0ARL9D81xLwk8NAnaAAAA\nAElFTkSuQmCC\n",
|
||
|
"text/plain": [
|
||
|
"<matplotlib.figure.Figure at 0x10c20e908>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"view_colormap('RdBu')"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"We'll see examples of using some of these color maps as we continue.\n",
|
||
|
"\n",
|
||
|
"There are a large number of colormaps available in Matplotlib; to see a list of them, you can use IPython to explore the ``plt.cm`` submodule. For a more principled approach to colors in Python, you can refer to the tools and documentation within the Seaborn library (see [Visualization With Seaborn](04.14-Visualization-With-Seaborn.ipynb))."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"### Color limits and extensions\n",
|
||
|
"\n",
|
||
|
"Matplotlib allows for a large range of colorbar customization.\n",
|
||
|
"The colorbar itself is simply an instance of ``plt.Axes``, so all of the axes and tick formatting tricks we've learned are applicable.\n",
|
||
|
"The colorbar has some interesting flexibility: for example, we can narrow the color limits and indicate the out-of-bounds values with a triangular arrow at the top and bottom by setting the ``extend`` property.\n",
|
||
|
"This might come in handy, for example, if displaying an image that is subject to noise:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 10,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAlEAAADkCAYAAAC45HExAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzsvXm8JEd1JvqdWDKr7u19V3eLFlpaQggQAiRAG5tBgAGb\nZ7MYjw3e8T7jBTDY894zGAPPA7aZed4HPDYGz9hjhBdswMbYIEBgsLF2JNDSLbXU6vUuVZkZceaP\niMiMzMpabt3bUjfU+f3yV1mZkZGReTLjfPmdEyeImTGTmcxkJjOZyUxmMpOViXi0GzCTmcxkJjOZ\nyUxmcibKDETNZCYzmclMZjKTmUwhMxA1k5nMZCYzmclMZjKFzEDUTGYyk5nMZCYzmckUMgNRM5nJ\nTGYyk5nMZCZTyAxEzWQmM5nJTGYyk5lMIY84iCKi64joViK6nYje8EiffyYzmclMppVZ/zWTmcwk\nFnok80QRkQBwO4DnAjgI4EYAr2LmWx+xRsxkJjOZyRQy679mMpOZNOWRZqIuB3AHM9/NzDmADwJ4\n2SPchpnMZCYzmUZm/ddMZjKTmjzSIGoPgHuj//f5bTOZyUxmcrrLrP+ayUxmUpPTMrCciGZz0cxk\nJpi9C2eizHQ2k5k4We27QER7ieijRLRjrdq01qIe4fMdAPCY6P9ev21AZh3RTGYynVCynpEvjCt2\nNzOf8wg05xtJZv3XTGbyCIrYdtG99uhdEFv3HwJAj3Z72uSRBlE3AjifiPYBuB/AqwC8uq3g8tIS\nQi/EDDBQ/rcMsDUAVUSajcqW26I/w3o0O6arEw21vfPtb8Mb3vTmRpmqUFgV5X+q1UPlMfX/YNv6\nS+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
|
"<matplotlib.figure.Figure at 0x10d93be48>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# make noise in 1% of the image pixels\n",
|
||
|
"speckles = (np.random.random(I.shape) < 0.01)\n",
|
||
|
"I[speckles] = np.random.normal(0, 3, np.count_nonzero(speckles))\n",
|
||
|
"\n",
|
||
|
"plt.figure(figsize=(10, 3.5))\n",
|
||
|
"\n",
|
||
|
"plt.subplot(1, 2, 1)\n",
|
||
|
"plt.imshow(I, cmap='RdBu')\n",
|
||
|
"plt.colorbar()\n",
|
||
|
"\n",
|
||
|
"plt.subplot(1, 2, 2)\n",
|
||
|
"plt.imshow(I, cmap='RdBu')\n",
|
||
|
"plt.colorbar(extend='both')\n",
|
||
|
"plt.clim(-1, 1);"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Notice that in the left panel, the default color limits respond to the noisy pixels, and the range of the noise completely washes-out the pattern we are interested in.\n",
|
||
|
"In the right panel, we manually set the color limits, and add extensions to indicate values which are above or below those limits.\n",
|
||
|
"The result is a much more useful visualization of our data."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"### Discrete Color Bars\n",
|
||
|
"\n",
|
||
|
"Colormaps are by default continuous, but sometimes you'd like to represent discrete values.\n",
|
||
|
"The easiest way to do this is to use the ``plt.cm.get_cmap()`` function, and pass the name of a suitable colormap along with the number of desired bins:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 11,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAUMAAAEACAYAAAAp/xTFAAAABHNCSVQICA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iJhAzGa4qjfDKTFrij1+LMNTgg3MbCLwByg2btQfv0oBzM8v\nwG6P+PM1940q5jeUY8rtw5Q7YmqoL82SnQr7/AF0DsXHL/PAsllKjQqndI5a8/OjMxrRQIDpoyrG\nRCI+kbaictiKIqaW6RMHQP1+gLJtpsRihcVZIJkIggb8cQttYnwzk1FuNg6Hesfwm7ax+9i2q6Pt\nlpMSzvFXCmkvhkYQmPfAZM+AY0UDDM4uJok/QLF+vbT90uf1wmLlHwWJhTBEx9gMGgrVp8ZiMTQ9\njxl3YhaAYlFa3dxS4cSJgSnZNizqi7LQOTqLpqaacMJXJSEEBCG89+oa/OFw/EM3d4u+cEIpIawr\nzUbXkCvO33DtWrVJofk5MzKdsGOrgRByO4BvQpi5PkYpfSRmey6AX0DImm8G8HVK6U/0njetp8kh\n5LJD88Ba+TWaWBcWh8OGDJkpnxohlGJkbt4w1xkjhDDAyKoDANn2aEFQWt3UIoQmAnSORkauavsM\nSwgTSddQvHlBbz9PBzjLCX8CQCuldAuAmwB8nRCie2CX1mI42hNZRFDbUWITO2jhXbtWcrcVJwjI\nz3dg57ZquMYSb6c5cCl1kjeYJEZhruDiSp7DKrtoosc2ygr9KyjQlgT47u2CjcQ7oT4iSStS/ftd\nTamVJswAeMoJUwChKU8OgDFKqe6ndVqLYVFttCFZjSC2desXol83X1S9T3a2HXV1RZh0e5FfLL0a\nrhe/aHWQJTCzCr6Si8G
|
||
|
"text/plain": [
|
||
|
"<matplotlib.figure.Figure at 0x116d71940>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"plt.imshow(I, cmap=plt.cm.get_cmap('Blues', 6))\n",
|
||
|
"plt.colorbar()\n",
|
||
|
"plt.clim(-1, 1);"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"The discrete version of a colormap can be used just like any other colormap."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Example: Handwritten Digits\n",
|
||
|
"\n",
|
||
|
"For an example of where this might be useful, let's look at an interesting visualization of some hand written digits data.\n",
|
||
|
"This data is included in Scikit-Learn, and consists of nearly 2,000 $8 \\times 8$ thumbnails showing various hand-written digits.\n",
|
||
|
"\n",
|
||
|
"For now, let's start by downloading the digits data and visualizing several of the example images with ``plt.imshow()``:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 12,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAV0AAAFdCAYAAACgiL63AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzsfWd3I0mSpEEQIKEVQVmqS8z17Nzu7P3/33C9s7uvpren\nRXVRQmsNAvehz7wsg8kqAkiwu3bh7+VDiyoyEBlh4WHubh5aLpfY2c52trOdPY2Ff+8B7GxnO9vZ\n/yTbge7OdraznT2h7UB3Zzvb2c6e0Hagu7Od7WxnT2g70N3Zzna2sye0HejubGc729kTWvRz/zMU\nCv3h8smWy2XI77/vxrq+fS3jBHZj3ZZ9LWP9WsYJPDzWz4Lu//+Lvv+93++j0WigXq/b5+XlJS4u\nLnBxcYHLy0t0Oh1ks1lks1nkcjlks1kcHR3h/PzcntPTU0SjUezt7dkTiUQAAKFQCKFQ6N4/rzpW\nP/v+++/x97//He/fv8ff//53XFxcYD6fe55CoWDjPDs7w9nZGQqFAvL5PAqFAgqFAhKJhO/PX3es\n/O/8nM/naLfb6HQ66HQ6aLfb+PDhA/7xj3/ghx9+wA8//ICbmxskk0kkEgn7fPPmDf785z/jn/7p\nn/BP//RPePny5drj9Hs6nQ6ur69xc3ODm5sbXF9f4+rqC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gv5TYIur+ekPBKJBKLRKMrlMo6Pj3FycoKzszPk83nzbDmnkUjEgnz0QBk3\nCfKmpkZqpFar4erqCh8/fkS9Xvc4CUp7xeNxz54jFmgAM/DsBU4SObnBYGCeLnkOpoWcn5/jm2++\nwdnZmUUh+VSrVQ+f0u/
|
||
|
"text/plain": [
|
||
|
"<matplotlib.figure.Figure at 0x10bb38d30>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# load images of the digits 0 through 5 and visualize several of them\n",
|
||
|
"from sklearn.datasets import load_digits\n",
|
||
|
"digits = load_digits(n_class=6)\n",
|
||
|
"\n",
|
||
|
"fig, ax = plt.subplots(8, 8, figsize=(6, 6))\n",
|
||
|
"for i, axi in enumerate(ax.flat):\n",
|
||
|
" axi.imshow(digits.images[i], cmap='binary')\n",
|
||
|
" axi.set(xticks=[], yticks=[])"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Because each digit is defined by the hue of its 64 pixels, we can consider each digit to be a point lying in 64-dimensional space: each dimension represents the brightness of one pixel.\n",
|
||
|
"But visualizing relationships in such high-dimensional spaces can be extremely difficult.\n",
|
||
|
"One way to approach this is to use a *dimensionality reduction* technique such as manifold learning to reduce the dimensionality of the data while maintaining the relationships of interest.\n",
|
||
|
"Dimensionality reduction is an example of unsupervised machine learning, and we will discuss it in more detail in [What Is Machine Learning?](05.01-What-Is-Machine-Learning.ipynb).\n",
|
||
|
"\n",
|
||
|
"Deferring the discussion of these details, let's take a look at a two-dimensional manifold learning projection of this digits data (see [In-Depth: Manifold Learning](05.10-Manifold-Learning.ipynb) for details):"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 13,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# project the digits into 2 dimensions using IsoMap\n",
|
||
|
"from sklearn.manifold import Isomap\n",
|
||
|
"iso = Isomap(n_components=2)\n",
|
||
|
"projection = iso.fit_transform(digits.data)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"We'll use our discrete colormap to view the results, setting the ``ticks`` and ``clim`` to improve the aesthetics of the resulting colorbar:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 14,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAW8AAAEACAYAAAB8nvebAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzsnXd0HNX5sJ+7vWi12pW06lZz790UG4whhpCE4hBaKEkc\nPoJxQhJSTEkgJIQ4tEACSX6BQCgJNaH3YrDBuGAbN7nIVu/SSlpp++7c74+VV1pLMrIt2bI9zzk6\nZ+fOvTN3RrvvvPPetwgpJSoqKioqxxaaoz0BFRUVFZWDRxXeKioqKscgqvBWUVFROQZRhbeKiorK\nMYgqvFVUVFSOQVThraKionIMctjCWwiRK4T4QAixTQixRQjxo652hxDiHSHETiHE20IIe48xNwkh\ndgshSoQQCw93DioqKionGuJw/byFEJlAppRykxAiCfgcOB/4LtAipfyjEOKXgENKuUwIMR54GpgF\n5ALvAaOk6nCuoqKiMmAOW/OWUtZLKTd1fe4ESogJ5fOBf3V1+xdwQdfn84BnpJQRKWU5sBuYfbjz\nUFFRUTmRGFSbtxCiAJgKfAZkSCkbICbgAVdXtxygqsewmq42FRUVFZUBMmjCu8tk8gJwQ5cGvr8Z\nRDWLqKioqAwSusE4iBBCR0xwPymlfLmruUEIkSGlbOiyizd2tdcAeT2G53a19XVcVeCrqKgMGCml\nONSxBQU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QCAU/3fBQFRVGQUpLUj6t5W1sbXq/3\n8G+SyjGBEEIjhNgghHhlKM+jat4qwwKv14vX5yMSbSQajcTbk2wOdpZsxOF0oShRzOaYhDSZbWzZ\nvjue/rXVE0Zv6LaXRxQ
|
||
|
"text/plain": [
|
||
|
"<matplotlib.figure.Figure at 0x1139266d8>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# plot the results\n",
|
||
|
"plt.scatter(projection[:, 0], projection[:, 1], lw=0.1,\n",
|
||
|
" c=digits.target, cmap=plt.cm.get_cmap('cubehelix', 6))\n",
|
||
|
"plt.colorbar(ticks=range(6), label='digit value')\n",
|
||
|
"plt.clim(-0.5, 5.5)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"The projection also gives us some interesting insights on the relationships within the dataset: for example, the ranges of 5 and 3 nearly overlap in this projection, indicating that some hand written fives and threes are difficult to distinguish, and therefore more likely to be confused by an automated classification algorithm.\n",
|
||
|
"Other values, like 0 and 1, are more distantly separated, and therefore much less likely to be confused.\n",
|
||
|
"This observation agrees with our intuition, because 5 and 3 look much more similar than do 0 and 1.\n",
|
||
|
"\n",
|
||
|
"We'll return to manifold learning and to digit classification in [Chapter 5](05.00-Machine-Learning.ipynb)."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"<!--NAVIGATION-->\n",
|
||
|
"< [Customizing Plot Legends](04.06-Customizing-Legends.ipynb) | [Contents](Index.ipynb) | [Multiple Subplots](04.08-Multiple-Subplots.ipynb) >"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"anaconda-cloud": {},
|
||
|
"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.4.3"
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 0
|
||
|
}
|