{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#### New to Plotly?\n", "Plotly's Python library is free and open source! [Get started](https://plotly.com/python/getting-started/) by dowloading the client and [reading the primer](https://plotly.com/python/getting-started/).\n", "
You can set up Plotly to work in [online](https://plotly.com/python/getting-started/#initialization-for-online-plotting) or [offline](https://plotly.com/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plotly.com/python/getting-started/#start-plotting-online).\n", "
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Imports\n", "The tutorial below imports [numpy](http://www.numpy.org/), [pandas](https://plotly.com/pandas/intro-to-pandas-tutorial/), and [scipy](https://www.scipy.org/)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "from plotly.tools import FigureFactory as FF\n", "\n", "import numpy as np\n", "import pandas as pd\n", "import scipy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Make the Data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We are generating a 1D dataset from a `Weibull Distribution` which has the distrubution\n", "\n", "$$\n", "\\begin{align*}\n", "X = \\log(U)^{\\frac{1}{a}}\n", "\\end{align*}\n", "$$\n", "\n", "where $U$ is drawn from the `Uniform Distribution`." ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 0.86317076 0.79217698 2.07432654 0.70721605 0.24102326 1.44261213\n", " 0.85526797 1.0158948 1.19976016 1.78112064]\n" ] } ], "source": [ "x=np.random.weibull(1.25, size=1000)\n", "print(x[:10])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Histogram" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By using a histogram, we can properly divide a 1D dataset into bins with a particular size or width, so as to form a discrete probability distribution" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "trace = go.Histogram(x=x, xbins=dict(start=np.min(x), size=0.25, end=np.max(x)),\n", " marker=dict(color='rgb(0, 0, 100)'))\n", "\n", "layout = go.Layout(\n", " title=\"Histogram Frequency Counts\"\n", ")\n", "\n", "fig = go.Figure(data=go.Data([trace]), layout=layout)\n", "py.iplot(fig, filename='histogram-freq-counts')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Larger Bins" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can experiment with our bin size and the histogram by grouping the data into larger intervals" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "trace = go.Histogram(x=x, xbins=dict(start=np.min(x), size=0.75, end=np.max(x)),\n", " marker=dict(color='rgb(0, 0, 100)'))\n", "\n", "layout = go.Layout(\n", " title=\"Histogram Frequency Counts\"\n", ")\n", "\n", "fig = go.Figure(data=go.Data([trace]), layout=layout)\n", "py.iplot(fig, filename='histogram-freq-counts-larger-bins')" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Collecting git+https://github.com/plotly/publisher.git\n", " Cloning https://github.com/plotly/publisher.git to /var/folders/ld/6cl3s_l50wd40tdjq2b03jxh0000gp/T/pip-ctBFME-build\n", "Installing collected packages: publisher\n", " Found existing installation: publisher 0.10\n", " Uninstalling publisher-0.10:\n", " Successfully uninstalled publisher-0.10\n", " Running setup.py install for publisher ... \u001b[?25l-\b \b\\\b \bdone\n", "\u001b[?25hSuccessfully installed publisher-0.10\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/Users/brandendunbar/Desktop/test/venv/lib/python2.7/site-packages/IPython/nbconvert.py:13: ShimWarning: The `IPython.nbconvert` package has been deprecated. You should import from nbconvert instead.\n", " \"You should import from nbconvert instead.\", ShimWarning)\n", "/Users/brandendunbar/Desktop/test/venv/lib/python2.7/site-packages/publisher/publisher.py:53: UserWarning: Did you \"Save\" this notebook before running this command? Remember to save, always save.\n", " warnings.warn('Did you \"Save\" this notebook before running this command? '\n" ] } ], "source": [ "from IPython.display import display, HTML\n", "\n", "display(HTML(''))\n", "display(HTML(''))\n", "\n", "! pip install git+https://github.com/plotly/publisher.git --upgrade\n", "import publisher\n", "publisher.publish(\n", " 'python-Frequency-Counts.ipynb', 'python/frequency-counts/', 'Frequency Counts | plotly',\n", " 'Learn how to perform frequency counts using Python.',\n", " title='Frequency Counts in Python. | plotly',\n", " name='Frequency Counts',\n", " language='python',\n", " page_type='example_index', has_thumbnail='false', display_as='statistics', order=2,\n", " ipynb= '~notebook_demo/111')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } }, "nbformat": 4, "nbformat_minor": 0 }