-
Notifications
You must be signed in to change notification settings - Fork 8
/
GSW_yields_animation.py
42 lines (32 loc) · 1.49 KB
/
GSW_yields_animation.py
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
32
33
34
35
36
37
38
39
40
41
# coding: utf-8
# In[1]:
"""
US Yield Curve Dynamics
Repository of materials (not including this file):
* https://github.com/NYUDataBootcamp/Materials
Written by David Cai, June 2015, txc202@nyu.edu
Created with Python 3.4.3
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# input yield data from csv
file = '/Users/davidcai/YieldsAssignment/GSW_nominal_yields.csv' # file location
df = pd.read_csv(file, index_col=0) # read in csv file and treat first column as row labels
dates = df.index # create list of dates
maturity = list(range(1,122))
def animate(nframe):
plt.cla() # clear window
plt.plot(maturity, df.iloc[nframe], label='%s'%dates[nframe]) # graph yield vs maturity, label graph with date
plt.xlim(0,120) # Set x-axis range from 0 to 120 months
plt.xlabel('Maturity (months)') # add x-axis label
plt.ylim(-5,20) # Set y-axis range from -5 to 20 percent
plt.ylabel('Yield (percentage)') # add y-axis label
plt.title('US Treasury Yields') # add title
plt.legend(loc='upper center', borderpad=0.25) # move legend to upper center, increase legend whitespace
fig = plt.figure(figsize=(5,5)) # set window size to 5 x 5 inches
anim = animation.FuncAnimation(fig, animate, frames=len(df))
# create new graph for each observation date using animate function
anim.save('bond_yields.gif', writer='imagemagick', fps=6); # save to gif file, requires external program ImageMagick
# animation has six frames per second