{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": "## matplotlib and seaborn essentials" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": "import numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn\n%matplotlib inline" }, { "cell_type": "markdown", "metadata": {}, "source": "### Common plots with matplotlib" }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": "y = np.random.randn(1000)" }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": "plt.plot(y)" }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": "x = np.linspace(-10., 10., 1000)\ny = np.sin(3 * x) * np.exp(-.1 * x**2)" }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": "plt.plot(x, y)" }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": "x = np.linspace(-5., 5., 100)\ny = np.sin(3 * x) * np.exp(-.1 * x ** 2)" }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": "plt.plot(x, y, '--^',\n lw=3, color='#fdbb84',\n mfc='#2b8cbe', ms=8)" }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": "x = np.random.randn(100)\ny = x + np.random.randn(100)" }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": "plt.scatter(x, y)" }, { "cell_type": "markdown", "metadata": {}, "source": "### Customizing matplotlib figures" }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": "# Left panel.\nplt.subplot(1, 2, 1)\nx = np.linspace(-10., 10., 1000)\nplt.plot(x, np.sin(x), '-r', label='sinus')\nplt.plot(x, np.cos(x), ':g', lw=1, label='cosinus')\nplt.xticks([-10, 0, 10])\nplt.yticks([-1, 0, 1])\nplt.ylim(-2, 2)\nplt.xlabel(\"x axis\")\nplt.ylabel(\"y axis\")\nplt.title(\"Two plots\")\nplt.legend()\n\n# Right panel.\nplt.subplot(1, 2, 2, polar=True)\nx = np.linspace(0, 2 * np.pi, 1000)\nplt.plot(x, 1 + 2 * np.cos(6 * x))\nplt.yticks([])\nplt.xlim(-.1, 3.1)\nplt.ylim(-.1, 3.1)\nplt.xticks(np.linspace(0, 5 * np.pi / 3, 6))\nplt.title(\"A polar plot\")\nplt.grid(color='k', linewidth=1, linestyle=':')" }, { "cell_type": "markdown", "metadata": {}, "source": "### Interacting with matplotlib figures in the Notebook" }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": "from ipywidgets import interact" }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": "x = np.linspace(-5., 5., 1000)" }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": "@interact\ndef plot_sin(a=(1, 10)):\n plt.plot(x, np.sin(a*x))\n plt.ylim(-1, 1)" }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": "%matplotlib qt" }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": "lines = plt.plot([0, 1], [0, 1], 'b')" }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": "[]" }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": "lines" }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": "lines[0].set_color('r')\nplt.draw()" }, { "cell_type": "markdown", "metadata": {}, "source": "### High-level plotting with seaborn" }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": " sepal_length sepal_width petal_length petal_width species\n0 5.1 3.5 1.4 0.2 setosa\n1 4.9 3.0 1.4 0.2 setosa\n2 4.7 3.2 1.3 0.2 setosa" }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": "df = seaborn.load_dataset(\"iris\")\ndf.head(3)" }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": "seaborn.pairplot(df, hue=\"species\", size=2.5)" } ], "metadata": {}, "nbformat": 4, "nbformat_minor": 0 }