티스토리 뷰
matplotlib.pyplot과 pandas DataFrame을 사용하여 선 그래프를 그리는 다양한 방식을 소개하겠습니다.
- matplotlib.pyplot의 plt.plot() 사용
import pandas as pd
import matplotlib.pyplot as plt
# 데이터 읽기
df = pd.read_csv("../dataset/GOOG.csv", index_col=0, parse_dates=True)
# plt.plot() 사용하여 선 그래프 그리기
plt.figure()
plt.plot(df.index, df['Close'], label="close")
plt.plot(df.index, df['High'], label="high")
plt.xlabel("Date")
plt.ylabel("Value")
plt.legend()
plt.show()
- pandas DataFrame의 df.plot() 사용
# df.plot() 사용하여 선 그래프 그리기
df_selected=df[['Close','High']]
ax = df_selected.plot()
ax.set_xlabel("Date")
ax.set_ylabel("Value")
plt.show()
- 하나의 그래프에 여러 개의 서브플롯 (subplot) 사용
fig, axes = plt.subplots(nrows=2, ncols=1, sharex=True)
# 첫 번째 서브플롯
df['Close'].plot(ax=axes[0], title='close')
axes[0].set_ylabel("Value")
# 두 번째 서브플롯
df['high'].plot(ax=axes[1], title='high')
axes[1].set_xlabel("Date")
axes[1].set_ylabel("Value")
plt.show()
- 두 개의 컬럼을 하나의 선으로 시각화 (두 컬럼의 합)
df['sum'] = df['column1'] + df['column2']
df['sum'].plot()
plt.xlabel("Date")
plt.ylabel("Sum")
plt.show()
df['sum'] = df['column1'] + df['column2']
df['sum'].plot()
plt.xlabel("Date")
plt.ylabel("Sum")
plt.show()
728x90
반응형
'머신러닝 파이썬' 카테고리의 다른 글
LSTM을 활용한 구글 주가 예측: 딥러닝을 이용한 주식 시장 맛보기 분석(feat. Keras) (0) | 2023.04.13 |
---|---|
주가 데이터 군집화 탐색: PCA와 K-means를 활용한 시각적 분석 (0) | 2023.04.07 |
주식 데이터를 사용하여 K-means 클러스터링을 구현하는 방법(feat. 파이썬, 3D 시각화, Elbow curve) (0) | 2023.04.04 |
주가 데이터를 히스트로그램으로 시각화하기 (0) | 2023.03.31 |
주가 데이터를 heatmap 으로 시각화하기 (0) | 2023.03.31 |
댓글