[英]Python Bokeh Range Does Not Change Upon Update
我正在使用bokeh生成股票數據,從而用戶選擇一個股票名稱和開始/結束日期以生成圖形。 但是,選擇某些股票時,y軸會變形(請參閱我的上一個問題: 輸入某些股票時,散景圖會變形 )。
我試圖通過手動選擇將y范圍設置為股票的最小值和最大值來緩解這種情況,但是,新范圍不會更新,並且y軸不會更改。 如何解決此問題? 任何建議,將不勝感激。
p=figure(
height=400,
x_axis_type='datetime',
title=(company+' ('+tickerstring+') '),
tools='pan, box_zoom, wheel_zoom, reset',
)
x = np.array(sandpdates, dtype=np.datetime64)
y=sandpclose
r=p.line(x, y)
p.grid.grid_line_color="white"
p.xaxis.axis_label = 'Date'
p.yaxis.axis_label = 'Price'
p.add_tools(HoverTool(
tooltips=[
("Date", "@x{%F}"),
('Close',"@y")
],
formatters={
'x':'datetime', # use 'datetime' formatter for 'date' field
},
mode='vline'
))
def update(f, startyear=2015, startmonth=1, startday=1, endyear=current_year, endmonth=current_month, endday=current_day):
fstocksymbol=str(f.upper())
starts=dt.datetime(startyear,startmonth,startday)
end = dt.datetime(endyear,endmonth,endday)
if int(startyear)> int(endyear):
print('Please ensure the starting date does not exceed the end date')
elif int(startyear)==int(endyear):
if startmonth>endmonth:
print('Please ensure the starting date does not exceed the end date')
elif startmonth==endmonth:
if startday>endday:
print('Please ensure the starting date does not exceed the end date')
else:
print('')
if fstocksymbol in stocksymbols:
p.title.text = (symbolsdictionary[fstocksymbol]).upper()+' ('+fstocksymbol+')'
tickerstring=fstocksymbol
firstfunction=stockname(tickerstring, starts, end)
secondfunction=stockdata(firstfunction)
stockdates=[]
stockcloseprices=[]
for value in secondfunction:
stockdates.append(value[0])
stockcloseprices.append(float(value[4]))
finaldate=np.array(stockdates, dtype=np.datetime64)
p.y_range = Range1d(min(stockcloseprices), max(stockcloseprices))
r.data_source.data['x'] = finaldate
r.data_source.data['y'] = stockcloseprices
push_notebook()
elif fstocksymbol=='':
print('')
else:
print("")
interact(update, f=stocksymbols, startyear=list(range(int(current_year-5),int(current_year+1))), startmonth=list(range(1,13)), startday=list(range(1,32)),
endyear=list(range(int(current_year-5),int(current_year+1))), endmonth=list(range(1,13)), endday=list(range(1,32)))
grid = gridplot([p, b], ncols=2, plot_width=570, plot_height=400)
show(grid, notebook_handle=True)
而不是直接使用新的Range1D分配給范圍的開始和結束屬性:
p.y_range.start = min(stockcloseprices)
p.y_range.end = max(stockcloseprices)
還嘗試為數據分配一個新的ColumnDataSource(),而不是按照以下方式直接向其中分配一些內容:
r.data_source.data = dict(x=finaldate, y=stockcloseprices)
我相信Bokeh僅在包含開始,結束和數據的圖的某些屬性上檢測事件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.