Issue
I am trying to create a graph of the treasury yield curve to compare the rates from two separate dates. I am having difficulty with combining the two curves and creating a clean graph. My question: how do I plot the two yield curves together, with the yields (rates) are on the y-axis, and the maturities (2yr, 5yr, 10yr, 20yr, 30yr) are on the x-axis?
import numpy as np
import pandas as pd
import datetime as dt
import pandas.io.data as web
import matplotlib.pyplot as plt
import quandl as q
from pandas import DataFrame
import matplotlib
matplotlib.style.use('ggplot')
treasury = q.get("USTREASURY/YIELD", trim_start="2000-01-01", returns="pandas")
fig, ax = plt.subplots()
treas = DataFrame(treasury)
treas.drop(treas.columns[[0,1,2,3,5,7]], axis=1, inplace=True)
today = treas.iloc[-1:]
first = treas.iloc[:1]
first = first.T
today = today.T
ax.plot(first, 'o')
ax.plot(today, 'x')
#first.plot(marker='o')
#today.plot(marker='o')
plt.show()
Solution
Is this what you were looking for?
import matplotlib.pyplot as plt
import pandas as pd
import quandl as ql
#import Quandl as ql
%matplotlib inline
yield_ = ql.get("USTREASURY/YIELD")
today = yield_.iloc[-1,:]
month_ago = yield_.iloc[-30,:]
df = pd.concat([today, month_ago], axis=1)
df.columns = ['today', 'month_ago']
df.plot(style={'today': 'ro-', 'month_ago': 'bx--'}
,title='Treasury Yield Curve, %');
Answered By - Sergey Bushmanov
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.