Graph with segmented colors and horizontal multiple graphs
The following python code in the Jupyter notebook plot a graph with segmented colors. It is done by splitting x and y respectively.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import numpy as np import matplotlib.pyplot as plt # sample data x = np.arange(0.0, 2.0, 0.01) y_fit = np.sin(2*np.pi*x) y_real = y_fit + (np.random.random(x.size)-0.5)*0.5 # graph with segmented colors fig, ax = plt.subplots() ax.plot( x[0:50], y_fit [0:50], color = 'blue') ax.scatter(x[0:50], y_real[0:50], color = 'blue') ax.plot( x[50:100], y_fit [50:100], color = 'red') ax.scatter(x[50:100], y_real[50:100], color = 'red') ax.plot( x[100:], y_fit [100:], color = 'black') ax.scatter(x[100:], y_real[100:], color = 'black') plt.show | cs |
In fact, I want to draw the following horizontal multiple graphs of yield curves at selected dates.
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 42 43 44 45 46 47 48 49 50 51 | import numpy as np import matplotlib.pyplot as plt # whole x axis = mat1, mat2, mat3 mat1 = [3, 6, 9, 12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 144, 180, 240] mat2 = [x+300 for x in mat1] mat3 = [x+300 for x in mat2] # Observed yields at 2019-12-31, 2020-12-31, 2021-12-31 y1 = [1.523, 1.571, 1.595, 1.606, 1.586, 1.608, 1.659, 1.699, 1.768, 1.818, 1.864, 1.881, 1.939, 1.999, 2.107, 2.345] y2 = [0.073, 0.09 , 0.097, 0.103, 0.126, 0.168, 0.263, 0.374, 0.511, 0.648, 0.773, 0.847, 0.925, 1.047, 1.223, 1.505] y3 = [0.097, 0.187, 0.293, 0.399, 0.731, 0.969, 1.165, 1.272, 1.363, 1.432, 1.432, 1.464, 1.488, 1.551, 1.676, 1.98 ] # Fitted yields at 2019-12-31, 2020-12-31, 2021-12-31 y1_fit = [1.579, 1.573, 1.57 , 1.569, 1.58 , 1.61 , 1.651, 1.7 , 1.752, 1.804, 1.856, 1.906, 1.954, 2.04 , 2.147, 2.277] y2_fit = [0.116, 0.093, 0.079, 0.071, 0.094, 0.173, 0.281, 0.399, 0.519, 0.635, 0.742, 0.841, 0.931, 1.085, 1.26 , 1.453] y3_fit = [0.096, 0.206, 0.307, 0.401, 0.71 , 0.938, 1.109, 1.24 , 1.341, 1.421, 1.485, 1.536, 1.579, 1.644, 1.71 , 1.777] # Horozontal multiple graphs of yield curve at three dates fig, ax = plt.subplots(figsize=(10, 4)) ax.plot(mat1,y1_fit, color = 'blue', label='2019-12-31') ax.scatter(mat1, y1, color = 'blue') ax.plot(mat2,y2_fit, color = 'red', label='2020-12-31') ax.scatter(mat2, y2, color = 'red') ax.plot(mat3,y3_fit, color = 'black', label='2021-12-31') ax.scatter(mat3, y3, color = 'black') # add selected maturities for x axis points mat1_axis = [3, 36, 60, 120, 180, 240] mat2_axis = [x+300 for x in mat1_axis] mat3_axis = [x+300 for x in mat2_axis] plt.xticks(np.array(mat1_axis + mat2_axis +mat3_axis), np.array(mat1_axis + mat1_axis + mat1_axis)) plt.xlabel('Maturity (month)') plt.ylabel('Yield (percent)') plt.legend(loc='lower right') plt.show | cs |
As expected, from 2019 to 2021, the level factor of U.S. Treasury yield curve shifted down from 2019 to 2020 and then the curvature factor rose more strongly than the slope factor from 2020 to 2021 before the FOMC's interest rate hikes.
Although 2022 data is not displayed, we already know that the Federal Open Market Committee (FOMC) has raised the federal funds rate by nearly 4 percentage points since March, which is the fastest pace of increases in four decades. As a result, the FOMC's target federal funds rate is currently 4.25% to 4.50%.
No comments:
Post a Comment