Matplotlib US Treasury Yield Curve

I'm currently trying to create a data framework consisting of daily US Treasury rates. As you can see, pandas automatically formats the columns so that they are in order, which I clearly don't want. Here are some of my code. I needed to make a small example to show the problem that I am facing.

import quandl
import matplotlib.pyplot as plt

One_Month = quandl.get('FRED/DGS1MO')

^^ Repeatable for all bets

Yield_Curve = pd.DataFrame({'1m': One_Month['Value'], '3m': Three_Month['Value'], '1yr': One_Year['Value']})
Yield_Curve.loc['2017-06-22'].plot()
plt.show()

enter image description here

Yield_Curve.tail()


             1m      1yr     3m
Date            
2017-06-16  0.85    1.21    1.03
2017-06-19  0.85    1.22    1.02
2017-06-20  0.88    1.22    1.01
2017-06-21  0.85    1.22    0.99
2017-06-22  0.80    1.22    0.96

As I said, I only added three bids to the data framework, but obviously two-year, three-year and five-year bids will also cause a problem.

I searched and saw this post: Building a Treasury yield curve, how to overlay two yield curves using matplotlib

, (One_Month, Three_Month....), , .

: ?

!

+4
3

pandas-datareader . reindex_axis, @Andrew L, , . , .

from pandas_datareader.data import DataReader as dr
syms = ['DGS10', 'DGS5', 'DGS2', 'DGS1MO', 'DGS3MO']
yc = dr(syms, 'fred') # could specify start date with start param here
names = dict(zip(syms, ['10yr', '5yr', '2yr', '1m', '3m']))
yc = yc.rename(columns=names)
yc = yc[['1m', '3m', '2yr', '5yr', '10yr']]

print(yc)
              1m    3m   2yr   5yr  10yr
DATE                                    
2010-01-01   NaN   NaN   NaN   NaN   NaN
2010-01-04  0.05  0.08  1.09  2.65  3.85
2010-01-05  0.03  0.07  1.01  2.56  3.77
2010-01-06  0.03  0.06  1.01  2.60  3.85
2010-01-07  0.02  0.05  1.03  2.62  3.85
         ...   ...   ...   ...   ...
2017-06-16  0.85  1.03  1.32  1.75  2.16
2017-06-19  0.85  1.02  1.36  1.80  2.19
2017-06-20  0.88  1.01  1.36  1.77  2.16
2017-06-21  0.85  0.99  1.36  1.78  2.16
2017-06-22  0.80  0.96  1.34  1.76  2.15

yc.loc['2016-06-01'].plot(label='Jun 1')
yc.loc['2016-06-02'].plot(label='Jun 2')
plt.legend(loc=0)

enter image description here

+2

, reindex_axis():

df = df.reindex_axis(labels=['1m', '3m', '1yr'], axis=1)

df
              1m    3m   1yr
Date                        
2017-06-16  0.85  1.03  1.21
2017-06-19  0.85  1.02  1.22
2017-06-20  0.88  1.01  1.22
2017-06-21  0.85  0.99  1.22
2017-06-22  0.80  0.96  1.22
+4

If you don't want to change the original column order, even though you need a sorted column to finance the notation, I think you should make your own custom column order, as shown below.

fi_col = df.columns.str.extract('(\d)(\D+)', expand=True).sort_values([1, 0]).reset_index(drop=True)
fi_col = fi_col[0] + fi_col[1]

print(df[fi_col])

              1m    3m   1yr
Date                        
2017-06-16  0.85  1.03  1.21
2017-06-19  0.85  1.02  1.22
2017-06-20  0.88  1.01  1.22
2017-06-21  0.85  0.99  1.22
2017-06-22  0.80  0.96  1.22
+2
source

Source: https://habr.com/ru/post/1680087/


All Articles