{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Matplotlib: A 2D plotting Library" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Plot-simple" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:25:21.890999Z", "start_time": "2018-10-16T19:25:21.757055Z" } }, "outputs": [], "source": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "\n", "\n", "t = np.arange(0.0, 2.0, 0.05)\n", "s = 1 + np.sin(2 * np.pi * t)\n", "plt.plot(t, s)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:26:04.576733Z", "start_time": "2018-10-16T19:26:04.390600Z" } }, "outputs": [], "source": [ "plt.plot(t, s, 'r*--', lw=2)\n", "plt.show()\n", "plt.scatter(t, s)\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:26:47.811847Z", "start_time": "2018-10-16T19:26:47.661009Z" } }, "outputs": [], "source": [ "plt.scatter(t, s)\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:26:51.534370Z", "start_time": "2018-10-16T19:26:51.358189Z" } }, "outputs": [], "source": [ "plt.fill(t, s)\n", "plt.savefig(\"test2.pdf\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![test](test2.png)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:27:47.633687Z", "start_time": "2018-10-16T19:27:47.566562Z" } }, "outputs": [], "source": [ "plt.figure?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:28:23.066123Z", "start_time": "2018-10-16T19:28:22.905909Z" } }, "outputs": [], "source": [ "%matplotlib inline\n", "\n", "plt.figure(1, (15, 5))\n", "plt.scatter(t, s, alpha=0.7)\n", "plt.fill(t, s, alpha=0.6)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:28:31.862435Z", "start_time": "2018-10-16T19:28:31.700829Z" } }, "outputs": [], "source": [ "plt.figure(1, (15, 5))\n", "plt.fill(t, s)\n", "plt.scatter(t, s)\n", "\n", "plt.xlabel('time (s)')\n", "plt.ylabel('voltage (mV)')\n", "plt.title('About as simple as it gets, folks')\n", "plt.grid(True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Subplot" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:28:46.744567Z", "start_time": "2018-10-16T19:28:46.581239Z" } }, "outputs": [], "source": [ "x1 = np.linspace(0.0, 5.0)\n", "x2 = np.linspace(0.0, 2.0)\n", "\n", "y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)\n", "y2 = np.cos(2 * np.pi * x2)\n", "\n", "plt.figure(1, (15, 5))\n", "plt.plot(x1, y1, 'o-', label='y1')\n", "plt.plot(x2, y2, '.-', label='y2')\n", "plt.legend(loc=0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:29:09.675473Z", "start_time": "2018-10-16T19:29:09.401063Z" } }, "outputs": [], "source": [ "plt.figure(1, (15, 5))\n", "plt.plot(x1, y1, 'o-')\n", "plt.title('A tale of 2 subplots')\n", "plt.ylabel('Damped oscillation')\n", "\n", "plt.figure(2, (15, 5))\n", "plt.plot(x2, y2, '.-')\n", "plt.xlabel('time (s)')\n", "plt.ylabel('Undamped')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:29:59.304748Z", "start_time": "2018-10-16T19:29:59.048565Z" } }, "outputs": [], "source": [ "plt.figure(1, (15, 10))\n", "\n", "plt.subplot(2, 1, 1)\n", "plt.plot(x1, y1, 'o-')\n", "plt.title('A tale of 2 subplots')\n", "plt.ylabel('Damped oscillation')\n", "plt.xlim([0, 5])\n", "\n", "plt.subplot(2, 1, 2)\n", "plt.plot(x2, y2, '.-')\n", "plt.xlabel('time (s)')\n", "plt.ylabel('Undamped')\n", "plt.xlim([0, 2])\n", "plt.ylim([-1, 1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Doble escala" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:30:21.977403Z", "start_time": "2018-10-16T19:30:21.969468Z" } }, "outputs": [], "source": [ "t = np.arange(0.01, 10.0, 0.01)\n", "s1 = np.exp(t)\n", "s2 = np.sin(2 * np.pi * t)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:30:22.766203Z", "start_time": "2018-10-16T19:30:22.634951Z" } }, "outputs": [], "source": [ "fig = plt.figure(1,(15,5))\n", "\n", "plt.plot(t, s1, 'b-', label='exp')\n", "plt.plot(t, s2, 'r.', label='sin')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:31:52.700717Z", "start_time": "2018-10-16T19:31:52.518478Z" } }, "outputs": [], "source": [ "fig = plt.figure(1,(15,5))\n", "ax1 = plt.subplot(111)\n", "ax2 = ax1.twinx()\n", "\n", "ax1.plot(t, s1, 'b-')\n", "ax1.set_xlabel('time (s)')\n", "ax1.set_ylabel('exp', color='b')\n", "ax1.tick_params('y', colors='b')\n", "\n", "\n", "ax2.plot(t, s2, 'r.')\n", "ax2.set_ylabel('sin', color='r')\n", "ax2.tick_params('y', colors='r')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Styles" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:32:46.220512Z", "start_time": "2018-10-16T19:32:46.217071Z" } }, "outputs": [], "source": [ "def simple_plot():\n", " plt.figure()\n", "\n", " plt.subplot(2, 1, 1)\n", " plt.plot(x1, y1, 'o-', label='sin * exp')\n", " plt.title('A tale of 2 subplots')\n", " plt.ylabel('Damped oscillation')\n", " plt.legend()\n", " \n", " plt.subplot(2, 1, 2)\n", " plt.plot(x2, y2, '.-', label='sin')\n", " plt.xlabel('time (s)')\n", " plt.ylabel('Undamped')\n", " plt.legend()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:32:47.072443Z", "start_time": "2018-10-16T19:32:46.860089Z" } }, "outputs": [], "source": [ "simple_plot()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:33:44.692956Z", "start_time": "2018-10-16T19:33:44.448750Z" } }, "outputs": [], "source": [ "plt.style.use('classic')\n", "simple_plot()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:34:01.618387Z", "start_time": "2018-10-16T19:34:01.369074Z" } }, "outputs": [], "source": [ "plt.style.use('default')\n", "simple_plot()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:34:16.254612Z", "start_time": "2018-10-16T19:34:16.243341Z" }, "scrolled": true }, "outputs": [], "source": [ "plt.style.available" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:34:42.028223Z", "start_time": "2018-10-16T19:34:41.786521Z" } }, "outputs": [], "source": [ "plt.style.use('ggplot')\n", "simple_plot()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:36:09.986140Z", "start_time": "2018-10-16T19:36:09.965261Z" } }, "outputs": [], "source": [ "plt.rcParams" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:36:47.201624Z", "start_time": "2018-10-16T19:36:46.836629Z" } }, "outputs": [], "source": [ "plt.rcParams['figure.figsize'] = (15, 10)\n", "\n", "simple_plot()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:37:43.609476Z", "start_time": "2018-10-16T19:37:43.253309Z" } }, "outputs": [], "source": [ "plt.rcParams['axes.titlesize'] = 26\n", "plt.rcParams['axes.labelsize'] = 26\n", "plt.rcParams['xtick.labelsize'] = 20\n", "plt.rcParams['ytick.labelsize'] = 20\n", "plt.rcParams['legend.fontsize'] = 20\n", "plt.rcParams['text.color'] = 'black'\n", "\n", "simple_plot()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Histograma" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:38:43.090192Z", "start_time": "2018-10-16T19:38:42.569250Z" } }, "outputs": [], "source": [ "# example data\n", "mu = 100 # mean of distribution\n", "sigma = 15 # standard deviation of distribution\n", "x = mu + sigma * np.random.randn(1000)\n", "\n", "plt.plot(x)\n", "plt.text(300, 150, r'$\\mu=100,\\ \\sigma=15$')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:39:48.663720Z", "start_time": "2018-10-16T19:39:47.127855Z" } }, "outputs": [], "source": [ "num_bins = 1000\n", "f, bins, _ = plt.hist(x, num_bins, normed=1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:40:22.343141Z", "start_time": "2018-10-16T19:40:20.874613Z" } }, "outputs": [], "source": [ "import matplotlib.mlab as mlab\n", "\n", "y = mlab.normpdf(bins, mu, sigma)\n", "\n", "fig, ax = plt.subplots()\n", "\n", "ax.hist(x, num_bins, normed=1)\n", "ax.plot(bins, y, '--')\n", "ax.set_xlabel('Smarts')\n", "ax.set_ylabel('Probability density')\n", "ax.set_title(r'Histogram of IQ: $\\mu=100$, $\\sigma=15$')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Trazos" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:42:12.402507Z", "start_time": "2018-10-16T19:42:12.164860Z" } }, "outputs": [], "source": [ "import matplotlib.path as mpath\n", "import matplotlib.patches as mpatches\n", "\n", "\n", "fig, ax = plt.subplots()\n", "\n", "Path = mpath.Path\n", "path_data = [\n", " (Path.MOVETO, (1.58, -2.57)),\n", " (Path.CURVE4, (0.35, -1.1)),\n", " (Path.CURVE4, (-1.75, 2.0)),\n", " (Path.CURVE4, (0.375, 2.0)),\n", " (Path.LINETO, (0.85, 1.15)),\n", " (Path.CURVE4, (2.2, 3.2)),\n", " (Path.CURVE4, (3, 0.05)),\n", " (Path.CURVE4, (2.0, -0.5)),\n", " (Path.CLOSEPOLY, (1.58, -2.57)),\n", " ]\n", "codes, verts = zip(*path_data)\n", "\n", "path = mpath.Path(verts, codes)\n", "patch = mpatches.PathPatch(path, facecolor='r', alpha=0.2)\n", "ax.add_patch(patch)\n", "\n", "# plot control points and connecting lines\n", "x, y = path.vertices[:,0], path.vertices[:,1]\n", "ax.plot(x, y, 'go-')\n", "\n", "ax.grid()\n", "ax.axis('equal')\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Campos vectoriales 2D" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:45:10.513259Z", "start_time": "2018-10-16T19:45:10.499762Z" }, "scrolled": true }, "outputs": [], "source": [ "%matplotlib inline\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from matplotlib import cm\n", "\n", "\n", "Y, X = np.mgrid[-3:3:100j, -3:3:100j]\n", "\n", "U = -1 - X**2 + Y\n", "V = 1 + X - Y**2\n", "speed = np.sqrt(U * U + V * V)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:46:30.124538Z", "start_time": "2018-10-16T19:46:29.447427Z" } }, "outputs": [], "source": [ "plt.streamplot(X, Y, U, V, color=V, linewidth=2, cmap=cm.RdBu)\n", "plt.colorbar()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:46:43.609199Z", "start_time": "2018-10-16T19:46:42.988124Z" } }, "outputs": [], "source": [ "plt.streamplot(X, Y, U, V, density=0.9)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:46:47.188591Z", "start_time": "2018-10-16T19:46:46.621034Z" } }, "outputs": [], "source": [ "plt.streamplot(X, Y, U, V, density=0.6)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:47:04.873615Z", "start_time": "2018-10-16T19:47:04.403023Z" } }, "outputs": [], "source": [ "lw = 5 * speed / speed.max()\n", "plt.streamplot(X, Y, U, V, density=0.6, color='k', linewidth=lw)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Elipses" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:48:37.144433Z", "start_time": "2018-10-16T19:48:36.912989Z" } }, "outputs": [], "source": [ "import numpy.random as rnd\n", "from matplotlib.patches import Ellipse\n", "\n", "NUM = 500\n", "\n", "ells = [Ellipse(xy=rnd.rand(2) * 10, width=rnd.rand(), height=rnd.rand(), angle=rnd.rand() * 360) \n", " for i in range(NUM)]\n", "\n", "fig = plt.figure(1, (20, 15))\n", "ax = fig.add_subplot(111)\n", "for e in ells:\n", " ax.add_artist(e)\n", " e.set_clip_box(ax.bbox)\n", " e.set_alpha(rnd.rand())\n", " e.set_facecolor(rnd.rand(3))\n", "\n", "ax.set_xlim(0, 10)\n", "ax.set_ylim(0, 10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Grafico barras" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:49:18.317524Z", "start_time": "2018-10-16T19:49:18.069532Z" } }, "outputs": [], "source": [ "n_groups = 5\n", "\n", "means_men = (20, 35, 30, 35, 27)\n", "std_men = (2, 3, 4, 1, 2)\n", "\n", "means_women = (25, 32, 34, 20, 25)\n", "std_women = (3, 5, 2, 3, 3)\n", "\n", "fig, ax = plt.subplots(figsize=(20,10))\n", "\n", "index = np.arange(n_groups)\n", "bar_width = 0.35\n", "\n", "opacity = 0.4\n", "error_config = {'ecolor': '0.3'}\n", "\n", "rects1 = plt.bar(index, means_men, bar_width,\n", " alpha=opacity,\n", " color='b',\n", " yerr=std_men,\n", " error_kw=error_config,\n", " label='Men')\n", "\n", "rects2 = plt.bar(index + bar_width, means_women, bar_width,\n", " alpha=opacity,\n", " color='r',\n", " yerr=std_women,\n", " error_kw=error_config,\n", " label='Women')\n", "\n", "plt.xlabel('Group')\n", "plt.ylabel('Scores')\n", "plt.title('Scores by group and gender')\n", "plt.xticks(index + bar_width / 2, ('A', 'B', 'C', 'D', 'E'))\n", "plt.legend()\n", "\n", "plt.tight_layout()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Grafico con tabla" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:50:53.081197Z", "start_time": "2018-10-16T19:50:52.672806Z" }, "scrolled": false }, "outputs": [], "source": [ "data = [[ 66386, 174296, 75131, 577908, 32015],\n", " [ 58230, 381139, 78045, 99308, 160454],\n", " [ 89135, 80552, 152558, 497981, 603535],\n", " [ 78415, 81858, 150656, 193263, 69638],\n", " [ 139361, 331509, 343164, 781380, 52269]]\n", "\n", "columns = ('Freeze', 'Wind', 'Flood', 'Quake', 'Hail')\n", "rows = ['%d year' % x for x in (100, 50, 20, 10, 5)]\n", "\n", "values = np.arange(0, 2500, 500)\n", "value_increment = 1000\n", "\n", "# Get some pastel shades for the colors\n", "colors = plt.cm.RdBu_r(np.linspace(0, 1.0, len(rows)))\n", "n_rows = len(data)\n", "\n", "index = np.arange(len(columns)) + 0.3\n", "bar_width = 0.4\n", "\n", "# Initialize the vertical-offset for the stacked bar chart.\n", "y_offset = np.array([0.0] * len(columns))\n", "\n", "# Plot bars and create text labels for the table\n", "plt.figure(1, (20, 10))\n", "cell_text = []\n", "for row in range(n_rows):\n", " plt.bar(index, data[row], bar_width, bottom=y_offset, color=colors[row])\n", " y_offset = y_offset + data[row]\n", " cell_text.append(['%1.1f' % (x/1000.0) for x in y_offset])\n", "# Reverse colors and text labels to display the last value at the top.\n", "colors = colors[::-1]\n", "cell_text.reverse()\n", "\n", "# Add a table at the bottom of the axes\n", "the_table = plt.table(cellText=cell_text,\n", " rowLabels=rows,\n", " rowColours=colors,\n", " colLabels=columns,\n", " loc='bottom')\n", "the_table.auto_set_font_size(False)\n", "the_table.set_fontsize(15)\n", "the_table.scale(1, 4)\n", "\n", "# Adjust layout to make room for the table:\n", "plt.subplots_adjust(left=0.2, bottom=0.2)\n", "\n", "plt.ylabel(\"Loss in ${0}'s\".format(value_increment))\n", "plt.yticks(values * value_increment, ['%d' % val for val in values])\n", "plt.xticks([])\n", "plt.title('Loss by Disaster')\n", "\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Pie Plot " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:51:36.774141Z", "start_time": "2018-10-16T19:51:36.661244Z" } }, "outputs": [], "source": [ "plt.figure(1, (10, 10))\n", "_ = plt.pie(np.random.random(4), \n", " explode=(0, 0.1, 0, 0.2), \n", " autopct='%1.1f%%', \n", " labels=('Frogs', 'Hogs', 'Dogs', 'Logs'), \n", " colors=['yellowgreen', 'gold', 'lightskyblue', 'lightcoral'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Dates as Index" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:57:51.222731Z", "start_time": "2018-10-16T19:57:51.079007Z" } }, "outputs": [], "source": [ "import numpy as np\n", "import datetime\n", "import matplotlib.pyplot as plt\n", "import matplotlib.dates as mdates\n", "import matplotlib.cbook as cbook\n", "\n", "#datafile = cbook.get_sample_data('goog.npy')\n", "r = np.load('goog.npy', encoding='bytes').view(np.recarray)\n", "r" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:57:52.009009Z", "start_time": "2018-10-16T19:57:51.798330Z" } }, "outputs": [], "source": [ "fig, ax = plt.subplots(figsize=(20, 10))\n", "ax.plot(r.date, r.adj_close)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:59:19.850902Z", "start_time": "2018-10-16T19:59:19.612132Z" } }, "outputs": [], "source": [ "fig, ax = plt.subplots(figsize=(20, 10))\n", "ax.plot(r.date, r.adj_close)\n", "\n", "# format the ticks\n", "\n", "years = mdates.YearLocator() # every year\n", "months = mdates.MonthLocator() # every month\n", "yearsFmt = mdates.DateFormatter('%y-%m')\n", "\n", "ax.xaxis.set_major_locator(years)\n", "ax.xaxis.set_minor_locator(months)\n", "ax.xaxis.set_major_formatter(yearsFmt)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# xkcd" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T19:59:42.379891Z", "start_time": "2018-10-16T19:59:41.961328Z" } }, "outputs": [], "source": [ "with plt.xkcd():\n", " # Based on \"Stove Ownership\" from XKCD by Randall Monroe\n", " # http://xkcd.com/418/\n", "\n", " fig = plt.figure()\n", " ax = fig.add_axes((0.1, 0.2, 0.8, 0.7))\n", " ax.spines['right'].set_color('none')\n", " ax.spines['top'].set_color('none')\n", " plt.xticks([])\n", " plt.yticks([])\n", " ax.set_ylim([-30, 10])\n", "\n", " data = np.ones(100)\n", " data[70:] -= np.arange(30)\n", "\n", " plt.annotate(\n", " 'THE DAY I REALIZED\\nI COULD COOK BACON\\nWHENEVER I WANTED',\n", " xy=(70, 1), arrowprops=dict(arrowstyle='->'), xytext=(15, -10))\n", "\n", " plt.plot(data)\n", "\n", " plt.xlabel('time')\n", " plt.ylabel('my overall health')\n", " fig.text(\n", " 0.5, 0.05,\n", " '\"Stove Ownership\" from xkcd by Randall Monroe',\n", " ha='center')\n", "\n", " # Based on \"The Data So Far\" from XKCD by Randall Monroe\n", " # http://xkcd.com/373/\n", "\n", " fig = plt.figure()\n", " ax = fig.add_axes((0.1, 0.2, 0.8, 0.7))\n", " ax.bar([0, 1], [0, 100], 0.25)\n", " ax.spines['right'].set_color('none')\n", " ax.spines['top'].set_color('none')\n", " ax.xaxis.set_ticks_position('bottom')\n", " ax.set_xticks([0, 1])\n", " ax.set_xlim([-0.5, 1.5])\n", " ax.set_ylim([0, 110])\n", " ax.set_xticklabels(['CONFIRMED BY\\nEXPERIMENT', 'REFUTED BY\\nEXPERIMENT'])\n", " plt.yticks([])\n", "\n", " plt.title(\"CLAIMS OF SUPERNATURAL POWERS\")\n", "\n", " fig.text(\n", " 0.5, 0.05,\n", " '\"The Data So Far\" from xkcd by Randall Monroe',\n", " ha='center')\n", "\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Superficies" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T20:00:51.937833Z", "start_time": "2018-10-16T20:00:51.925547Z" } }, "outputs": [], "source": [ "# Make data\n", "import numpy as np\n", "x = np.arange(-10, 10, 0.5)\n", "y = np.arange(-10, 10, 0.5)\n", "\n", "X, Y = np.meshgrid(x, y)\n", "\n", "R = np.sqrt(X**2 + Y**2)\n", "Z = np.sin(R)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T20:00:52.638818Z", "start_time": "2018-10-16T20:00:52.622110Z" } }, "outputs": [], "source": [ "X, Y" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T20:02:57.347103Z", "start_time": "2018-10-16T20:02:57.236189Z" } }, "outputs": [], "source": [ "#%matplotlib inline\n", "%matplotlib notebook\n", "from mpl_toolkits.mplot3d import Axes3D\n", "import matplotlib.pyplot as plt\n", "from matplotlib import cm\n", "from matplotlib.ticker import LinearLocator, FormatStrFormatter\n", "\n", "\n", "fig = plt.figure(1, (15, 10))\n", "ax = fig.gca(projection='3d')\n", "\n", "cmap = cm.RdBu #coolwarm, RdBu , RdBu_r\n", "\n", "# Plot the surface.\n", "surf = ax.plot_surface(X, Y, Z, alpha=0.4, cmap=cmap, linewidth=0, antialiased=False) \n", "# Plot contour\n", "#ax.contour(X, Y, Z, zdir='z', cmap=cmap)\n", "ax.contour(X, Y, Z, zdir='z', offset=np.min(Z), cmap=cmap)\n", "ax.contour(X, Y, Z, zdir='x', offset=np.min(X), cmap=cmap)\n", "ax.contour(X, Y, Z, zdir='y', offset=np.max(Y), cmap=cmap)\n", "\n", "# Add a color bar which maps values to colors.\n", "fig.colorbar(surf, shrink=0.5, aspect=5)\n", "\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Ejemplo: Regresión Lineal" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T20:03:43.616557Z", "start_time": "2018-10-16T20:03:43.610077Z" } }, "outputs": [], "source": [ "%matplotlib inline\n", "# los plots se muestran en este mismo notebook\n", "\n", "import numpy as np\n", "import scipy\n", "import matplotlib\n", "import matplotlib.pyplot as plt\n", "import matplotlib.cm as cm\n", "import matplotlib.mlab as mlab\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T20:03:55.110652Z", "start_time": "2018-10-16T20:03:55.107660Z" } }, "outputs": [], "source": [ "# data acquisition\n", "alumnos = np.array([28918, 29387, 30321, 31281, 35619, 37286, 38317, 38938])\n", "years = np.linspace(2006, 2013, 8)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T20:05:59.385086Z", "start_time": "2018-10-16T20:05:59.216838Z" } }, "outputs": [], "source": [ "# plot\n", "fig = plt.figure(1, figsize=(20, 6))\n", "plt.rc('xtick', labelsize=20)\n", "plt.rc('ytick', labelsize=20)\n", "plt.gca().get_xaxis().get_major_formatter().set_useOffset(False)\n", "plt.plot(years, alumnos, 'ro', c='#00BFFF', ms=10, label='Datos')\n", "plt.axis([2005, 2014, 26000, 42000])\n", "#plt.title('Cantidad de alumnos Universidad de Chile 2006-2013',fontsize=25)\n", "\n", "\n", "# query/prediction\n", "years_reg = np.linspace(2005, 2014, 10)\n", "\n", "# solution\n", "invX = np.linalg.inv(np.dot(np.vstack((years, np.ones(8))),\n", " np.vstack((years, np.ones(8))).T))\n", "weights = np.dot(invX, np.dot(\n", " np.vstack((years, np.ones(8)))[:, np.newaxis], alumnos))\n", "\n", "\n", "# plot\n", "plt.plot(years_reg, (np.dot(weights.T, np.vstack((years_reg, np.ones(10))))\n", " ).flatten(), 'r', lw=5, label='Regresion lineal')\n", "plt.xticks(years_reg)\n", "plt.legend(loc=0, fontsize=20)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T20:06:03.298015Z", "start_time": "2018-10-16T20:06:03.291776Z" } }, "outputs": [], "source": [ "n = 250 # number of observations\n", "# input data are drawn from a zero-mean unit variance normal pdf\n", "x = np.random.uniform(-5, 5, n)\n", "\n", "# cambiar los siguientes tres parámetros para generar distintos ejemplos\n", "a = 2 # slope parameter\n", "b = -2 # offset parameter\n", "sigma = 0.5\n", "# observations follow a linear Gaussian model\n", "y = a * x + b + np.random.normal(0, sigma, n)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T20:06:05.976718Z", "start_time": "2018-10-16T20:06:05.794869Z" } }, "outputs": [], "source": [ "fig = plt.figure(2, figsize=(20, 6))\n", "\n", "plt.plot(x, a * x + b, 'b', lw=2, label='Media')\n", "plt.plot(x, a * x + b + 2 * sigma, 'b', lw=1, label='Intervalo confianza 95% ')\n", "plt.plot(x, a * x + b - 2 * sigma, 'b', lw=1)\n", "plt.plot(x, y, 'ro', ms=5, label='Datos')\n", "plt.legend(loc=0, fontsize=20)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-16T20:06:28.882221Z", "start_time": "2018-10-16T20:06:20.305640Z" } }, "outputs": [], "source": [ "matplotlib.rcParams['xtick.direction'] = 'out'\n", "matplotlib.rcParams['ytick.direction'] = 'out'\n", "plt.rc('xtick', labelsize=13)\n", "plt.rc('ytick', labelsize=13)\n", "\n", "# prior distribution on w is Gaussian with mean and variance:\n", "V0 = np.array([[1, 0], [0, 1]])\n", "w0 = np.array([0, 0])\n", "\n", "\n", "f, axarr = plt.subplots(1, 5, figsize=(20, 4))\n", "plt.gca().get_xaxis().get_major_formatter().set_useOffset(False)\n", "\n", "# datos\n", "axarr[0].set_title('Datos')\n", "\n", "# distribución conjunta\n", "axarr[1].set_title('Prior conjunta')\n", "axarr[1].plot(a, b, 'r+', ms=20)\n", "delta = 0.025\n", "w1_plot = np.arange(-3.0, 3.0, delta)\n", "w2_plot = np.arange(-3.0, 3.0, delta)\n", "W1, W2 = np.meshgrid(w1_plot, w2_plot)\n", "Z1 = mlab.bivariate_normal(\n", " W1, W2, V0[0, 0] * 0.5, V0[1, 1] * 0.5, w0[0], w0[1], V0[0, 1])\n", "CS = axarr[1].contour(W1, W2, Z1)\n", "plt.clabel(CS, inline=1, fontsize=10)\n", "\n", "# distribución marginal w1\n", "axarr[2].set_title('Prior w1')\n", "Z2 = mlab.normpdf(w1_plot, w0[0], V0[0, 0])\n", "CS = axarr[2].plot(w1_plot, Z2)\n", "\n", "# distribución marginal w2\n", "axarr[3].set_title('Prior w2')\n", "Z3 = mlab.normpdf(w1_plot, w0[1], V0[1, 1])\n", "CS = axarr[3].plot(w1_plot, Z3)\n", "\n", "# muestras del modelo\n", "axarr[4].set_title('Muestras del modelo')\n", "for i in range(0, 10):\n", " w = np.random.multivariate_normal(w0, V0)\n", " CS = axarr[4].plot(x, w[0] * x + w[1, np.newaxis], 'r', lw=2)\n", "\n", "\n", "for i in range(0, 25):\n", " # compute statistics w y V\n", " invV0 = np.linalg.inv(V0)\n", " x_tilde = np.vstack((x[:i], np.ones(i)))\n", " V = sigma**2 * np.linalg.inv(sigma**2 * invV0 + np.dot(x_tilde, x_tilde.T))\n", " w = np.dot(np.dot(V, invV0), w0.T) + \\\n", " np.dot(np.dot(V, x_tilde), y[:i]) / sigma**2\n", "\n", " f, axarr = plt.subplots(1, 5, figsize=(20, 4))\n", " plt.gca().get_xaxis().get_major_formatter().set_useOffset(False)\n", "\n", " # datos\n", " axarr[0].set_title('Datos')\n", " axarr[0].scatter(x[:i], y[:i])\n", "\n", " # distribución conjunta\n", " axarr[1].set_title('Posterior conjunta')\n", " axarr[1].plot(a, b, 'r+', mew=5, ms=20)\n", "\n", " Z1 = mlab.bivariate_normal(\n", " W1, W2, V[0, 0]**0.5, V[1, 1]**0.5, w[0], w[1], V[0, 1])\n", " CS = axarr[1].contour(W1, W2, Z1)\n", " plt.clabel(CS, inline=1, fontsize=10)\n", "\n", " # distribución marginal w1\n", " axarr[2].set_title('Posterior w1')\n", " Z2 = mlab.normpdf(w1_plot, w[0], V[0, 0])\n", " CS = axarr[2].plot(w1_plot, Z2)\n", "\n", " # distribución marginal w2\n", " axarr[3].set_title('Posterior w2')\n", " Z3 = mlab.normpdf(w1_plot, w[1], V[1, 1])\n", " CS = axarr[3].plot(w1_plot, Z3)\n", "\n", " # muestras del modelo\n", " axarr[4].set_title('Muestras del modelo')\n", " for j in range(0, 10):\n", " w_sample = np.random.multivariate_normal(w, V)\n", " CS = axarr[4].plot(x, w_sample[0] * x +\n", " w_sample[1, np.newaxis], 'r', lw=2)\n", " axarr[4].plot(x, a * x + b)\n", " plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python [default]", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }