Python/Time Series with Python
TimeSeries with Python _ Pandas 4 _plot
MINSU KANG
2019. 12. 24. 22:48
이번 포스팅에서는 Pandas로 그림을 그리는 메소드인 plot에 대해서 살펴본다.
plot을 이용해 Pandas의 그림을 그리고, 다양한 변수들을 이용해 그림을 꾸미는것을 살펴본다
df = pd.read_csv(path, index_col = "Date", parse_dates=True)
#%% Plot Formatting
title = "Starbucks Closing Stock Prices"
ylabel = 'Closing Price'
xlabel = 'Closing Date'
ax = df['Close'].plot(figsize=(12,5)) # ax로 변수를 선언해도 plot이 실행된다
ax.autoscale(axis='x', tight=True)
ax.set(xlabel = xlabel, ylabel = ylabel)
#figure 1
# X 의 제한범위
df['Close']['2017-01-01':'2017-03-01'].plot(figsize=(12,4)).autoscale(axis='x', tight=True)
df['Close'].plot(figsize=(12,4), xlim=['2017-01-01','2017-03-01']) #위 표현을 다르게 표현하는 방법
#figure 2
# 제목 및 각 축의 라벨들을 표현하는 방법
title = 'Stock Prices'
ylabel = 'Price'
xlabel = 'Date'
ax = df['Close'].plot(xlim = ['2017-01-04','2017-03-01'], ylim = [51,57], figsize=(12,4), title=title)
ax.set(xlabel=xlabel, ylabel=ylabel)
# 색깔 및 스타일
df['Close'].plot(xlim = ['2017-01-01','2017-03-01'], ylim=[51,57], ls='--', c='r')
#%%
# X축 메소드
from matplotlib import dates
ax = df['Close'].plot(xlim=['2017-01-01','2017-03-01'],ylim=[51,57])
# REMOVE PANDAS DEFAULT "Date" LABEL
ax.set(xlabel='')
ax.xaxis.set_major_locator(dates.WeekdayLocator(byweekday=0))
#%%
ax = df['Close'].plot(xlim=['2017-01-01','2017-03-01'],ylim=[51,57],title='Stock Prices')
ax.set(xlabel='')
ax.xaxis.set_major_locator(dates.WeekdayLocator(byweekday=0))
ax.xaxis.set_major_formatter(dates.DateFormatter("%a-%B-%d"))
#figure 3
#%%
ax = df['Close'].plot(xlim=['2017-01-01','2017-03-01'],ylim=[51,57],rot=0,title='Stock Prices')
ax.set(xlabel='')
ax.xaxis.set_major_locator(dates.WeekdayLocator(byweekday=0))
ax.xaxis.set_major_formatter(dates.DateFormatter('%d'))
ax.xaxis.set_minor_locator(dates.MonthLocator())
ax.xaxis.set_minor_formatter(dates.DateFormatter('\n\n%b'))
#figure 4
#%%
ax = df['Close'].plot(xlim=['2017-01-01','2017-03-01'],ylim=[51,57],rot=0,title='Stock Prices')
ax.set(xlabel='')
ax.xaxis.set_major_locator(dates.WeekdayLocator(byweekday=0))
ax.xaxis.set_major_formatter(dates.DateFormatter('%d'))
ax.xaxis.set_minor_locator(dates.MonthLocator())
ax.xaxis.set_minor_formatter(dates.DateFormatter('\n\n%b'))
ax.yaxis.grid(True)
ax.xaxis.grid(True)
#figure 5
#Legend 추가
test_predictions.plot(legend=True,label='PREDICTION',xlim=['1958-01-01','1961-01-01'])