Python : Graph with segmented colors and horizontal multiple graphs

This post shows how to plot a graph with segmented colors and then uses this to draw horizontal multiple graphs.


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
= np.arange(0.02.00.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 = [369122436486072
        8496108120144180240]
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.5231.5711.5951.6061.5861.6081.6591.699
      1.7681.8181.8641.8811.9391.9992.1072.345]
y2 = [0.0730.09 , 0.0970.1030.1260.1680.2630.374
      0.5110.6480.7730.8470.9251.0471.2231.505]
y3 = [0.0970.1870.2930.3990.7310.9691.1651.272
      1.3631.4321.4321.4641.4881.5511.6761.98 ]
 
# Fitted yields at 2019-12-31, 2020-12-31, 2021-12-31
y1_fit = [1.5791.5731.57 , 1.5691.58 , 1.61 , 1.6511.7  , 
          1.7521.8041.8561.9061.9542.04 , 2.1472.277]
y2_fit = [0.1160.0930.0790.0710.0940.1730.2810.399
          0.5190.6350.7420.8410.9311.0851.26 , 1.453]
y3_fit = [0.0960.2060.3070.4010.71 , 0.9381.1091.24 , 
          1.3411.4211.4851.5361.5791.6441.71 , 1.777]
 
# Horozontal multiple graphs of yield curve at three dates
fig, ax = plt.subplots(figsize=(104))
 
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 = [33660120180240]
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