시각화하기 (Visualization)¶
pandas Dataframe 에 담긴 데이터를 그래프로 그려 봅니다.
- Seaborn 라이브러리로 이미지 형태로 그려봅니다.
- plotly 라이브러리로 반응형 그래프를 그려봅니다.
In [1]:
Copied!
# 필요 라이브러리 설치
%pip install -U matplotlib seaborn plotly
# 필요 라이브러리 설치
%pip install -U matplotlib seaborn plotly
Successfully installed contourpy-1.3.1 cycler-0.12.1 fonttools-4.55.3 kiwisolver-1.4.8 matplotlib-3.10.0 pillow-11.0.0 plotly-5.24.1 pyparsing-3.2.0 seaborn-0.13.2 tenacity-9.0.0 [notice] A new release of pip is available: 24.0 -> 24.3.1 [notice] To update, run: pip install --upgrade pip Note: you may need to restart the kernel to use updated packages.
In [1]:
Copied!
import pandas as pd
df = pd.read_csv("./AAPL_data.csv")[2:]
df
import pandas as pd
df = pd.read_csv("./AAPL_data.csv")[2:]
df
Out[1]:
| Price | Close | High | Low | Open | Volume | |
|---|---|---|---|---|---|---|
| 2 | 2023-01-03 | 123.7684555053711 | 129.53777972145332 | 122.87781986866916 | 128.92423659950236 | 112117500 |
| 3 | 2023-01-04 | 125.04503631591797 | 127.32110440506466 | 123.77835783326215 | 125.56951966733021 | 89113600 |
| 4 | 2023-01-05 | 123.71897888183594 | 126.44036106917035 | 123.46169000194207 | 125.80702181866339 | 80962700 |
| 5 | 2023-01-06 | 128.27110290527344 | 128.93412872931725 | 123.59032994150238 | 124.6986773645302 | 87754700 |
| 6 | 2023-01-09 | 128.7955780029297 | 132.02166222689684 | 128.53828914886986 | 129.11225514638548 | 70790800 |
| ... | ... | ... | ... | ... | ... | ... |
| 247 | 2023-12-22 | 192.6561737060547 | 194.45734722393942 | 192.02924020237552 | 194.22845757962483 | 37122800 |
| 248 | 2023-12-26 | 192.10885620117188 | 192.94475743470915 | 191.88992751842636 | 192.66612369019674 | 28919300 |
| 249 | 2023-12-27 | 192.20835876464844 | 192.5566585360189 | 190.15840400250286 | 191.55158790358445 | 48087700 |
| 250 | 2023-12-28 | 192.6362762451172 | 193.71101293849657 | 192.22827139973785 | 193.1935437488545 | 34049900 |
| 251 | 2023-12-29 | 191.5913848876953 | 193.452263485871 | 190.7952819760843 | 192.9547010641641 | 42628800 |
250 rows × 6 columns
In [17]:
Copied!
df["Open"] = df["Open"].astype('float')
df["Volume"] = df["Volume"].astype('float')
df["Close"] = df["Close"].astype('float')
df['date'] = pd.to_datetime(df["Price"])
df.dtypes
df["Open"] = df["Open"].astype('float')
df["Volume"] = df["Volume"].astype('float')
df["Close"] = df["Close"].astype('float')
df['date'] = pd.to_datetime(df["Price"])
df.dtypes
Out[17]:
Price object Close float64 High object Low object Open float64 Volume float64 date datetime64[ns] date_numeric int64 dtype: object
In [18]:
Copied!
df['date_numeric'] = (df['date'] - pd.Timestamp("2023-01-01")) // pd.Timedelta('1d')
sns.regplot(x='date_numeric', y='Close', data=df)
df['date_numeric'] = (df['date'] - pd.Timestamp("2023-01-01")) // pd.Timedelta('1d')
sns.regplot(x='date_numeric', y='Close', data=df)
In [4]:
Copied!
import plotly.express as px
px.line(df, x = "date", y = 'Close')
import plotly.express as px
px.line(df, x = "date", y = 'Close')
In [5]:
Copied!
import plotly.graph_objects as go
# Create a figure
fig = go.Figure()
# Add line plot for 'value'
fig.add_trace(go.Scatter(x=df['date'], y=df['Close'], name='Close', mode='lines', yaxis='y1'))
# Add bar plot for 'volume' on secondary y-axis
fig.add_trace(go.Bar(x=df['date'], y=df['Volume'], name='Volume', yaxis='y2', opacity=0.6))
# Update layout for dual axes
fig.update_layout(
title='Time vs Value and Volume',
xaxis=dict(title='Time'),
yaxis=dict(
title='Value',
titlefont=dict(color='blue'),
tickfont=dict(color='blue'),
),
yaxis2=dict(
title='Volume',
titlefont=dict(color='orange'),
tickfont=dict(color='orange'),
anchor='x',
overlaying='y',
side='right',
),
legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1),
xaxis_tickformat='%Y-%m-%d %H:%M',
)
# Show the figure
fig.show()
import plotly.graph_objects as go
# Create a figure
fig = go.Figure()
# Add line plot for 'value'
fig.add_trace(go.Scatter(x=df['date'], y=df['Close'], name='Close', mode='lines', yaxis='y1'))
# Add bar plot for 'volume' on secondary y-axis
fig.add_trace(go.Bar(x=df['date'], y=df['Volume'], name='Volume', yaxis='y2', opacity=0.6))
# Update layout for dual axes
fig.update_layout(
title='Time vs Value and Volume',
xaxis=dict(title='Time'),
yaxis=dict(
title='Value',
titlefont=dict(color='blue'),
tickfont=dict(color='blue'),
),
yaxis2=dict(
title='Volume',
titlefont=dict(color='orange'),
tickfont=dict(color='orange'),
anchor='x',
overlaying='y',
side='right',
),
legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1),
xaxis_tickformat='%Y-%m-%d %H:%M',
)
# Show the figure
fig.show()
In [ ]:
Copied!