import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as dates
%matplotlib inline
df_apple = pd.read_csv('data/apple_stock.csv',index_col='Date',parse_dates=True)
df_apple.head()
High | Low | Open | Close | Volume | Adj Close | |
---|---|---|---|---|---|---|
Date | ||||||
2009-12-31 | 30.478571 | 30.080000 | 30.447144 | 30.104286 | 88102700.0 | 20.159719 |
2010-01-04 | 30.642857 | 30.340000 | 30.490000 | 30.572857 | 123432400.0 | 20.473503 |
2010-01-05 | 30.798571 | 30.464285 | 30.657143 | 30.625713 | 150476200.0 | 20.508902 |
2010-01-06 | 30.747143 | 30.107143 | 30.625713 | 30.138571 | 138040000.0 | 20.182680 |
2010-01-07 | 30.285715 | 29.864286 | 30.250000 | 30.082857 | 119282800.0 | 20.145369 |
# Adj.Close 와 Adj.Volume 의 variance 문제로 보기 불편함.
df_apple[['Volume','Adj Close']].plot()
<matplotlib.axes._subplots.AxesSubplot at 0x7ff81ec060f0>
# Adj.Close 와 Adj.Volume 의 축을 함께 그리기
df_apple[['Volume','Adj Close']].plot(secondary_y=['Volume'])
<matplotlib.axes._subplots.AxesSubplot at 0x7ff81e9033c8>
# figsize 조정, ylabel, xlabel, title 추가
df_apple['Adj Close'].plot(figsize=(12,8))
plt.ylabel('Close Price')
plt.xlabel('Overwrite Date Index')
plt.title('Apple')
Text(0.5, 1.0, 'Apple')
df_apple['Adj Close'].plot(xlim=['2015-01-01','2018-01-01'])
<matplotlib.axes._subplots.AxesSubplot at 0x7ff8187a70f0>
df_apple['Adj Close'].plot(xlim=['2015-01-01','2018-01-01'],ylim=[80,180])
<matplotlib.axes._subplots.AxesSubplot at 0x7ff817d514a8>
df_apple['Adj Close'].plot(xlim=['2015-01-01','2018-01-01'],ylim=[80,180],
ls='--',c='r')
<matplotlib.axes._subplots.AxesSubplot at 0x7ff8189b01d0>
idx = df_apple.loc['2015-01-01':'2018-01-01'].index
stock = df_apple.loc['2015-01-01':'2018-01-01']['Adj Close']
fig, ax = plt.subplots()
ax.plot_date(idx, stock,'-')
plt.tight_layout()
plt.show()
fig, ax = plt.subplots()
ax.plot_date(idx, stock,'-')
fig.autofmt_xdate() # Auto fixes the overlap!
plt.tight_layout()
plt.show()
fig, ax = plt.subplots()
ax.plot_date(idx, stock,'-')
ax.yaxis.grid(True)
ax.xaxis.grid(True)
fig.autofmt_xdate() # Auto fixes the overlap!
plt.tight_layout()
plt.show()
idx = df_apple.loc['2018-01-01':].index
stock = df_apple.loc['2018-01-01':]['Adj Close']
fig, ax = plt.subplots(figsize=(15,7))
ax.plot_date(idx, stock,'-')
# Grids
ax.yaxis.grid(True)
ax.xaxis.grid(True)
# Major Axis
ax.xaxis.set_major_locator(dates.MonthLocator())
ax.xaxis.set_major_formatter(dates.DateFormatter('%b\n%Y'))
fig.autofmt_xdate() # Auto fixes the overlap!
plt.tight_layout()
plt.show()
fig, ax = plt.subplots(figsize=(15,7))
ax.plot_date(idx, stock,'-')
# Grids
ax.yaxis.grid(True)
ax.xaxis.grid(True)
# Major Axis
ax.xaxis.set_major_locator(dates.MonthLocator())
ax.xaxis.set_major_formatter(dates.DateFormatter('\n\n\n\n%Y--%B'))
fig.autofmt_xdate() # Auto fixes the overlap!
plt.tight_layout()
plt.show()
fig, ax = plt.subplots(figsize=(15,7))
ax.plot_date(idx, stock,'-')
# Major Axis
ax.xaxis.set_major_locator(dates.MonthLocator())
ax.xaxis.set_major_formatter(dates.DateFormatter('\n\n%Y--%B'))
# Minor Axis
ax.xaxis.set_minor_locator(dates.WeekdayLocator())
ax.xaxis.set_minor_formatter(dates.DateFormatter('%d'))
# Grids
ax.yaxis.grid(True)
ax.xaxis.grid(True)
fig.autofmt_xdate() # Auto fixes the overlap!
plt.tight_layout()
plt.show()
fig, ax = plt.subplots(figsize=(15,8))
ax.plot_date(idx, stock,'-')
# Major Axis
ax.xaxis.set_major_locator(dates.WeekdayLocator(byweekday=1))
ax.xaxis.set_major_formatter(dates.DateFormatter('%B-%d-%a'))
# Grids
ax.yaxis.grid(True)
ax.xaxis.grid(True)
fig.autofmt_xdate() # Auto fixes the overlap!
plt.tight_layout()
plt.show()