gpt4 book ai didi

python - 绘制国债 yield 曲线,如何使用 matplotlib 叠加两条 yield 曲线

转载 作者:行者123 更新时间:2023-11-28 21:49:42 28 4
gpt4 key购买 nike

我正在尝试创建 treasury yield curve 的图表比较两个不同日期的汇率。我很难将两条曲线组合起来并创建一个干净的图形。 我的问题:如何将两条 yield 曲线绘制在一起, yield (利率)在 y 轴上,到期期限(2 年、5 年、10 年、20 年、30 年)在x 轴?

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()

最佳答案

这就是您要找的吗?

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, %');

enter image description here

关于python - 绘制国债 yield 曲线,如何使用 matplotlib 叠加两条 yield 曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33017564/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com