绘制国债收益率曲线,如何使用matplotlib覆盖两条收益率曲线 [英] Plotting Treasury Yield Curve, how to overlay two yield curves using matplotlib
本文介绍了绘制国债收益率曲线,如何使用matplotlib覆盖两条收益率曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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()
这是我当前情节的样子.我试图翻转轴,以使每个收益率曲线都是一条带有标记的线.
Here is what my plot looks like currently. I am trying to flip the axis, so that each yield curve is a line with markers.
推荐答案
这是您要找的东西吗?
Is this what you were looking for?
import matplotlib.pyplot as plt
import pandas as pd
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, %');
这篇关于绘制国债收益率曲线,如何使用matplotlib覆盖两条收益率曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文