I will muzzle the time series
python==3.8 plotly==4.10.0
Preprocessing of time series data is difficult, but beautiful visualization is just as difficult.
If there is a strange peak in the time series visualization, ・ What month and day was it? ・ If it is abnormal, can you refer to the comment at that time? ・ Is it possible to expand or change the scale a little more? Will occur
It's hard to go to the source data side each time, but with plotly dynamic visualization It ’s easy to identify what month and day it is. It is also possible to narrow down to a specific period and expand to only half-year orders
Try using plotly line plot
import plotly.express as px
fig = px.line(x=[1,2,3,4,5], y=[10,11,15,16,8], title="line plot")
fig.show()
Ordinary linear plot
Try using stock price data that can be called from plotly datasets
import plotly.express as px
import pandas as pd
df = px.data.stocks()
fig = px.line(df,x='date', y='NFLX', title="NFLX stocks plot")
fig.show()
If the date is entered in a recognizable format, it will be set to the time by specifying it on the X axis.
import plotly.express as px
import pandas as pd
df = px.data.stocks()
fig = px.line(df,x='date', y='NFLX', title="NFLX stocks plot", range_x=['2018-07-01','2019-12-31'])
fig.show()
import plotly.express as px
import pandas as pd
df = px.data.stocks()
fig = px.line(df,x='date', y=df.columns[1:6], title="6 company stocks plot")
fig.show()
import plotly.express as px
import pandas as pd
df = px.data.stocks()
fig = px.line(df,x='date', y=df.columns[1:6], title="6 company stocks plot")
fig.update_xaxes(
dtick="M1",
tickformat="%b\n%Y")
fig.show()
〇 Pass the month break to dtick with M 〇 The format of the scale label can be passed in character type, and the display format can be changed using line feed symbols, etc.
For example
fig.update_layout(
title="6 company stocks plot",
xaxis_tickformat = '%d %B (%a)<br>%Y'
)
If you specify as, you can operate the display of the day of the week and the date
Normally, you can refer to one point of information by hovering the mouse, but You can also change the layout to display information for all data
Normal case
import plotly.express as px
import pandas as pd
df = px.data.stocks()
fig = px.line(df,x='date', y=df.columns[1:6], title="6 company stocks plot")
fig.update_xaxes(
dtick="M1",
tickformat="%b\n%Y")
fig.update_layout(template=go.layout.Template())
fig.show()
By using update_layout, you can add a function to specify the display section in the graph put in the fig. Applicable to both go.Figure and plotly.express Convenient because you can copy and use it
Add to graph_objects
import plotly.graph_objects as go
import pandas as pd
fig = go.Figure()
fig.add_trace(go.Scatter(x=list(df.date), y=list(df.GOOG)))
fig.update_layout(
title_text="range slider and selectors"
)
fig.update_layout(
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(count=1,
label="YTD",
step="year",
stepmode="todate"),
dict(count=1,
label="1y",
step="year",
stepmode="backward"),
dict(step="all")
])
),
rangeslider=dict(
visible=True
),
type="date"
)
)
fig.show()
Add to plotly.express
import plotly.express as px
fig = px.line(df,x='date', y=df.columns[1:6])
fig.update_layout(
title_text="range slider and selectors"
)
fig.update_layout(
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(count=1,
label="YTD",
step="year",
stepmode="todate"),
dict(count=1,
label="1y",
step="year",
stepmode="backward"),
dict(step="all")
])
),
rangeslider=dict(
visible=True
),
type="date"
)
)
fig.show()
import plotly.express as px
fig = px.scatter(df,x=df['date'], y=df.columns[1:6])
fig.show()
import plotly.graph_objects as go
fig = go.Figure([
go.Scatter(
x=df['date'], y=df[df.columns[i]],
opacity=.5
)
for i in range(1,len(df.columns))
])
fig.show()
graph_objects is good at drawing like adding to the campus from above Add go.scatter to the canvas created with go.Figure This time I added go.scatter 5 times while changing the column with for, You can add them individually with add_trace In that case, some lines can be broken lines and some lines can be solid lines.
The following is a change of line type
import plotly.graph_objects as go
fig = go.Figure([
go.Scatter(
x=df['date'], y=df[df.columns[i]],
opacity=.5,
line=dict(dash='dash')
)
for i in range(1,len(df.columns))
])
fig.show()
You can easily make a candle chart by specifying the opening price; high price; low price; closing price of the stock price.
import plotly.graph_objects 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')
fig = go.Figure(data=[go.Candlestick(x=df['Date'],
open=df['AAPL.Open'],
high=df['AAPL.High'],
low=df['AAPL.Low'],
close=df['AAPL.Close'])])
fig.update_layout(
title_text="Candle"
)
fig.update_layout(
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(count=1,
label="YTD",
step="year",
stepmode="todate"),
dict(count=1,
label="1y",
step="year",
stepmode="backward"),
dict(step="all")
])
),
rangeslider=dict(
visible=True
),
type="date"
)
)
fig.show()
It's great to be able to see the values
Recommended Posts