6. 观察股价

  1. # 读取Schlumberger stock数据集,行索引设为Date列,并将其转变为DatetimeIndex
  2. In[43]: slb = pd.read_csv('data/slb_stock.csv', index_col='Date', parse_dates=['Date'])
  3. slb.head()
  4. Out[43]:

6. 观察股价 - 图1

  1. # 选取Close这列,用describe返回统计信息
  2. In[44]: slb_close = slb['Close']
  3. slb_summary = slb_close.describe(percentiles=[.1, .9])
  4. slb_summary
  5. Out[44]: count 1895.000000
  6. mean 79.121905
  7. std 11.767802
  8. min 51.750000
  9. 10% 64.892000
  10. 50% 78.000000
  11. 90% 93.248000
  12. max 117.950000
  13. Name: Close, dtype: float64
  1. # 用布尔索引选取最高和最低10%的收盘价
  2. In[45]: upper_10 = slb_summary.loc['90%']
  3. lower_10 = slb_summary.loc['10%']
  4. criteria = (slb_close < lower_10) | (slb_close > upper_10)
  5. slb_top_bottom_10 = slb_close[criteria]
  6. # 过滤出的数据使用灰色,所有的收盘价使用黑色,用matplotlib在十分之一和十分之九分位数位置画横线
  7. In[46]: slb_close.plot(color='black', figsize=(12,6))
  8. slb_top_bottom_10.plot(marker='o', style=' ', ms=4, color='lightgray')
  9. xmin = criteria.index[0]
  10. xmax = criteria.index[-1]
  11. plt.hlines(y=[lower_10, upper_10], xmin=xmin, xmax=xmax,color='black')
  12. Out[46]: <matplotlib.collections.LineCollection at 0x1174b3278>

6. 观察股价 - 图2

更多

  1. # 使用fill_between可以在两条线之间填充颜色
  2. In[47]: slb_close.plot(color='black', figsize=(12,6))
  3. plt.hlines(y=[lower_10, upper_10],
  4. xmin=xmin, xmax=xmax,color='lightgray')
  5. plt.fill_between(x=criteria.index, y1=lower_10,
  6. y2=slb_close.values, color='black')
  7. plt.fill_between(x=criteria.index,y1=lower_10,
  8. y2=slb_close.values, where=slb_close < lower_10,
  9. color='lightgray')
  10. plt.fill_between(x=criteria.index, y1=upper_10,
  11. y2=slb_close.values, where=slb_close > upper_10,
  12. color='lightgray')
  13. Out[47]:

6. 观察股价 - 图3