Created
May 10, 2023 18:59
-
-
Save namuan/05f9f4a2016b42e165f5bd4bf0aff7eb to your computer and use it in GitHub Desktop.
Plot interest rate data from Bank of England
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import datetime | |
import matplotlib.pyplot as plt | |
def parse_data_string(data_string): | |
pattern = r"(\d{1,2})\s([a-zA-Z]{3})\s(\d{2})\s(\d+\.\d+)" | |
match = re.match(pattern, data_string) | |
if match: | |
day = int(match.group(1)) | |
month_str = match.group(2) | |
year = int(match.group(3)) | |
rate = float(match.group(4)) | |
# Handle years with only two digits | |
if year >= 0 and year <= 24: | |
year += 2000 | |
else: | |
year += 1900 | |
date = datetime.datetime.strptime(f"{day} {month_str} {year}", "%d %b %Y") | |
return date, rate | |
return None | |
data = ''' | |
23 Mar 23 4.25 | |
02 Feb 23 4.00 | |
15 Dec 22 3.50 | |
03 Nov 22 3.00 | |
22 Sep 22 2.25 | |
04 Aug 22 1.75 | |
16 Jun 22 1.25 | |
05 May 22 1.00 | |
17 Mar 22 0.75 | |
03 Feb 22 0.50 | |
16 Dec 21 0.25 | |
19 Mar 20 0.10 | |
11 Mar 20 0.25 | |
02 Aug 18 0.75 | |
02 Nov 17 0.50 | |
04 Aug 16 0.25 | |
05 Mar 09 0.50 | |
05 Feb 09 1.00 | |
08 Jan 09 1.50 | |
04 Dec 08 2.00 | |
06 Nov 08 3.00 | |
08 Oct 08 4.50 | |
10 Apr 08 5.00 | |
07 Feb 08 5.25 | |
06 Dec 07 5.50 | |
05 Jul 07 5.75 | |
10 May 07 5.50 | |
11 Jan 07 5.25 | |
09 Nov 06 5.00 | |
03 Aug 06 4.75 | |
04 Aug 05 4.50 | |
05 Aug 04 4.75 | |
10 Jun 04 4.50 | |
06 May 04 4.25 | |
05 Feb 04 4.00 | |
06 Nov 03 3.75 | |
10 Jul 03 3.50 | |
06 Feb 03 3.75 | |
08 Nov 01 4.00 | |
04 Oct 01 4.50 | |
18 Sep 01 4.75 | |
02 Aug 01 5.00 | |
10 May 01 5.25 | |
05 Apr 01 5.50 | |
08 Feb 01 5.75 | |
10 Feb 00 6.00 | |
13 Jan 00 5.75 | |
04 Nov 99 5.50 | |
08 Sep 99 5.25 | |
10 Jun 99 5.00 | |
08 Apr 99 5.25 | |
04 Feb 99 5.50 | |
07 Jan 99 6.00 | |
10 Dec 98 6.25 | |
05 Nov 98 6.75 | |
08 Oct 98 7.25 | |
04 Jun 98 7.50 | |
06 Nov 97 7.25 | |
07 Aug 97 7.00 | |
10 Jul 97 6.75 | |
06 Jun 97 6.50 | |
06 May 97 6.25 | |
30 Oct 96 5.94 | |
06 Jun 96 5.69 | |
08 Mar 96 5.94 | |
18 Jan 96 6.13 | |
13 Dec 95 6.38 | |
02 Feb 95 6.63 | |
07 Dec 94 6.13 | |
12 Sep 94 5.63 | |
08 Feb 94 5.13 | |
23 Nov 93 5.38 | |
26 Jan 93 5.88 | |
13 Nov 92 6.88 | |
16 Oct 92 7.88 | |
22 Sep 92 8.88 | |
05 May 92 9.88 | |
04 Sep 91 10.38 | |
12 Jul 91 10.88 | |
24 May 91 11.38 | |
12 Apr 91 11.88 | |
22 Mar 91 12.38 | |
27 Feb 91 12.88 | |
13 Feb 91 13.38 | |
08 Oct 90 13.88 | |
06 Oct 89 14.88 | |
08 Sep 89 13.75 | |
04 Sep 89 13.88 | |
31 Aug 89 13.84 | |
25 May 89 13.75 | |
25 Nov 88 12.88 | |
25 Aug 88 11.88 | |
08 Aug 88 10.88 | |
21 Jul 88 10.38 | |
07 Jul 88 9.88 | |
24 Jun 88 8.88 | |
10 Jun 88 8.38 | |
03 Jun 88 7.88 | |
17 May 88 7.38 | |
08 Apr 88 7.88 | |
17 Mar 88 8.38 | |
01 Feb 88 8.88 | |
03 Dec 87 8.38 | |
04 Nov 87 8.88 | |
23 Oct 87 9.38 | |
06 Aug 87 9.88 | |
08 May 87 8.88 | |
28 Apr 87 9.38 | |
18 Mar 87 9.88 | |
09 Mar 87 10.38 | |
15 Oct 86 10.88 | |
23 May 86 9.88 | |
18 Apr 86 10.38 | |
11 Apr 86 10.88 | |
19 Mar 86 11.38 | |
15 Jan 86 12.38 | |
26 Jul 85 11.38 | |
11 Jul 85 11.88 | |
19 Apr 85 12.38 | |
28 Mar 85 12.88 | |
20 Mar 85 13.38 | |
28 Jan 85 13.88 | |
14 Jan 85 11.88 | |
23 Nov 84 9.50 | |
19 Nov 84 9.75 | |
05 Nov 84 10.00 | |
17 Aug 84 10.50 | |
16 Aug 84 10.75 | |
09 Aug 84 11.00 | |
08 Aug 84 11.50 | |
11 Jul 84 12.00 | |
06 Jul 84 10.00 | |
29 Jun 84 8.88 | |
10 May 84 9.06 | |
14 Mar 84 8.56 | |
07 Mar 84 8.81 | |
03 Oct 83 9.06 | |
10 Aug 83 9.56 | |
09 Aug 83 9.44 | |
14 Jun 83 9.56 | |
13 Jun 83 9.81 | |
14 Apr 83 10.06 | |
13 Apr 83 10.31 | |
15 Mar 83 10.56 | |
12 Jan 83 11.00 | |
26 Nov 82 10.00 | |
02 Nov 82 9.13 | |
01 Nov 82 9.38 | |
12 Oct 82 9.63 | |
30 Sep 82 10.13 | |
29 Sep 82 10.25 | |
28 Sep 82 10.38 | |
27 Sep 82 10.50 | |
27 Aug 82 10.63 | |
26 Aug 82 10.88 | |
25 Aug 82 11.00 | |
24 Aug 82 11.13 | |
17 Aug 82 11.25 | |
16 Aug 82 11.38 | |
04 Aug 82 11.50 | |
02 Aug 82 11.56 | |
30 Jul 82 11.63 | |
29 Jul 82 11.75 | |
28 Jul 82 11.81 | |
26 Jul 82 11.94 | |
21 Jul 82 12.06 | |
13 Jul 82 12.13 | |
12 Jul 82 12.25 | |
09 Jul 82 12.50 | |
08 Jun 82 12.63 | |
20 Apr 82 13.13 | |
19 Apr 82 13.00 | |
16 Apr 82 13.13 | |
10 Mar 82 13.25 | |
25 Feb 82 13.63 | |
22 Feb 82 13.81 | |
22 Jan 82 13.88 | |
21 Jan 82 14.00 | |
20 Jan 82 14.13 | |
19 Jan 82 14.25 | |
18 Jan 82 14.31 | |
04 Dec 81 14.38 | |
25 Nov 81 14.56 | |
09 Nov 81 14.63 | |
06 Nov 81 15.06 | |
28 Oct 81 15.13 | |
12 Oct 81 15.00 | |
15 Sep 81 14.00 | |
25 Aug 81 12.69 | |
11 Mar 81 12.00 | |
25 Nov 80 14.00 | |
03 Jul 80 16.00 | |
15 Nov 79 17.00 | |
13 Jun 79 14.00 | |
05 Apr 79 12.00 | |
01 Mar 79 13.00 | |
08 Feb 79 14.00 | |
09 Nov 78 12.50 | |
08 Jun 78 10.00 | |
15 May 78 9.00 | |
08 May 78 8.75 | |
12 Apr 78 7.50 | |
09 Jan 78 6.50 | |
28 Nov 77 7.00 | |
17 Oct 77 5.00 | |
10 Oct 77 5.50 | |
19 Sep 77 6.00 | |
12 Sep 77 6.50 | |
15 Aug 77 7.00 | |
08 Aug 77 7.50 | |
16 May 77 8.00 | |
02 May 77 8.25 | |
25 Apr 77 8.75 | |
18 Apr 77 9.00 | |
12 Apr 77 9.25 | |
31 Mar 77 9.50 | |
21 Mar 77 10.50 | |
10 Mar 77 11.00 | |
03 Feb 77 12.00 | |
31 Jan 77 12.25 | |
24 Jan 77 13.25 | |
10 Jan 77 14.00 | |
29 Dec 76 14.25 | |
20 Dec 76 14.50 | |
22 Nov 76 14.75 | |
07 Oct 76 15.00 | |
13 Sep 76 13.00 | |
24 May 76 11.50 | |
26 Apr 76 10.50 | |
08 Mar 76 9.00 | |
01 Mar 76 9.25 | |
09 Feb 76 9.50 | |
02 Feb 76 10.00 | |
26 Jan 76 10.50 | |
19 Jan 76 10.75 | |
05 Jan 76 11.00 | |
29 Dec 75 11.25 | |
01 Dec 75 11.50 | |
17 Nov 75 11.75 | |
06 Oct 75 12.00 | |
28 Jul 75 11.00 | |
05 May 75 10.00 | |
21 Apr 75 9.75 | |
24 Mar 75 10.00 | |
10 Mar 75 10.25 | |
17 Feb 75 10.50 | |
10 Feb 75 10.75 | |
27 Jan 75 11.00 | |
20 Jan 75 11.25 | |
'''.strip().split('\n') | |
dates = [] | |
rates = [] | |
for line in data: | |
date, rate = parse_data_string(line) | |
dates.append(date) | |
rates.append(rate) | |
start_date = datetime.datetime(1975, 1, 1) | |
dates_filtered = [date for date in dates if date >= start_date] | |
rates_filtered = [rate for date, rate in zip(dates, rates) if date >= start_date] | |
with plt.xkcd(): | |
plt.plot(dates_filtered, rates_filtered) | |
plt.xlabel('Date') | |
plt.ylabel('Bank Rate') | |
plt.title('Bank Rate History (1970 onwards)') | |
plt.xticks(rotation=45) | |
plt.grid() | |
plt.tight_layout() | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment