#!/usr/bin/env python # coding: utf-8 # #### New to Plotly? # 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/). #
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). #
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started! # #### Version Check # Plotly's python package is updated frequently. Run `pip install plotly --upgrade` to use the latest version. # In[1]: import plotly plotly.__version__ # #### Imports # In[2]: import plotly.plotly as py import plotly.graph_objs as go import plotly.figure_factory as FF import numpy as np import pandas as pd # #### A Simple Example # 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()`. # In[3]: df = pd.read_csv('sample-data.csv') sample_data_table = FF.create_table(df.head()) py.iplot(sample_data_table, filename='sample-data-table') # In[4]: trace1 = go.Scatter( x=df['x'], y=df['logx'], # Data mode='lines', name='logx' # Additional options ) trace2 = go.Scatter(x=df['x'], y=df['sinx'], mode='lines', name='sinx' ) trace3 = go.Scatter(x=df['x'], y=df['cosx'], mode='lines', name='cosx') layout = go.Layout(title='Simple Plot from csv data', plot_bgcolor='rgb(230, 230,230)') fig = go.Figure(data=[trace1, trace2, trace3], layout=layout) # Plot data in the notebook py.iplot(fig, filename='simple-plot-from-csv') # #### Plotting Data from External Source # 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. # In[5]: df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_apple_stock.csv') df_external_source = FF.create_table(df.head()) py.iplot(df_external_source, filename='df-external-source-table') # In[6]: trace = go.Scatter(x = df['AAPL_x'], y = df['AAPL_y'], name='Share Prices (in USD)') layout = go.Layout(title='Apple Share Prices over time (2014)', plot_bgcolor='rgb(230, 230,230)', showlegend=True) fig = go.Figure(data=[trace], layout=layout) py.iplot(fig, filename='apple-stock-prices') # ### Dash Example # [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. # In[1]: from IPython.display import IFrame IFrame(src= "https://dash-simple-apps.plotly.host/dash-plotfromcsvplot/", width="100%", height="650px", frameBorder="0") # In[1]: from IPython.display import IFrame IFrame(src= "https://dash-simple-apps.plotly.host/dash-plotfromcsvplot/code", width="100%", height=500, frameBorder="0") # #### Reference # See https://plotly.com/python/getting-started for more information about Plotly's Python API! # In[3]: from IPython.display import display, HTML display(HTML('')) display(HTML('')) get_ipython().system(' pip install git+https://github.com/plotly/publisher.git --upgrade') import publisher publisher.publish( 'plotting-csv-data.ipynb', 'python/plot-data-from-csv/', 'Plot CSV Data', 'How to create charts from csv files with Plotly and Python', title = 'Plot Data from CSV | plotly', thumbnail='thumbnail/csv.jpg', language='python', page_type='example_index', has_thumbnail='false', display_as='databases', order=1, ipynb= '~notebook_demo/84')