티스토리 뷰

 

GOOG.csv
0.00MB

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
반응형
댓글
250x250
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함
공지사항