{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Graphing Equations\n", "\n", "Lets revisit high school and think about those tiny screens a lot of us had to use.\n", "\n", "We may recreate those kinds of plots, of mathematical functions, quite straightforwardly.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "from matplotlib import pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lets define a domain from -5 to 5, of 100 points, and plot some XY curves that show some functions." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "domain = np.linspace(-5.0,5.0,100)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "y = np.power(domain, 2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline \n", "# \"magic\" command telling Jupyter NB to embed plots\n", "\n", "# always label and title your plot, at minimum\n", "plt.xlabel(\"X\")\n", "plt.ylabel(\"Y\")\n", "plt.title(\"Parabolic Curve\")\n", "p = plt.plot(domain, y)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x3 = np.power(domain, 3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.xlabel(\"X\")\n", "plt.ylabel(\"Y\")\n", "plt.title(\"X to the 3rd Power\")\n", "p = plt.plot(domain, x3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def poly(x):\n", " return (x - 3) * (x + 5) * (x - 1) * x" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "Poly = np.vectorize(poly)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "y = Poly(domain)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.xlabel(\"X\")\n", "plt.ylabel(\"Y\")\n", "plt.title(\"4th Degree Polynomial\")\n", "plt.grid()\n", "p = plt.plot(domain, y)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "y0 = np.sin(domain)\n", "y1 = np.cos(domain)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.xlabel(\"X\")\n", "plt.ylabel(\"Y\")\n", "plt.title(\"Sine & Cosine\")\n", "plt.grid()\n", "plt.plot(domain, y0, color = \"orange\", label=\"Sine\")\n", "plt.plot(domain, y1, color = \"green\", label=\"Cosine\")\n", "p = plt.legend()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now that we've plotted some data, lets organize the data into a data table, or \"data frame\" to be more precise. Pandas is all about the DataFrame object.\n", "\n", "Our domain is a 1-D ndarray lets remember. As such, we may turn it into a Series, which is the one dimensional equivalent in pandas. DataFrames are 2-dimensional and above." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "domain.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "col0 = pd.Series(domain)\n", "col1 = pd.Series(np.power(domain,2))\n", "col2 = pd.Series(x3)\n", "col3 = Poly(domain)\n", "\n", "datadict = {\"Input\":col0, \"Parabola\":col1, \"3rd Power\":col2, \"Polynomial\":col3}\n", "df = pd.DataFrame(datadict, columns = [\"Input\", \"Parabola\", \"3rd Power\", \"Polynomial\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Without the ```columns``` argument, there's no guarantee that ```datadict``` will gives us the left-to-right column order we desire." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we're starting to introduce how data may be selected by numeric indexes, yes, but also by labels." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.loc[:,\"3rd Power\"].head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.loc[:10,[\"Input\", \"3rd Power\"]] # rows 0-10 inclusive, two columns" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.4" } }, "nbformat": 4, "nbformat_minor": 2 }