mirror of
https://github.com/donnemartin/data-science-ipython-notebooks.git
synced 2024-03-22 13:30:56 +08:00
436 lines
214 KiB
Python
436 lines
214 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",
|
||
|
"< [Histograms, Binnings, and Density](04.05-Histograms-and-Binnings.ipynb) | [Contents](Index.ipynb) | [Customizing Colorbars](04.07-Customizing-Colorbars.ipynb) >"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# Customizing Plot Legends"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Plot legends give meaning to a visualization, assigning meaning to the various plot elements.\n",
|
||
|
"We previously saw how to create a simple legend; here we'll take a look at customizing the placement and aesthetics of the legend in Matplotlib.\n",
|
||
|
"\n",
|
||
|
"The simplest legend can be created with the ``plt.legend()`` command, which automatically creates a legend for any labeled plot elements:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"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": true
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"%matplotlib inline\n",
|
||
|
"import numpy as np"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 3,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXYAAAD7CAYAAAB+B7/XAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XuczXX+B/DXe2ZkyQiDFBoil4pEKoSj2khrs1sppdJW\ntqJYtkV2FyVd+WUrpa2Emu5tLkUJE9qKZMgltzBuYciduTjv3x/vcVkNM3PO95zvOZ95PR+P8zAz\nvvP9vs+Zc97ncz6f9+fzEVUFERG5I8HvAIiIyFtM7EREjmFiJyJyDBM7EZFjmNiJiBzDxE5E5Jik\naF1IRFhXSUQUAlWV4hwf1Ra7qvKmisGDB/seQ6zc+FjwseBjcepbKNgVQ0TkGCZ2IiLHMLH7IBAI\n+B1CzOBjcQwfi2P4WIRHQu3DKfaFRDRa1yIicoWIQIs5eBp2VYyIlAYwG8Bp+ef7QFWHhnteIopt\ntWrVwvr16/0OwxmpqalYt26dJ+fypMUuImVV9YCIJAL4CsBDqjrvhGPYYidySH5L0u8wnHGyxzOU\nFrsnfeyqeiD/y9KwVjv/2kREPvEksYtIgogsBPAzgOmqOt+L8xIRUfF51WIPqurFA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anvk8GFL5G++CUydCjRuDPzud/ZkatoUOO204p8zLw9YtAiYPdvuxrx5wNVXWyPr\n+ut9ea1RrNm/H/joI2sJzJ8P3Hcf8PTT4Z934ULgww+tpb9uHXDjjfbEa9XK3hQiaOVK4LXX7PJ5\nefZcv+oqoGVLS+ShyMy
|
||
|
"text/plain": [
|
||
|
"<matplotlib.figure.Figure at 0x10c21e9e8>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"x = np.linspace(0, 10, 1000)\n",
|
||
|
"fig, ax = plt.subplots()\n",
|
||
|
"ax.plot(x, np.sin(x), '-b', label='Sine')\n",
|
||
|
"ax.plot(x, np.cos(x), '--r', label='Cosine')\n",
|
||
|
"ax.axis('equal')\n",
|
||
|
"leg = ax.legend();"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"But there are many ways we might want to customize such a legend.\n",
|
||
|
"For example, we can specify the location and turn off the frame:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 4,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXYAAAD7CAYAAAB+B7/XAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XuczeX2B/DPmhk5ZIRxK+SaS0UuqRC26kTk0KmUIjqV\nU1FEHeQUStKFX06lOJVQ000d15AwoRvJkCG3ZNzCkDtz2+v3xxqXNGZmX797f+fzfr32y8z4zve7\n9p69137286zneURVQURE7hHjdABERBRcTOxERC7DxE5E5DJM7ERELsPETkTkMkzsREQuExeuC4kI\n6yqJiPygquLL8WFtsasqb6oYOnSo4zFEyo2PBR8LPhZ53/zB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c02aBKxfD/z1r8C111rGDDNVew3MmAF89ZW9LipWtLtXty5wxRX2OipTBoiLs+MzMoB9+6zx\ntHWrvQ7XrLGft2oFtG4NdOpkSd/tkpKSkJSUFNA5RFWDEoyIVAMwU1Xrn+P/tcDXWrsWmD0b6Nr1\n3E/gIMnOtkT+3nvAnDlAgwbAzTfbk6lxY+C883w/Z1YWsGoVsHix3Y1ly4AbbrBGVqdOjrzWKNIc\nPQp89pm1BJYvBx58EHjxxcDPu3Il8Omn1tL/9Vfgttvsideihb0phNCGDcDbb9vls7LsuX799UDz\n5pbI/ZGaao2sBQvsjaJ6deD224F77rE3i8JARKCqvn1uUdWAbwASAewEkA4gFcC9uRyjBbZ+ver9\n96uWLq36t7+pLlyo6vU
|
||
|
"text/plain": [
|
||
|
"<matplotlib.figure.Figure at 0x10c21e9e8>"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 4,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"ax.legend(loc='upper left', frameon=False)\n",
|
||
|
"fig"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"We can use the ``ncol`` command to specify the number of columns in the legend:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 5,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXYAAAD7CAYAAAB+B7/XAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3Xd4lFX2B/DvCSALEgxVpINUlaaiUh2UFSysuKyuICqu\nyqqo8ANdQVSCIioKK64iuiIqGlHRpSkoAhGwUSQgRZpIaAKhSQ1J5vz+OKGIIWTqO3Pz/TzPPCTh\nzf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TvX5v63aOFZfOGycqW1+c47D3jjDaBkSa8jOr1gEnt8lDvmwe8HkpOBe+8F\nJk0Cevd2M6kDdr969ABmzbL73KcPkJXldVSF1N69wPDhBSoPjBfz5gHNm9tSGhMn5jH9f/9+Wx/g\njTc8iS+cGjYEvv3WyiRbtgTWr/c6osiIy8S+d689z2bOBBYuLGBDYtu2iMcVaY0a2bIiq1fbGk/b\nt3sdUSGzapVlwJ9/tkkRcU4VePlla6m/8YZ96s1zOZmrr7ZW+/PPAw88EPetihIlgHfesZr3Fi2A\nGTO8jij84i6xr1tnkyRq1LDEXqCFgDIz7e152LC4b2mVKWPdT61bW45Zys0Io2P2bFsZ7pFHgFde\nifuV3LKybEDxtdeAb76x3J2v+vWB77+3F2CHDrYUYxwTsa7b8eNtQbGRI72OKMwCLaMJ9oYwlDt+\n840tojVqVBC/nJ5uc45vvTW6i1pEUEqKlR5//rnXkThuzBhb4cqR+ep79qhedZXq1Ver/vZbgL+c\nna36yCOqM2dGJDYv/PK
|
||
|
"text/plain": [
|
||
|
"<matplotlib.figure.Figure at 0x10c21e9e8>"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 5,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"ax.legend(frameon=False, loc='lower center', ncol=2)\n",
|
||
|
"fig"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"We can use a rounded box (``fancybox``) or add a shadow, change the transparency (alpha value) of the frame, or change the padding around the text:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 6,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXYAAAD7CAYAAAB+B7/XAAAABHNCSVQICAgIfAhkiAAA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na222EUaZmZmafdMCQQTOxERVzlypWRlpbmdhhF2s8/\n//ynDT2CxcROVMTdc8896Nu3L9LT090OpcjJzMzEDz/8gMTERHTu3BmqimLFgq+m7tgOSkQUnZ54\n4glce+21iIuLQ05OjtvhFCkxMTGoUqUKbr31VjRo0AClSpVC2bJlgz5v0PXYC3wh1mMniliqikWL\nFmHu3LlQ1Xw3myBnqSrKly+PxMTEvyT2QOqxM7ET0TE5OTnIyMhwO4wiJzY2FiVKlDjlGyoTOxGR\nxwSS2Dl4SkTkMUzsREQew8ROROQxTOxERB7DxE5E5DFM7EREHsPETkTkMY4kdhF5W0S2i8hSJ85H\nRESBc6rFPhZAO4fORUREQXAksavqHAB/OHEuIiIKDvvYiYg8Jrxle9u3B844AzjnHKBGDaB2bSAx\nEYjQSnL79wPLlgErVwJr1gA7dgC7dtnPjzrjDKBSJaByZeD884EGDYALLwQqVHAvbvKQb74Bxo4F\nDhywW04O8Le/Ad26Affc43Z0BZKdDaxadfy2ebO9jvbssf8TAYoXBypWtNfSuecCF1xgt/POi9j0\nENHCmtiTKlYEMjOBtWvh+/13+FasAG68MZwh5CkjA5g+HZg6FZg9G1i71p5cDRoA9erZrWJFoGxZ\ne7KpAgcPAjt3Atu3A/P
|
||
|
"text/plain": [
|
||
|
"<matplotlib.figure.Figure at 0x10c21e9e8>"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 6,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"ax.legend(fancybox=True, framealpha=1, shadow=True, borderpad=1)\n",
|
||
|
"fig"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"For more information on available legend options, see the ``plt.legend`` docstring."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Choosing Elements for the Legend\n",
|
||
|
"\n",
|
||
|
"As we have already seen, the legend includes all labeled elements by default.\n",
|
||
|
"If this is not what is desired, we can fine-tune which elements and labels appear in the legend by using the objects returned by plot commands.\n",
|
||
|
"The ``plt.plot()`` command is able to create multiple lines at once, and returns a list of created line instances.\n",
|
||
|
"Passing any of these to ``plt.legend()`` will tell it which to identify, along with the labels we'd like to specify:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 7,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAX8AAAEACAYAAABbMHZzAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzsnXd8VGX2/z83lTRSJg1IoSUh9CCCFJFeUyhSXVjd1XVX\nXXfdn6uru2tZ16/KKrq6RdcOokAoplFDh9B7TQESAiFl0nsyM+f3x5PBEGYmM3M7zPv1mtcLZu7z\n3JO5d849z3lO4YgIDhw4cODg/sJJbgEcOHDgwIH0OJS/AwcOHNyHOJS/AwcOHNyHOJS/AwcOHNyH\nOJS/AwcOHNyHOJS/AwcOHNyHCKL8OY77kuO4Eo7jzlo45mOO43I5jjvNcdxQIc7rwIEDBw7sQyjL\n/2sA08x9yHHcDAB9iCgKwNMAPhXovA4cOHDgwA4EUf5EdABApYVDkgCsbDv2CABfjuNChDi3AwcO\nHDiwHal8/j0AFLb7/8229xw4cODAgQw4NnwdOHDg4D7ERaLz3AQQ3u7/YW3v3QXHcY5iQw4cOHBg\nI0TE2XK8kJY/1/YyRSqAZQDAcdxDAKqIqMTcREQEIkKzrhn/O/4/dP+gO55MeRK1zbW3P1PKKzub\nMG4cYcQIwo4dBIPB8vFXrxKefJLQrRth3TrLx77++uuS/z0GgwHf3LqFwAMH8NucHBQ3N1s8Xm8w\nIK2sDLFHjmDa6dO40dQkuEy8v4eyMtCSJaBevU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ePWnXLulKzXT8Xr8oKqIxJ07c324fI95u3nh74tv4/dbfGx8mNnHrFvDBB8Dy5SIIZyePPQZ4\neQFffmnf+I9v3kScjw8
|
||
|
"text/plain": [
|
||
|
"<matplotlib.figure.Figure at 0x10d779518>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"y = np.sin(x[:, np.newaxis] + np.pi * np.arange(0, 2, 0.5))\n",
|
||
|
"lines = plt.plot(x, y)\n",
|
||
|
"\n",
|
||
|
"# lines is a list of plt.Line2D instances\n",
|
||
|
"plt.legend(lines[:2], ['first', 'second']);"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"I generally find in practice that it is clearer to use the first method, applying labels to the plot elements you'd like to show on the legend:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 8,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAX8AAAEACAYAAABbMHZzAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzsnXd8VGX2/z83lTRSJg1IoSUh9CCCFJFeUyhSXVjd1XVX\nXXfdn6uru2tZ16/KKrq6RdcOokAoplFDh9B7TQESAiFl0nsyM+f3x5PBEGYmM3M7zPv1mtcLZu7z\n3JO5d849z3lO4YgIDhw4cODg/sJJbgEcOHDgwIH0OJS/AwcOHNyHOJS/AwcOHNyHOJS/AwcOHNyH\nOJS/AwcOHNyHOJS/AwcOHNyHCKL8OY77kuO4Eo7jzlo45mOO43I5jjvNcdxQIc7rwIEDBw7sQyjL\n/2sA08x9yHHcDAB9iCgKwNMAPhXovA4cOHDgwA4EUf5EdABApYVDkgCsbD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FkbvvfcehYaG0rJly0ir1VJ8fDz5+flRQEAAjRs3joiIli5dSk5OTuTp6Uk+\nPj70DwlitNt/r9qWFgppC9zA/ez2MVLbXEs9PuhBhwrt15Zvv000dy4vMQRl1SqiYcNsLx5npFGn\no8isLNqlkOJkhrZN5//ySZNOTyeKiZEuuqczTp5k7h87i9oZDAaasnIKrchSwMOsjecynqPnMp6z\na6ySlT8R0fjx4+mrr74iIqLHH3/8DuXv4uJCr7zyCrW0tFBTUxO98sor9Jvf/Ib0ej3pdDo60K5+\nRM+ePWnXLulKzXT8Xr8oKqIxJ07c324fI95u3nh74tv4/dbfGx8mNnHrFvDBB8Dy5SIIZyePPQZ4\neQFffmnf+I9v3kScjw8
|
||
|
"text/plain": [
|
||
|
"<matplotlib.figure.Figure at 0x11025d908>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"plt.plot(x, y[:, 0], label='first')\n",
|
||
|
"plt.plot(x, y[:, 1], label='second')\n",
|
||
|
"plt.plot(x, y[:, 2:])\n",
|
||
|
"plt.legend(framealpha=1, frameon=True);"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Notice that by default, the legend ignores all elements without a ``label`` attribute set."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Legend for Size of Points\n",
|
||
|
"\n",
|
||
|
"Sometimes the legend defaults are not sufficient for the given visualization.\n",
|
||
|
"For example, perhaps you're be using the size of points to mark certain features of the data, and want to create a legend reflecting this.\n",
|
||
|
"Here is an example where we'll use the size of points to indicate populations of California cities.\n",
|
||
|
"We'd like a legend that specifies the scale of the sizes of the points, and we'll accomplish this by plotting some labeled data with no entries:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 9,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAEZCAYAAACU3p4jAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzsvXd4XNW1sP+uaZpRH3VZsmy5yA1XjG0wxsYQgm2wEQ6B\nJEAoH8kXIOFn8t2bEBJaQkjh5t6QS2LIJcmlxAnFJpgQuo0xphjk3mS5SLa6rK7RjKbs3x/nSB71\nUS8+7/OcR3PO3mfvdc4crdln7bXXEqUUBgYGBgajH9NQC2BgYGBgMDgYCt/AwMDgHMFQ+AYGBgbn\nCIbCNzAwMDhHMBS+gYGBwTmCofANDAwMzhEMhd8LRCQgIhP0z38QkfuDyr4jIiUiUisizn7ud6ze\nrvRnu0Ht14nI+C7K94vIJQPR92hERB4UkeeGWo5gROTPIvJIH87v8hkxGN6cswpfRL4uIjv1B7hQ\nRP4pIotDPL1l8YJS6jtKqUf1Ni3AfwCXK6WilVJV/SmzUuqU3m6vFk+ISIqI/I+IFIlIjYgc1JWS\nQ28/Sil1Uq/bTjEopc5TSm3r84WEJutWEakUEetg9DeAdPhdichSEfHrP+A1InJIRG4ZZNm6RES2\niMhtwceCnxGDkcc5qfBF5F7gN8DPgCQgA3gSuDrUJjo5ngKEAYd6KdeAjNz1tp3Ax2jyLVRKxQBf\nAmKAiQPVb28QkXHAxUAAWN1N3ZH8DBfqP+AxwA+BP4rI1KEWymAUo5Q6pzYgGqgDru2izgXADqAK\nKAR+B1iCygPABP3zn4FHgMlAPeAHaoF39fKLgM/0tj4FLgxqZwvaj852oAGYoB97RD9WC7wJxOn1\nx+l9m/T9W4CDer084FtdXNPPgD3d3JuALsMdQBPg1tv+h15+AliufxY0JZUHlAN/A2L1sjDgOaAi\n6LoTe/Ad/QT4EHgc2Nym7M/A74F/6t/jcsCm180HivXyML1+LL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zd4kA9uJ9o/6OXBaKbU66Pj3\n0bwaEnTlaGBg0A0icgK4XSn1/lDLYtAaEclC83ZSaG/jE4CfKKWeaFPvCbTJ9gbgFqXU7sGQb7Am\nbe9B87aIbj4gIuloC3/aTmoaGBgYjEj0t7O50DLQPY3mPdeCbn6dqJSaLCILgfVob4YDzoCbdHTF\nvhL4nzZF/4nmX2xgYNAzDNPIyOBy4JhSqq0n3hq0NQwopT4FYkQkeTAEGowRfrNib/aaQETWAKeU\nUvsGMJqAgcGoRCnV1WS6wfDhejS34rak0dodu1A/VjrQAg3oCF9EVgGlun0q2C/6PjTvgZaqAymH\ngYGBwWCiB/1bDbw01LIEM9Aj/MXAahFZibYoKArtVWY8sEdfiJGOtmhmge6R0YKIGK+uBgYGIaOU\n6tPgcXxGpMo/1RBq9VKlVGfrHFYAXyilyjsoK6T1Oop0Bmv9wmAt6UVbSt4uhgraMnFnJ+eokcaD\nDz441CL0iJEmr1KGzIPBSJNXKaV0fdFXPaW8FTeEtHXVH/oK8U7KVgL/1D8vok34lYHchkNohWb3\nJQMDA4Mhx99HV3URCUebsP1W0LFvo/1APK2UekNEVopIHppb5q196rAHDJrCV0p9AHzQwXFjAsrA\nwGDY4Pd3FN4qdJRSLrTYSsHHnmqzf3efOuklw2GEP6pYtmzZUIvQI0aavGDIPBiMNHn7k4Cpx3Ho\nRgyDstK2t4QWtM/AwMAARKTPk7YiomrK1oZUNybplT73N9gYI3wDAwODIEKJ8jdSMRS+gYGBQRCB\nUbyQ2VD4BgYGBkEYCt/AwMDgHGH0qntD4RsYGBi0wuPrm1vmcOacTIBiYDASKC0t5Wtf+xqTJ0/m\nggsu4KqrriIvL4/i4mK
|
||
|
"text/plain": [
|
||
|
"<matplotlib.figure.Figure at 0x117d74cc0>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"import pandas as pd\n",
|
||
|
"cities = pd.read_csv('data/california_cities.csv')\n",
|
||
|
"\n",
|
||
|
"# Extract the data we're interested in\n",
|
||
|
"lat, lon = cities['latd'], cities['longd']\n",
|
||
|
"population, area = cities['population_total'], cities['area_total_km2']\n",
|
||
|
"\n",
|
||
|
"# Scatter the points, using size and color but no label\n",
|
||
|
"plt.scatter(lon, lat, label=None,\n",
|
||
|
" c=np.log10(population), cmap='viridis',\n",
|
||
|
" s=area, linewidth=0, alpha=0.5)\n",
|
||
|
"plt.axis(aspect='equal')\n",
|
||
|
"plt.xlabel('longitude')\n",
|
||
|
"plt.ylabel('latitude')\n",
|
||
|
"plt.colorbar(label='log$_{10}$(population)')\n",
|
||
|
"plt.clim(3, 7)\n",
|
||
|
"\n",
|
||
|
"# Here we create a legend:\n",
|
||
|
"# we'll plot empty lists with the desired size and label\n",
|
||
|
"for area in [100, 300, 500]:\n",
|
||
|
" plt.scatter([], [], c='k', alpha=0.3, s=area,\n",
|
||
|
" label=str(area) + ' km$^2$')\n",
|
||
|
"plt.legend(scatterpoints=1, frameon=False, labelspacing=1, title='City Area')\n",
|
||
|
"\n",
|
||
|
"plt.title('California Cities: Area and Population');"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"The legend will always reference some object that is on the plot, so if we'd like to display a particular shape we need to plot it.\n",
|
||
|
"In this case, the objects we want (gray circles) are not on the plot, so we fake them by plotting empty lists.\n",
|
||
|
"Notice too that the legend only lists plot elements that have a label specified.\n",
|
||
|
"\n",
|
||
|
"By plotting empty lists, we create labeled plot objects which are picked up by the legend, and now our legend tells us some useful information.\n",
|
||
|
"This strategy can be useful for creating more sophisticated visualizations.\n",
|
||
|
"\n",
|
||
|
"Finally, note that for geographic data like this, it would be clearer if we could show state boundaries or other map-specific elements.\n",
|
||
|
"For this, an excellent choice of tool is Matplotlib's Basemap addon toolkit, which we'll explore in [Geographic Data with Basemap](04.13-Geographic-Data-With-Basemap.ipynb)."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Multiple Legends\n",
|
||
|
"\n",
|
||
|
"Sometimes when designing a plot you'd like to add multiple legends to the same axes.\n",
|
||
|
"Unfortunately, Matplotlib does not make this easy: via the standard ``legend`` interface, it is only possible to create a single legend for the entire plot.\n",
|
||
|
"If you try to create a second legend using ``plt.legend()`` or ``ax.legend()``, it will simply override the first one.\n",
|
||
|
"We can work around this by creating a new legend artist from scratch, and then using the lower-level ``ax.add_artist()`` method to manually add the second artist to the plot:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 10,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXYAAAD7CAYAAAB+B7/XAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3Xl4TGf7B/DvY2vVW2smi0RirdiVUsQS1Pqj1rYo3VCi\nLV5LW1uFWqvaqqW1N5agKMJrXxKxU6QIEUuCRBIR2ZdZzv37I5JmT2bmzJzJ5P5c11xmOfOceybm\nPs95zrMIIgJjjDHrUUrpABhjjMmLEztjjFkZTuyMMWZlOLEzxpiV4cTOGGNWhhM7Y4xZmTLm2pEQ\ngvtVMsaYAYhI6LO9WWvsRMQ3IsyePVvxGCzlxt8Ffxf8XRR8MwQ3xTDGmJXhxM4YY1aGE7sC3N3d\nlQ7BYvB38S/+Lv7F34VxhKFtOHrvSAgy174YY8xaCCFAel48NbpXjBDiFQCnAZR7Wd4uIppjbLmM\nMcYMI0uNXQjxGhElCyFKAzgLYDwRXcqxDdfYGWNMT4bU2GVpYyei5Jd3X0F6rZ0zOGOMKUSWxC6E\nKCWEuAYgAsAxIrosR7mMMcb0J1eNXSKiNwE4AXhbCNFQjnIZY4zpT9YpB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TqVOnZI3l+vXrtG/fPtnKKwo/Pz/6v//7v8zHqamppNPp\nDC7v6tWrdPv27czH27Zto4cPHxoTYpEFBQXR119/TXXq1CEXFxcaP3487du3j549e2ZwmaGhobRl\nyxb69NNPqVq1avTWW2/R4sWL6enTpzJGXjC1Wk329vbk7u5OS5cupcuXL8v6O75y5Qrt3buXNBpN\nnq+/zJ365WN935BnIYA
|
||
|
"text/plain": [
|
||
|
"<matplotlib.figure.Figure at 0x117f1e3c8>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"fig, ax = plt.subplots()\n",
|
||
|
"\n",
|
||
|
"lines = []\n",
|
||
|
"styles = ['-', '--', '-.', ':']\n",
|
||
|
"x = np.linspace(0, 10, 1000)\n",
|
||
|
"\n",
|
||
|
"for i in range(4):\n",
|
||
|
" lines += ax.plot(x, np.sin(x - i * np.pi / 2),\n",
|
||
|
" styles[i], color='black')\n",
|
||
|
"ax.axis('equal')\n",
|
||
|
"\n",
|
||
|
"# specify the lines and labels of the first legend\n",
|
||
|
"ax.legend(lines[:2], ['line A', 'line B'],\n",
|
||
|
" loc='upper right', frameon=False)\n",
|
||
|
"\n",
|
||
|
"# Create the second legend and add the artist manually.\n",
|
||
|
"from matplotlib.legend import Legend\n",
|
||
|
"leg = Legend(ax, lines[2:], ['line C', 'line D'],\n",
|
||
|
" loc='lower right', frameon=False)\n",
|
||
|
"ax.add_artist(leg);"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"This is a peek into the low-level artist objects that comprise any Matplotlib plot.\n",
|
||
|
"If you examine the source code of ``ax.legend()`` (recall that you can do this with within the IPython notebook using ``ax.legend??``) you'll see that the function simply consists of some logic to create a suitable ``Legend`` artist, which is then saved in the ``legend_`` attribute and added to the figure when the plot is drawn."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"<!--NAVIGATION-->\n",
|
||
|
"< [Histograms, Binnings, and Density](04.05-Histograms-and-Binnings.ipynb) | [Contents](Index.ipynb) | [Customizing Colorbars](04.07-Customizing-Colorbars.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
|
||
|
}
|