{ "metadata": {}, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Matplotlib: legend\n", "======================================================================\n", "\n", "Legends for overlaid lines and markers\n", "--------------------------------------\n", "\n", "If you have a lot of points to plot, you may want to set a marker only\n", "once every *n* points, e.g." ] }, { "cell_type": "code", "collapsed": false, "input": [ "plot(x, y, '-r')\n", "plot(x[::20], y[::20], 'ro')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then the automatic legend sees this as two different plots. One approach\n", "is to create an extra line object that is not plotted anywhere but used\n", "only for the legend:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from matplotlib.lines import Line2D\n", "line = Line2D(range(10), range(10), linestyle='-', marker='o')\n", "legend((line,), (label,))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another possibility is to modify the line object within the legend:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "line = plot(x, y, '-r')\n", "markers = plot(x[::20], y[::20], 'ro')\n", "lgd = legend([line], ['data'], numpoints=3)\n", "lgd.get_lines()[0].set_marker('o')\n", "draw()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Legend outside plot\n", "-------------------\n", "\n", "There is no nice easy way to add a legend outside (to the right of) your\n", "plot, but if you set the axes right in the first place, it works OK:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "figure()\n", "axes([0.1,0.1,0.71,0.8])\n", "plot([0,1],[0,1],label=\"line 1\")\n", "hold(1)\n", "plot([0,1],[1,0.5],label=\"line 2\")\n", "legend(loc=(1.03,0.2))\n", "show()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Removing a legend from a plot\n", "-----------------------------\n", "\n", "One can simply set the attribute of the axes to and redraw:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "ax = gca()\n", "ax.legend_ = None\n", "draw()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you find yourself doing this often use a function such as" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def remove_legend(ax=None):\n", " \"\"\"Remove legend for ax or the current axes.\"\"\"\n", "\n", " from pylab import gca, draw\n", " if ax is None:\n", " ax = gca()\n", " ax.legend_ = None\n", " draw()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(Source: [Re: How to delete legend with matplotlib OO from a\n", "graph?](http://osdir.com/ml/python.matplotlib.general/2005-07/msg00285.html))\n", "\n", "Changing the font size of legend text\n", "-------------------------------------\n", "\n", "Note that to set the default font size, one can change the *legend.size*\n", "property in the matplotlib rc parameters file.\n", "\n", "To change the font size for just one plot, use the\n", "matplotlib.font\\_manager.FontProperties argument, *prop*, for the legend\n", "creation." ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = range(10)\n", "y = range(10)\n", "handles = plot(x, y)\n", "legend(handles, [\"label1\"], prop={\"size\":12})" ], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }