{ "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 downloading 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!\n", "#### Version Check\n", "Plotly's python package is updated frequently. Run `pip install plotly --upgrade` to use the latest version." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'2.0.1'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly\n", "plotly.__version__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Imports" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "import plotly.figure_factory as FF\n", "\n", "import numpy as np\n", "import pandas as pd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### A Simple Example\n", "CSV or comma-delimited-values is a very popular format for storing structured data. In this tutorial, we will see how to plot beautiful graphs using csv data, and Pandas. We will import data from a local file `sample-data.csv` with the pandas function: `read_csv()`." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv('sample-data.csv')\n", "\n", "sample_data_table = FF.create_table(df.head())\n", "py.iplot(sample_data_table, filename='sample-data-table')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "trace1 = go.Scatter(\n", " x=df['x'], y=df['logx'], # Data\n", " mode='lines', name='logx' # Additional options\n", " )\n", "trace2 = go.Scatter(x=df['x'], y=df['sinx'], mode='lines', name='sinx' )\n", "trace3 = go.Scatter(x=df['x'], y=df['cosx'], mode='lines', name='cosx')\n", "\n", "layout = go.Layout(title='Simple Plot from csv data',\n", " plot_bgcolor='rgb(230, 230,230)')\n", "\n", "fig = go.Figure(data=[trace1, trace2, trace3], layout=layout)\n", "\n", "# Plot data in the notebook\n", "py.iplot(fig, filename='simple-plot-from-csv')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Plotting Data from External Source\n", "In the next example, we will learn how to import csv data from an external source (a url), and plot it using Plotly and pandas. We are going to use this data for the example." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_apple_stock.csv')\n", "\n", "df_external_source = FF.create_table(df.head())\n", "py.iplot(df_external_source, filename='df-external-source-table')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "trace = go.Scatter(x = df['AAPL_x'], y = df['AAPL_y'],\n", " name='Share Prices (in USD)')\n", "layout = go.Layout(title='Apple Share Prices over time (2014)',\n", " plot_bgcolor='rgb(230, 230,230)', \n", " showlegend=True)\n", "fig = go.Figure(data=[trace], layout=layout)\n", "\n", "py.iplot(fig, filename='apple-stock-prices')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dash Example" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[Dash](https://plotly.com/products/dash/) is an Open Source Python library which can help you convert plotly figures into a reactive, web-based application. Below is a simple example of a dashboard created using Dash. Its [source code](https://github.com/plotly/simple-example-chart-apps/tree/master/dash-plotfromcsvplot) can easily be deployed to a PaaS." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.display import IFrame\n", "IFrame(src= \"https://dash-simple-apps.plotly.host/dash-plotfromcsvplot/\", width=\"100%\", height=\"650px\", frameBorder=\"0\")\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.display import IFrame\n", "IFrame(src= \"https://dash-simple-apps.plotly.host/dash-plotfromcsvplot/code\", width=\"100%\", height=500, frameBorder=\"0\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Reference\n", "See https://plotly.com/python/getting-started for more information about Plotly's Python API!" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "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 /private/var/folders/s5/vjqn03zs7nn8zs_fwzcf14r40000gn/T/pip-req-build-avbc02d1\n", "Building wheels for collected packages: publisher\n", " Building wheel for publisher (setup.py) ... \u001b[?25ldone\n", "\u001b[?25h Stored in directory: /private/var/folders/s5/vjqn03zs7nn8zs_fwzcf14r40000gn/T/pip-ephem-wheel-cache-4s27zuvk/wheels/99/3e/a0/fbd22ba24cca72bdbaba53dbc23c1768755fb17b3af0f33966\n", "Successfully built publisher\n", "Installing collected packages: publisher\n", " Found existing installation: publisher 0.13\n", " Uninstalling publisher-0.13:\n", " Successfully uninstalled publisher-0.13\n", "Successfully installed publisher-0.13\n", "\u001b[33mYou are using pip version 19.0.3, however version 19.1.1 is available.\n", "You should consider upgrading via the 'pip install --upgrade pip' command.\u001b[0m\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", " 'plotting-csv-data.ipynb', 'python/plot-data-from-csv/', 'Plot CSV Data',\n", " 'How to create charts from csv files with Plotly and Python',\n", " title = 'Plot Data from CSV | plotly',\n", " thumbnail='thumbnail/csv.jpg', language='python',\n", " page_type='example_index', has_thumbnail='false', display_as='databases', order=1,\n", " ipynb= '~notebook_demo/84')" ] } ], "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.5" } }, "nbformat": 4, "nbformat_minor": 1 }