简体   繁体   English

Python散景范围在更新时不会更改

[英]Python Bokeh Range Does Not Change Upon Update

I'm working with bokeh to generate stock data, whereby the user selects a tickername and start/end dates to generate a graph. 我正在使用bokeh生成股票数据,从而用户选择一个股票名称和开始/结束日期以生成图形。 However, when selecting certain stocks, the y axis becomes distorted (See my previous question: Bokeh plot becomes distorted when certain stocks are inputted ). 但是,选择某些股票时,y轴会变形(请参阅我的上一个问题: 输入某些股票时,散景图会变形 )。

I've tried to mitigate this by manually setting the y range as the min and max values of a stock upon selection, however, the new range does not update and the y axis does not change. 我试图通过手动选择将y范围设置为股票的最小值和最大值来缓解这种情况,但是,新范围不会更新,并且y轴不会更改。 How can I fix this issue? 如何解决此问题? Any suggestions would be appreciated. 任何建议,将不胜感激。

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)

Rather than using a new Range1D assign to the start and end properties of the range directly: 而不是直接使用新的Range1D分配给范围的开始和结束属性:

p.y_range.start = min(stockcloseprices)
p.y_range.end = max(stockcloseprices)

Also try assigning a new ColumnDataSource() to the data rather than assigning directly into it something along the lines of: 还尝试为数据分配一个新的ColumnDataSource(),而不是按照以下方式直接向其中分配一些内容:

r.data_source.data = dict(x=finaldate, y=stockcloseprices)

I believe Bokeh detects events only on certain properties of the plot of which start, end and data are included. 我相信Bokeh仅在包含开始,结束和数据的图的某些属性上检测事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM