jupyter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Time series can be represented using either plotly.express
functions (px.line
, px.scatter
) or plotly.graph_objects
charts objects (go.Scatter
). For more examples of such charts, see the documentation of line and scatter plots.
Plotly auto-sets the axis type to a date format when the corresponding data are either ISO-formatted date strings or if they're a date pandas column or datetime NumPy array.
# Using plotly.express
import plotly.express as px
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
fig = px.line(df, x='Date', y='AAPL.High')
fig.show()
# Using graph_objects
import plotly.graph_objects as go
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
fig = go.Figure([go.Scatter(x=df['Date'], y=df['AAPL.High'])])
fig.show()
The data range can be set manually using either datetime.datetime
objects, or date strings.
import plotly.graph_objects as go
import datetime
x = [datetime.datetime(year=2013, month=10, day=4),
datetime.datetime(year=2013, month=11, day=5),
datetime.datetime(year=2013, month=12, day=6)]
fig = go.Figure(data=[go.Scatter(x=x, y=[1, 3, 6])])
# Use datetime objects to set xaxis range
fig.update_layout(xaxis_range=[datetime.datetime(2013, 10, 17),
datetime.datetime(2013, 11, 20)])
fig.show()
import plotly.graph_objects as go
import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv")
fig = go.Figure()
fig.add_trace(go.Scatter(
x=df.Date,
y=df['AAPL.High'],
name="AAPL High",
line_color='deepskyblue',
opacity=0.8))
fig.add_trace(go.Scatter(
x=df.Date,
y=df['AAPL.Low'],
name="AAPL Low",
line_color='dimgray',
opacity=0.8))
# Use date string to set xaxis range
fig.update_layout(xaxis_range=['2016-07-01','2016-12-31'],
title_text="Manually Set Date Range")
fig.show()
import plotly.graph_objects as go
import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv")
fig = go.Figure()
fig.add_trace(go.Scatter(x=df.Date, y=df['AAPL.High'], name="AAPL High",
line_color='deepskyblue'))
fig.add_trace(go.Scatter(x=df.Date, y=df['AAPL.Low'], name="AAPL Low",
line_color='dimgray'))
fig.update_layout(title_text='Time Series with Rangeslider',
xaxis_rangeslider_visible=True)
fig.show()
See https://plot.ly/python/reference/#layout-xaxis-rangeslider and
https://plot.ly/python/reference/#layout-xaxis-rangeselector for more information and chart attribute options!