Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!
Plotly's python package is updated frequently. Run pip install plotly --upgrade
to use the latest version.
import plotly
plotly.__version__
'2.0.1'
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
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()
.
df = pd.read_csv('sample-data.csv')
sample_data_table = FF.create_table(df.head())
py.iplot(sample_data_table, filename='sample-data-table')
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')
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.
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')
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 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 can easily be deployed to a PaaS.
from IPython.display import IFrame
IFrame(src= "https://dash-simple-apps.plotly.host/dash-plotfromcsvplot/", width="100%", height="650px", frameBorder="0")
from IPython.display import IFrame
IFrame(src= "https://dash-simple-apps.plotly.host/dash-plotfromcsvplot/code", width="100%", height=500, frameBorder="0")
See https://plotly.com/python/getting-started for more information about Plotly's Python API!
from IPython.display import display, HTML
display(HTML('<link href="//fonts.googleapis.com/css?family=Open+Sans:600,400,300,200|Inconsolata|Ubuntu+Mono:400,700" rel="stylesheet" type="text/css" />'))
display(HTML('<link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">'))
! 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')
Collecting git+https://github.com/plotly/publisher.git
Cloning https://github.com/plotly/publisher.git to /private/var/folders/s5/vjqn03zs7nn8zs_fwzcf14r40000gn/T/pip-req-build-avbc02d1
Building wheels for collected packages: publisher
Building wheel for publisher (setup.py) ... done
Stored in directory: /private/var/folders/s5/vjqn03zs7nn8zs_fwzcf14r40000gn/T/pip-ephem-wheel-cache-4s27zuvk/wheels/99/3e/a0/fbd22ba24cca72bdbaba53dbc23c1768755fb17b3af0f33966
Successfully built publisher
Installing collected packages: publisher
Found existing installation: publisher 0.13
Uninstalling publisher-0.13:
Successfully uninstalled publisher-0.13
Successfully installed publisher-0.13
You are using pip version 19.0.3, however version 19.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.