提问人:dharmatech 提问时间:11/3/2023 更新时间:11/4/2023 访问量:45
合并两个图表(美国国债数据和$SPX)
Combine two charts (U.S. Treasuries data and $SPX)
问:
图1
该程序创建了美国财政部发行的票据与(票据+债券)比率的图表。
import requests
import pandas as pd
from bokeh.plotting import figure, show
from bokeh.models import NumeralTickFormatter, HoverTool
# ---------------------------------------------------------------------
page_size = 10000
url = 'https://api.fiscaldata.treasury.gov/services/api/fiscal_service/v1/accounting/od/auctions_query'
params = {
'fields' : 'record_date,issue_date,maturity_date,security_type,total_accepted',
'filter' : 'record_date:gte:1900-01-01',
'page[size]' : page_size
}
response = requests.get(url, params=params)
result_json = response.json()
df = pd.DataFrame(result_json['data'])
# ----------------------------------------------------------------------
df['record_date'] = pd.to_datetime(df['record_date'])
df['issue_date'] = pd.to_datetime(df['issue_date'])
df['maturity_date'] = pd.to_datetime(df['maturity_date'])
df['auction_date'] = pd.to_datetime(df['auction_date'])
df['total_accepted'] = pd.to_numeric(df['total_accepted'], errors='coerce')
df['total_accepted_neg'] = df['total_accepted'] * -1
# ----------------------------------------------------------------------
bills = df[df['security_type'] == 'Bill']
notes = df[df['security_type'] == 'Note']
bonds = df[df['security_type'] == 'Bond']
# ----------------------------------------------------------------------
freq='Q'
# ----------------------------------------------------------------------
bills_issued = bills.groupby(pd.Grouper(key='issue_date', freq=freq))['total_accepted'].sum().to_frame()
notes_issued = notes.groupby(pd.Grouper(key='issue_date', freq=freq))['total_accepted'].sum().to_frame()
bonds_issued = bonds.groupby(pd.Grouper(key='issue_date', freq=freq))['total_accepted'].sum().to_frame()
# ----------------------------------------------------------------------
bills_notes_bonds_issued = bills_issued.merge(notes_issued, how='outer', on='issue_date').merge(bonds_issued, how='outer', on='issue_date')
bills_notes_bonds_issued.columns = ['bills', 'notes', 'bonds']
bills_notes_bonds_issued['bills_notes_ratio'] = bills_notes_bonds_issued['bills'] / bills_notes_bonds_issued['notes']
bills_notes_bonds_issued['bills_notes_bonds_ratio'] = bills_notes_bonds_issued['bills'] / (bills_notes_bonds_issued['notes'] + bills_notes_bonds_issued['bonds'])
# ----------------------------------------------------------------------
p = figure(title=f'Treasury Securities Auctions Data : {freq}', sizing_mode='stretch_both', x_axis_type='datetime', x_axis_label='date', y_axis_label='')
p.add_tools(HoverTool(
tooltips=[
('issue_date', '@issue_date{%F}'),
('total_accepted', '@total_accepted{$0.0a}')
],
formatters={ '@issue_date': 'datetime' }))
p.yaxis.formatter = NumeralTickFormatter(format='0a')
p.line(x='issue_date', y='bills_notes_ratio', color='black', legend_label='Bills/Notes ratio', source=bills_notes_bonds_issued)
p.legend.click_policy = 'hide'
p.legend.location = 'top_left'
show(p)
# ----------------------------------------------------------------------
图2
该计划绘制了标准普尔500指数的图表:
import pandas as pd
from bokeh.plotting import figure, show
from bokeh.models import NumeralTickFormatter, HoverTool
import yfinance as yf
# ---------------------------------------------------------------------
spx = yf.Ticker('^GSPC')
data = spx.history(start='1980-01-01', interval='1d')
# ----------------------------------------------------------------------
p = figure(title=f'SPX', sizing_mode='stretch_both', x_axis_type='datetime', x_axis_label='date', y_axis_label='price')
p.yaxis.formatter = NumeralTickFormatter(format='0a')
p.legend.click_policy = 'hide'
p.legend.location = 'top_left'
p.line(x='Date', y='Close', color='red', legend_label='S&P 500', source=data)
show(p)
问题
将图表 1 和图表 2 中的数据绘制在同一图表上的好方法是什么?
答:
0赞
dharmatech
11/4/2023
#1
这是一种似乎有效的方法。
似乎我需要明确指定框 y 轴的范围。y
# ----------------------------------------------------------------------
import requests
import pandas as pd
from bokeh.plotting import figure, show
from bokeh.models import NumeralTickFormatter, HoverTool
from bokeh.models import LinearAxis, Range1d
import yfinance as yf
# ---------------------------------------------------------------------
page_size = 10000
url = 'https://api.fiscaldata.treasury.gov/services/api/fiscal_service/v1/accounting/od/auctions_query'
params = {
'fields' : 'record_date,issue_date,maturity_date,security_type,total_accepted',
'filter' : 'record_date:gte:1900-01-01',
'page[size]' : page_size
}
response = requests.get(url, params=params)
result_json = response.json()
df = pd.DataFrame(result_json['data'])
# ----------------------------------------------------------------------
df['record_date'] = pd.to_datetime(df['record_date'])
df['issue_date'] = pd.to_datetime(df['issue_date'])
df['maturity_date'] = pd.to_datetime(df['maturity_date'])
df['auction_date'] = pd.to_datetime(df['auction_date'])
df['total_accepted'] = pd.to_numeric(df['total_accepted'], errors='coerce')
df['total_accepted_neg'] = df['total_accepted'] * -1
# ----------------------------------------------------------------------
bills = df[df['security_type'] == 'Bill']
notes = df[df['security_type'] == 'Note']
bonds = df[df['security_type'] == 'Bond']
# ----------------------------------------------------------------------
freq='Q'
# ----------------------------------------------------------------------
bills_issued = bills.groupby(pd.Grouper(key='issue_date', freq=freq))['total_accepted'].sum().to_frame()
notes_issued = notes.groupby(pd.Grouper(key='issue_date', freq=freq))['total_accepted'].sum().to_frame()
bonds_issued = bonds.groupby(pd.Grouper(key='issue_date', freq=freq))['total_accepted'].sum().to_frame()
# ----------------------------------------------------------------------
bills_notes_bonds_issued = bills_issued.merge(notes_issued, how='outer', on='issue_date').merge(bonds_issued, how='outer', on='issue_date')
bills_notes_bonds_issued.columns = ['bills', 'notes', 'bonds']
bills_notes_bonds_issued['bills_notes_ratio'] = bills_notes_bonds_issued['bills'] / bills_notes_bonds_issued['notes']
bills_notes_bonds_issued['bills_notes_bonds_ratio'] = bills_notes_bonds_issued['bills'] / (bills_notes_bonds_issued['notes'] + bills_notes_bonds_issued['bonds'])
# ----------------------------------------------------------------------
spx = yf.Ticker('^GSPC')
data = spx.history(start='1980-01-01', interval='1d')
# ----------------------------------------------------------------------
p = figure(title=f'Treasury Securities Auctions Data : {freq}', sizing_mode='stretch_both', x_axis_type='datetime', x_axis_label='date', y_axis_label='', y_range=(0, 10))
p.line(x='issue_date', y='bills_notes_ratio', color='black', legend_label='Bills/Notes ratio', source=bills_notes_bonds_issued)
p.extra_y_ranges = {"spx": Range1d(start=min(data['Close']), end=max(data['Close']))}
p.add_layout(LinearAxis(y_range_name="spx", axis_label='SPX'), 'right')
p.line(x='Date', y='Close', color='blue', legend_label='SPX', source=data, y_range_name="spx")
p.legend.click_policy = 'hide'
p.legend.location = 'top_left'
show(p)
评论