我试图绘制两个数据集(HIBOR和US_yield)。我不知道为什么HIBOR的值在绘图中是错误的。但是,当我单独绘制它时,值是正确的。
import requests
import pandas as pd
import xmltodict
from datetime import datetime
import json
import matplotlib.pyplot as plt
year=2023
us_url=f'https://home.treasury.gov/resource-center/data-chart-center/interest-rates/pages/xml?data=daily_treasury_yield_curve&field_tdr_date_value={year}'
us_data=requests.get(us_url).content
hk_url=f'https://api.hkma.gov.hk/public/market-data-and-statistics/daily-monetary-statistics/daily-figures-interbank-liquidity'
hk_data=requests.get(hk_url).content
# Parase us data
dict_data_us=xmltodict.parse(us_data)
dict_us=dict()
for key in dict_data_us['feed']['entry'][0]['content']['m:properties'].keys():
dict_us[key.replace('d:','')]=[i['content']['m:properties'][key]['#text'] for i in dict_data_us['feed']['entry']]
df_us=pd.DataFrame(dict_us)
df_us['Date']=[datetime.strptime(i, '%Y-%m-%dT%H:%M:%S') for i in df_us['NEW_DATE']]
df_us.set_index('Date', inplace=True)
# Parase hk data hibor
dict_data_hk=json.loads(hk_data)
dict_hk=dict()
for key in dict_data_hk['result']['records'][0].keys():
dict_hk[key]=[i[key] for i in dict_data_hk['result']['records']]
df_hk=pd.DataFrame(dict_hk)
df_hk['Date']=[datetime.strptime(i, '%Y-%m-%d') for i in df_hk['end_of_date']]
df_hk.set_index('Date', inplace=True)
df_hk.sort_index(inplace=True)
plt.plot(df_hk['hibor_fixing_1m'][-40:], label='HIBOR_1M')
plt.plot(df_us['BC_1MONTH'][-40:], label='US_Yield_1M')
plt.legend()
plt.show()
1条答案
按热度按时间yacmzcpb1#
原因是y轴字段在绘图中读取为字符串。您需要按如下所示向它们添加
astype(float)
...这将给予你下面的情节。希望这是你正在寻找的