Matplotlib 中选择两个数据集时 y 轴数值错误

huangapple go评论69阅读模式
英文:

Wrong y axis value when two dataset are selected in Matplotlib

问题

我试图绘制两个数据集(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

# 解析美国数据
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)

# 解析香港数据 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()
英文:

I am trying to plot two datasets (HIBOR and US_yield). I don't know why the values of HIBOR in the plot are wrong. However, the values are correct when I plot it alone.

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()

Matplotlib 中选择两个数据集时 y 轴数值错误

Matplotlib 中选择两个数据集时 y 轴数值错误

答案1

得分: 1

原因是在你的图中,y轴字段被读取为字符串。你需要在它们上面添加astype(float),如下所示...

plt.plot(df_us['BC_1MONTH'][-40:].astype(float), label='US_Yield_1M')
plt.plot(df_hk['hibor_fixing_1m'][-40:].astype(float), label='HIBOR_1M')

这将给你下面的图。希望这是你要找的。

英文:

The reason is that the y-axis fields are read as string in your plot. You need to add astype(float) to them as below...

plt.plot(df_us['BC_1MONTH'][-40:].astype(float), label='US_Yield_1M')
plt.plot(df_hk['hibor_fixing_1m'][-40:].astype(float), label='HIBOR_1M')

This will give you the below plot. Hope this is what you are looking for

Matplotlib 中选择两个数据集时 y 轴数值错误

huangapple
  • 本文由 发表于 2023年2月16日 12:56:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75468006.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定