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!
datetime
Objects¶import plotly.plotly as py
import plotly.graph_objs as go
import pandas as pd
from datetime import datetime
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
data = [go.Scatter(x=df.Date, y=df['AAPL.High'])]
py.iplot(data, filename = 'time-series-simple')
import plotly.plotly as py
import plotly.graph_objs as go
import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv")
data = [go.Scatter(
x=df.Date,
y=df['AAPL.Close'])]
py.iplot(data)
import plotly.plotly as py
import plotly.graph_objs as go
import datetime
def to_unix_time(dt):
epoch = datetime.datetime.utcfromtimestamp(0)
return (dt - epoch).total_seconds() * 1000
x = [datetime.datetime(year=2013, month=10, day=04),
datetime.datetime(year=2013, month=11, day=05),
datetime.datetime(year=2013, month=12, day=06)]
data = [go.Scatter(
x=x,
y=[1, 3, 6])]
layout = go.Layout(xaxis = dict(
range = [to_unix_time(datetime.datetime(2013, 10, 17)),
to_unix_time(datetime.datetime(2013, 11, 20))]
))
fig = go.Figure(data = data, layout = layout)
py.iplot(fig)
import plotly.plotly as py
import plotly.graph_objs as go
import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv")
trace_high = go.Scatter(
x=df.Date,
y=df['AAPL.High'],
name = "AAPL High",
line = dict(color = '#17BECF'),
opacity = 0.8)
trace_low = go.Scatter(
x=df.Date,
y=df['AAPL.Low'],
name = "AAPL Low",
line = dict(color = '#7F7F7F'),
opacity = 0.8)
data = [trace_high,trace_low]
layout = dict(
title = "Manually Set Date Range",
xaxis = dict(
range = ['2016-07-01','2016-12-31'])
)
fig = dict(data=data, layout=layout)
py.iplot(fig, filename = "Manually Set Range")
import plotly.plotly as py
import plotly.graph_objs as go
import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv")
trace_high = go.Scatter(
x=df.Date,
y=df['AAPL.High'],
name = "AAPL High",
line = dict(color = '#17BECF'),
opacity = 0.8)
trace_low = go.Scatter(
x=df.Date,
y=df['AAPL.Low'],
name = "AAPL Low",
line = dict(color = '#7F7F7F'),
opacity = 0.8)
data = [trace_high,trace_low]
layout = dict(
title='Time Series with Rangeslider',
xaxis=dict(
rangeselector=dict(
buttons=list([
dict(count=1,
label='1m',
step='month',
stepmode='backward'),
dict(count=6,
label='6m',
step='month',
stepmode='backward'),
dict(step='all')
])
),
rangeslider=dict(
visible = True
),
type='date'
)
)
fig = dict(data=data, layout=layout)
py.iplot(fig, filename = "Time Series with Rangeslider")
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-timeseriesplot/", width="100%", height="750px", frameBorder="0")
from IPython.display import IFrame
IFrame(src= "https://dash-simple-apps.plotly.host/dash-timeseriesplot/code", width="100%", height=500, frameBorder="0")
See https://plotly.com/python/reference/#layout-xaxis-rangeslider and
https://plotly.com/python/reference/#layout-xaxis-rangeselector for more information and chart attribute options!
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(
'time-series.ipynb', 'python/time-series/', 'Python Time Series | Examples | Plotly',
'How to plot date and time in python. ',
title= 'Time Series Plots | plotly',
name = 'Time Series',
has_thumbnail='true', thumbnail='thumbnail/time-series.jpg',
language='python', page_type='example_index',
display_as='financial', order=0,
ipynb='~notebook_demo/213')
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-mtlz90v1
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-mkvkhtzs/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.