import pandas_datareader.data as web
#stock_data = web.DataReader('銘柄','API', 開始日, 終了日)
nikkei225 = web.DataReader('^N225', 'yahoo', "2021-04-01", "2021-04-30")
pip install --upgrade pandas
pip install --upgrade pandas-datareader
pip install plotly
import plotly
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot
print(plotly.__version__)
fig = go.Figure(data=go.Bar(y=[2, 3, 1]))
plotly.offline.iplot(fig)
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.show()
import pandas as pd
from datetime import datetime
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
df.tail()
Date | AAPL.Open | AAPL.High | AAPL.Low | AAPL.Close | AAPL.Volume | AAPL.Adjusted | dn | mavg | up | direction | |
---|---|---|---|---|---|---|---|---|---|---|---|
501 | 2017-02-10 | 132.460007 | 132.940002 | 132.050003 | 132.119995 | 20065500 | 132.119995 | 114.494004 | 124.498666 | 134.503328 | Decreasing |
502 | 2017-02-13 | 133.080002 | 133.820007 | 132.750000 | 133.289993 | 23035400 | 133.289993 | 114.820798 | 125.205166 | 135.589534 | Increasing |
503 | 2017-02-14 | 133.470001 | 135.089996 | 133.250000 | 135.020004 | 32815500 | 135.020004 | 115.175718 | 125.953499 | 136.731280 | Increasing |
504 | 2017-02-15 | 135.520004 | 136.270004 | 134.619995 | 135.509995 | 35501600 | 135.509995 | 115.545035 | 126.723499 | 137.901963 | Decreasing |
505 | 2017-02-16 | 135.669998 | 135.899994 | 134.839996 | 135.350006 | 22118000 | 135.350006 | 116.203299 | 127.504332 | 138.805366 | Decreasing |
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas as pd
# data
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
# Create figure with secondary y-axis
fig = make_subplots(specs=[[{"secondary_y": True}]])
# include candlestick with rangeselector
fig.add_trace(go.Candlestick(x=df['Date'],
open=df['AAPL.Open'], high=df['AAPL.High'],
low=df['AAPL.Low'], close=df['AAPL.Close']),
secondary_y=True)
# include a go.Bar trace for volumes
fig.add_trace(go.Bar(x=df['Date'], y=df['AAPL.Volume']),
secondary_y=False)
fig.layout.yaxis2.showgrid=False
fig.show()
import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots
# data
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
# Create subplots and mention plot grid size
fig = make_subplots(rows=2, cols=1, shared_xaxes=True,
vertical_spacing=0.03, subplot_titles=('OHLC', 'Volume'),
row_width=[0.2, 0.7])
# Plot OHLC on 1st row
fig.add_trace(go.Candlestick(x=df["Date"], open=df["AAPL.Open"], high=df["AAPL.High"],
low=df["AAPL.Low"], close=df["AAPL.Close"], name="OHLC"),
row=1, col=1
)
# Bar trace for volumes on 2nd row without legend
fig.add_trace(go.Bar(x=df['Date'], y=df['AAPL.Volume'], showlegend=False), row=2, col=1)
# Do not show OHLC's rangeslider plot
fig.update(layout_xaxis_rangeslider_visible=False)
fig.show()
figure_factory
の create_candlestick
を使って可視化した例になります。from plotly import figure_factory as FF
fig = FF.create_candlestick(nikkei225.Open, nikkei225.High, nikkei225.Low, nikkei225.Close, dates=nikkei225.index)
plotly.offline.iplot(fig)