-
Notifications
You must be signed in to change notification settings - Fork 0
/
interest_rate.py
121 lines (101 loc) · 4.23 KB
/
interest_rate.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# -*- coding: utf-8 -*-
"""
@Project Name macro_economic
@File Name: interest_rate
@Software: PyCharm
@Time: 2018/6/5 22:27
@Author: taosheng
@contact: [email protected]
@version: 1.0
@Description:
"""
import datetime
import numpy as np
import pandas as pd
import tushare as ts
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, NullFormatter, FormatStrFormatter
from matplotlib.dates import YEARLY, DateFormatter, rrulewrapper, DateFormatter, drange
from matplotlib.dates import RRuleLocator, YearLocator, MonthLocator, WeekdayLocator, DayLocator
np.set_printoptions(threshold=2000, linewidth=1000) # default 1000 default 75
pd.set_option('display.width', 1000) # default is 80
def plot_curve(dates, y, title=None):
"""
绘制曲线
:param dates:
:param y:
:return:
"""
fig, ax = plt.subplots()
# ax.plot(y, 'o-')
# ax.plot(range(len(y)), y, 'o-')
ax.plot_date(dates, y, marker="*", linewidth=1, linestyle="-", color="blue")
plt.title(title, fontproperties='SimHei', fontsize=12)
plt.xlabel("time")
plt.ylabel("rate")
### 设置X轴 ###
## 设置x轴范围
# datemin = datetime.date(dates.min().year - 5, 1, 1)
# datemax = datetime.date(dates.max().year + 5, 1, 1)
# ax.set_xlim(datemin, datemax)
## 设置x轴tick和ticklabel
# plt.xticks(np.arange(0, 50, 10), ('Tom', 'Dick', 'Harry', 'Sally', 'Sue'), rotation=90)
# ax.set_xticks([])
# ax.set_xticks(list(y.index))
# ax.set_xticklabels(dates)
# ax.set_xticklabels(["start","medium","end"])
# ax.xaxis.set_major_formatter(NullFormatter()) # 设置x轴标签文本的格式
# ax.xaxis.set_major_formatter(mdate.DateFormatter('%Y-%m-%d')) # 设置x轴时间标签显示格式
# 设置主刻度标签的位置,有标签文本的格式
ax.xaxis.set_major_locator(YearLocator(base=1, month=1, day=1, tz=None)) # tick every year on Feb 1st
ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d')) # 设置x轴标签文本的格式
# rule = rrulewrapper(YEARLY, byeaster=1, interval=5) # tick every 5th easter
# ax.xaxis.set_major_locator(RRuleLocator(rule))
# ax.xaxis.set_major_formatter(DateFormatter('%m/%d/%y')) # 设置x轴标签文本的格式
# 设置次刻度标签的位置,没有标签文本格式
ax.xaxis.set_minor_locator(YearLocator()) # 将x轴次刻度标签设置为年
### 设置Y轴 ###
ax.yaxis.set_major_locator(MultipleLocator(1)) # 将y轴主刻度标签设置为2的倍数
ax.yaxis.set_major_formatter(FormatStrFormatter('%1.1f')) # 设置y轴标签文本的格式
ax.yaxis.set_minor_locator(MultipleLocator(0.25)) # 将此y轴次刻度标签设置为0.1的倍数
# 打开网格
# ax.xaxis.grid(True, which='major') # x坐标轴的网格使用主刻度
# ax.yaxis.grid(True, which='major') # y坐标轴的网格使用次刻度
ax.grid(True)
fig.autofmt_xdate()
plt.show()
if __name__ == "__main__":
deposit_rate = ts.get_deposit_rate()
"""
date :变动日期
deposit_type :存款种类
rate:利率(%)
"""
print(deposit_rate.deposit_type.unique())
## 分组 ##
gb = deposit_rate.groupby(['deposit_type'])
# for name, group in gb:
# print(name)
# print(group)
# print(gb.groups.keys())
# name = '活期存款(不定期)'
# name = '定期存款整存整取(三个月)'
# name = '定期存款整存整取(半年)'
name = '定期存款整存整取(一年)'
# name = '定期存款整存整取(二年)'
# name = '定期存款整存整取(三年)'
# name = '定期存款整存整取(五年)'
data = gb.get_group(name)
print(data)
# print(data.shape)
# print(type(data))
# print(type(data["rate"]))
# print(data["rate"].dtype)
# data = data.reindex(data.index[::-1])
## 缺失值处理 ##
data = data.replace("--", np.nan)
print(data)
dates = data["date"].apply(lambda s: datetime.datetime.strptime(s, '%Y-%m-%d'))
y = data["rate"].astype(np.float64)
# y = list(y)
plot_curve(dates, y, title=name)