Matplotlib展示Finance Crisis的标准普尔指数

本文主要讲解annotate的用法,用的是标准普尔指数在Finance Crisis的例子。标准普尔指数数据请见GitHub。

import pandas as pd
import numpy as np
%matplotlib inline
%matplotlib notebook
import matplotlib.pyplot as plt

检查并清洗数据

# 检查1
data = pd.read_csv('data/spx/spx.csv',index_col=0,parse_dates=True)
data.info()
#<class 'pandas.core.frame.DataFrame'>
#DatetimeIndex: 5472 entries, 1990-02-01 to 2011-10-14
#Data columns (total 1 columns):
# #   Column  Non-Null Count  Dtype  
#---  ------  --------------  -----  
# 0   SPX     5472 non-null   float64
#dtypes: float64(1)
#memory usage: 85.5 KB
# 检查2
data.head()
#           SPX
#1990-02-01 328.79
#1990-02-02 330.92
#1990-02-05 331.85
#1990-02-06 329.66
#1990-02-07 333.75
# 清洗数据
spx = data['SPX']
spx
#1990-02-01     328.79
#1990-02-02     330.92
#1990-02-05     331.85
#1990-02-06     329.66
#1990-02-07     333.75
#               ...   
#2011-10-10    1194.89
#2011-10-11    1195.54
#2011-10-12    1207.25
#2011-10-13    1203.66
#2011-10-14    1224.58
#Name: SPX, Length: 5472, dtype: float64

画图表

# 初步的图表
from datetime import datetime
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
spx.plot(ax=ax,style='k-')

在这里插入图片描述

# 修正图表 : 添加文字解释,保留2007-2011数据
crisis_data = [
    (datetime(2007,10,11),'Peak of Bull Market'),
    (datetime(2008,3,12),'Bear Stearns Fails'),
    (datetime(2008,9,15),'Lehman Bankruptcy')
]

for date,label in crisis_data:
    ax.annotate(label,xy=(date,spx.asof(date)+50),xytext=(date,spx.asof(date)+200),arrowprops=dict(facecolor='red'),
               horizontalalignment='left',verticalalignment='top')

ax.set_xlim(['1/1/2007','1/1/2011'])
ax.set_ylim([600,1800])
ax.set_title('Important dates in 2008-2009 financial crisis')

在这里插入图片描述

将图标保存到文件,调整图像的边距

# bbox_inches='tight'
plt.savefig('spx.jpg',bbox_inches='tight')

猜你喜欢

转载自blog.csdn.net/m0_46629123/article/details/108889718