{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Third Party Libraries With Rich Output" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A number of third party libraries defined their own custom display logic. This gives their objects rich output by default when used in the Notebook." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from IPython.display import display" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Pandas" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[Pandas](http://pandas.pydata.org/) is a data analysis library for Python. Its `DataFrame` objects have an HTML table representation in the Notebook." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import pandas" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here is a small amount of stock data for APPL:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing data.csv\n" ] } ], "source": [ "%%writefile data.csv\n", "Date,Open,High,Low,Close,Volume,Adj Close\n", "2012-06-01,569.16,590.00,548.50,584.00,14077000,581.50\n", "2012-05-01,584.90,596.76,522.18,577.73,18827900,575.26\n", "2012-04-02,601.83,644.00,555.00,583.98,28759100,581.48\n", "2012-03-01,548.17,621.45,516.22,599.55,26486000,596.99\n", "2012-02-01,458.41,547.61,453.98,542.44,22001000,540.12\n", "2012-01-03,409.40,458.24,409.00,456.48,12949100,454.53" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Read this as into a `DataFrame`:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [], "source": [ "df = pandas.read_csv('data.csv')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And view the HTML representation:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
DateOpenHighLowCloseVolumeAdj Close
0 2012-06-01 569.16 590.00 548.50 584.00 14077000 581.50
1 2012-05-01 584.90 596.76 522.18 577.73 18827900 575.26
2 2012-04-02 601.83 644.00 555.00 583.98 28759100 581.48
3 2012-03-01 548.17 621.45 516.22 599.55 26486000 596.99
4 2012-02-01 458.41 547.61 453.98 542.44 22001000 540.12
5 2012-01-03 409.40 458.24 409.00 456.48 12949100 454.53
\n", "
" ], "text/plain": [ " Date Open High Low Close Volume Adj Close\n", "0 2012-06-01 569.16 590.00 548.50 584.00 14077000 581.50\n", "1 2012-05-01 584.90 596.76 522.18 577.73 18827900 575.26\n", "2 2012-04-02 601.83 644.00 555.00 583.98 28759100 581.48\n", "3 2012-03-01 548.17 621.45 516.22 599.55 26486000 596.99\n", "4 2012-02-01 458.41 547.61 453.98 542.44 22001000 540.12\n", "5 2012-01-03 409.40 458.24 409.00 456.48 12949100 454.53" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## SymPy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[SymPy](http://sympy.org/) is a symbolic computing library for Python. Its equation objects have LaTeX representations that are rendered in the Notebook." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from sympy.interactive.printing import init_printing\n", "init_printing(use_latex='mathjax')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from __future__ import division\n", "import sympy as sym\n", "from sympy import *\n", "x, y, z = symbols(\"x y z\")\n", "k, m, n = symbols(\"k m n\", integer=True)\n", "f, g, h = map(Function, 'fgh')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$\\frac{3 \\pi}{2} + \\frac{e^{i x}}{x^{2} + y}$$" ], "text/plain": [ " ⅈ⋅x \n", "3⋅π ℯ \n", "─── + ──────\n", " 2 2 \n", " x + y" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Rational(3,2)*pi + exp(I*x) / (x**2 + y)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$\\frac{1}{x} \\left(x \\sin{\\left (x \\right )} - 1\\right) + \\frac{1}{x}$$" ], "text/plain": [ "x⋅sin(x) - 1 1\n", "──────────── + ─\n", " x x" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 1/x + (x*sin(x) - 1)/x\n", "a" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$1 + \\frac{x^{2}}{2} + \\frac{5 x^{4}}{24} + \\mathcal{O}\\left(x^{6}\\right)$$" ], "text/plain": [ " 2 4 \n", " x 5⋅x ⎛ 6⎞\n", "1 + ── + ──── + O⎝x ⎠\n", " 2 24 " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(1/cos(x)).series(x, 0, 6)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Vincent" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[Vincent](https://vincent.readthedocs.io/en/latest/) is a visualization library that uses the [Vega](http://trifacta.github.io/vega/) visualization grammar to build [d3.js](http://d3js.org/) based visualizations in the Notebook and on http://nbviewer.ipython.org. `Visualization` objects in Vincetn have rich HTML and JavaSrcript representations." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import vincent\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import pandas.io.data as web\n", "import datetime\n", "all_data = {}\n", "date_start = datetime.datetime(2010, 1, 1)\n", "date_end = datetime.datetime(2014, 1, 1)\n", "for ticker in ['AAPL', 'IBM', 'YHOO', 'MSFT']:\n", " all_data[ticker] = web.DataReader(ticker, 'yahoo', date_start, date_end)\n", "price = pd.DataFrame({tic: data['Adj Close']\n", " for tic, data in all_data.items()})" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "vincent.initialize_notebook()" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "line = vincent.Line(price[['AAPL', 'IBM', 'YHOO', 'MSFT']], width=600, height=300)\n", "line.axis_titles(x='Date', y='Price')\n", "line.legend(title='Ticker')\n", "display(line)" ] } ], "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.4.2" } }, "nbformat": 4, "nbformat_minor": 0 }