{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Span Selector\n\n\nThe SpanSelector is a mouse widget to select a xmin/xmax range and plot the\ndetail view of the selected region in the lower axes\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\nimport matplotlib.pyplot as plt\nfrom matplotlib.widgets import SpanSelector\n\n# Fixing random state for reproducibility\nnp.random.seed(19680801)\n\nfig, (ax1, ax2) = plt.subplots(2, figsize=(8, 6))\nax1.set(facecolor='#FFFFCC')\n\nx = np.arange(0.0, 5.0, 0.01)\ny = np.sin(2*np.pi*x) + 0.5*np.random.randn(len(x))\n\nax1.plot(x, y, '-')\nax1.set_ylim(-2, 2)\nax1.set_title('Press left mouse button and drag to test')\n\nax2.set(facecolor='#FFFFCC')\nline2, = ax2.plot(x, y, '-')\n\n\ndef onselect(xmin, xmax):\n indmin, indmax = np.searchsorted(x, (xmin, xmax))\n indmax = min(len(x) - 1, indmax)\n\n thisx = x[indmin:indmax]\n thisy = y[indmin:indmax]\n line2.set_data(thisx, thisy)\n ax2.set_xlim(thisx[0], thisx[-1])\n ax2.set_ylim(thisy.min(), thisy.max())\n fig.canvas.draw()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
If the SpanSelector object is garbage collected you will lose the\n interactivity. You must keep a hard reference to it to prevent this.