{ "metadata": { "name": "", "signature": "sha256:818a4a74de89f7d6535437da31fe5b3add5fd9d0e11a4539fd2cfbbd1e8039e4" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Simple legend example\n", "\n", "This is the simplest use case for the plugin. We plot a few lines, instantiate the InteractiveLegendPlugin, and connect it to the figure. Note that we pass the labels for each of the lines explicitly. At the moment the label information of matplotlib is not passed along to D3. The plugin creates a legend to the right of the axes starting at the top. Each of the rectangles is clickable. When a legend item is selected, the rectangle is filled. \n" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import mpld3\n", "from mpld3 import plugins\n", "from mpld3.utils import get_id\n", "import numpy as np\n", "import collections\n", "import matplotlib.pyplot as plt\n", "\n", "N_paths = 5\n", "N_steps = 100\n", "\n", "x = np.linspace(0, 10, 100)\n", "y = 0.1 * (np.random.random((N_paths, N_steps)) - 0.5)\n", "y = y.cumsum(1)\n", "\n", "fig, ax = plt.subplots()\n", "labels = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n", "line_collections = ax.plot(x, y.T, lw=4, alpha=0.2)\n", "interactive_legend = plugins.InteractiveLegendPlugin(line_collections, labels)\n", "plugins.connect(fig, interactive_legend)\n", "\n", "mpld3.display()" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n", "\n", "\n", "
\n", "" ], "metadata": {}, "output_type": "pyout", "prompt_number": 1, "text": [ "