멋사 AI 7기 4주차 - 2

psy4072·2022년 10월 9일
0
post-thumbnail

Plotly

: 인터렉피브 시각화 도구

!pip install plotly --upgrade
import plotly.express as px

df = px.data.stocks()
=> 

		date	GOOG		AAPL		AMZN		FB			NFLX		MSFT
0	2018-01-01	1.000000	1.000000	1.000000	1.000000	1.000000	1.000000
1	2018-01-08	1.018172	1.011943	1.061881	0.959968	1.053526	1.015988

일별 수익률 선그래프

import plotly
import pandas as pd

px.line(df, x="date", y="GOOG", title="일별 시세")

# Pandas
df.set_index("date").plot(kind='line', subplots=True);

# Plotly
px.line(df.set_index("date"))

일별 수익률 막대그래프

df_1 = df.set_index("date") - 1
df_1.head(2)

			GOOG		AAPL		AMZN		FB			NFLX		MSFT
date						
2018-01-01	0.000000	0.000000	0.000000	0.000000	0.000000	0.000000
2018-01-08	0.018172	0.011943	0.061881	-0.040032	0.053526	0.015988
# Plotly express API
px.bar(df_1['GOOG']

# Pandas API
df_1['GOOG'].plot(figsize=(10, 2))

facet_col 이용한 서브플롯

df_1.columns.name = "company"
px.area(df_1, facet_col="company", facet_col_wrap=2)

여러 종목을 하나의 그래프로 표현

px.line(df, x='date', y='GOOG', hover_data={'date" : "|%y-%m-%d"})

Candlestick

# plotly.graph_objects 를 go라는 별칭으로 불러옵니다.
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
import plotly.graph_objects as go

import pandas as pd
from datetime import datetime
# go.Candlestick 을 그립니다.
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()

OHLC chart (Open-High-Low-Close)

# go.Ohlc를 그립니다.
fig = go.Figure(data=[go.Ohlc(x=df['Date'],
                open=df['AAPL.Open'],
                high=df['AAPL.High'],
                low=df['AAPL.Low'],
                close=df['AAPL.Close'])])

fig.show()

여러 종목의 수익률

pd.read_html("https://en.wikipedia.org/wiki/List_of_S%26P_500_companies")[0].head(5)
FAANG = ["FB", "AMZN", "AAPL", "NFLX", "GOOGL"]
[sym for sym in FAANG]

faang_list = [fdr.DataReader(sym, "2022")["Close"] for sym in FAANG]

df_faang = pd.concat(faang_list, axis=1)
df_faang.columns = FAANG
df_faang.head()


			FB		AMZN	AAPL	NFLX	GOOGL
Date					
2022-01-03	70.55	170.40	182.01	597.37	144.99
2022-01-04	NaN	1	67.52	179.70	591.15	144.40
2022-01-05	70.00	164.36	174.92	567.52	137.77
2022-01-06	69.70	163.25	172.00	553.29	137.75
2022-01-07	70.50	162.55	172.17	541.06	137.02
# 일별 수익률 
df_ratio = (df_faang / df_faang.iloc[0]) - 1
df_ratio.head()


			FB			AMZN		AAPL		NFLX		GOOGL
Date					
2022-01-03	0.000000	0.000000	0.000000	0.000000	0.000000
2022-01-04	NaN			-0.016901	-0.012692	-0.010412	-0.004069
2022-01-05	-0.007796	-0.035446	-0.038954	-0.049969	-0.049797
2022-01-06	-0.012048	-0.041960	-0.054997	-0.073790	-0.049934
2022-01-07	-0.000709	-0.046068	-0.054063	-0.094263	-0.054969

선 그래프

df_ratio.columns.name = "company"
px.line(df_ratio, facet_col = "company", facet_col_wrap=2, height=300)

areaplot

px.area(df_ratio,  facet_col="company", height=300 )

profile
Ⓓ🅰️🅣🄰 ♡♥︎

0개의 댓글