From 91c616af205f0e34d5406e61f6c536025ce63911 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Geron?= Date: Wed, 17 Feb 2016 23:53:23 +0100 Subject: [PATCH] Added matplotlib sections about images and lines --- tools_matplotlib.ipynb | 180 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 170 insertions(+), 10 deletions(-) diff --git a/tools_matplotlib.ipynb b/tools_matplotlib.ipynb index f2f6582..b16b6b4 100644 --- a/tools_matplotlib.ipynb +++ b/tools_matplotlib.ipynb @@ -6,7 +6,7 @@ "source": [ "# Tools - matplotlib\n", "\n", - "This notebook demonstrates how to use the matplotlib library to plot beautiful graphs." + "*This notebook demonstrates how to use the matplotlib library to plot beautiful graphs.*" ] }, { @@ -902,7 +902,9 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Lines" + "## Lines\n", + "You can draw lines simply using the `plot` function, as we have done so far. However, it is often convenient to create a utility function that plots a (seemingly) infinite line across the graph, given a slope and an intercept. You can also use the `hlines` and `vlines` functions that plot horizontal and vertical line segments.\n", + "For example:" ] }, { @@ -914,11 +916,20 @@ "outputs": [], "source": [ "from numpy.random import randn\n", + "\n", + "def plot_line(axis, slope, intercept, **kargs):\n", + " xmin, xmax = axis.get_xlim()\n", + " plt.plot([xmin, xmax], [xmin*slope+intercept, xmax*slope+intercept], **kargs)\n", + "\n", "x = randn(1000)\n", - "y = 3*x + 5 + randn(1000)*2\n", + "y = 0.5*x + 5 + randn(1000)*2\n", "plt.axis([-2.5, 2.5, -5, 15])\n", - "plt.scatter(x, y)\n", - "plt.vlines(0, -numpy.inf, numpy.inf)\n", + "plt.scatter(x, y, alpha=0.2)\n", + "plt.plot(1, 0, \"ro\")\n", + "plt.vlines(1, -5, 0, color=\"red\")\n", + "plt.hlines(0, -2.5, 1, color=\"red\")\n", + "plot_line(axis=plt.gca(), slope=0.5, intercept=5, color=\"magenta\")\n", + "plt.grid(True)\n", "plt.show()" ] }, @@ -954,7 +965,7 @@ "execution_count": 36, "metadata": { "collapsed": false, - "scrolled": false + "scrolled": true }, "outputs": [], "source": [ @@ -976,6 +987,155 @@ "plt.show()" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Images\n", + "Reading, generating and plotting images in matplotlib is quite straightforward.\n", + "\n", + "To read an image, just import the `matplotlib.image` module, and call its `imread` function, passing it the file name (or file object). This returns the image data, as a NumPy array. Let's try this with the `my_square_function.png` image we saved earlier." + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "import matplotlib.image as mpimg\n", + "\n", + "img = mpimg.imread('my_square_function.png')\n", + "print img.shape, img.dtype" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We have loaded a 288x432 image. Each pixel is represented by a 4-element array: red, green, blue, and alpha levels, stored as 32-bit floats between 0 and 1. Now all we need to do is to call `imshow`:" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "plt.imshow(img)\n", + "plt.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Tadaaa! You may want to hide the axes when you are displaying an image:" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "plt.imshow(img)\n", + "plt.axis('off')\n", + "plt.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "It's just as easy to generate your own image:" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "img = numpy.arange(100*100).reshape(100, 100)\n", + "print img\n", + "plt.imshow(img)\n", + "plt.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "As we did not provide RGB levels, the `imshow` function automatically maps values to a color gradient. By default, the color gradient goes from blue (for low values) to red (for high values), but you can select another color map. For example:" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": { + "collapsed": false, + "scrolled": false + }, + "outputs": [], + "source": [ + "plt.imshow(img, cmap=\"hot\")\n", + "plt.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You can also generate an RGB image directly:" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [], + "source": [ + "img = numpy.empty((20,30,3))\n", + "img[:, :10] = [0, 0, 0.6]\n", + "img[:, 10:20] = [1, 1, 1]\n", + "img[:, 20:] = [0.6, 0, 0]\n", + "plt.imshow(img)\n", + "plt.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Since the `img` array is just quite small (20x30), when the `imshow` function displays it, it grows the image to the figure's size. By default it uses [bilinear interpolation](https://en.wikipedia.org/wiki/Bilinear_interpolation) to fill the added pixels. This is why the edges look blurry.\n", + "You can select another interpolation algorithm, such as copying the color of the nearest pixel:" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": { + "collapsed": false, + "scrolled": false + }, + "outputs": [], + "source": [ + "plt.imshow(img, interpolation=\"nearest\")\n", + "plt.show()" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -986,7 +1146,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 44, "metadata": { "collapsed": true }, @@ -1009,7 +1169,7 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 45, "metadata": { "collapsed": false }, @@ -1045,7 +1205,7 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 46, "metadata": { "collapsed": false }, @@ -1053,7 +1213,7 @@ "source": [ "Writer = animation.writers['ffmpeg']\n", "writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800)\n", - "line_ani.save('my_wiggly.mp4', writer=writer)" + "line_ani.save('my_wiggly_animation.mp4', writer=writer)" ] }, {