为什么不用新数据更新散景图?

问题描述 投票:1回答:2

我正在创建一个散景应用程序,它从Quandl股票价格中提取数据,并根据用户输入的股票代码更改图表。我用这个bokeh tuorial的例子作为模型。

一切正常,但输入新符号时情节不会更新。

我已经尝试将新数据作为字典传递(在我将数据帧传递给ColumnDataSource()之前,但没有运气。

import pandas as pd
import numpy as np
from bokeh.models.widgets import TextInput, Select
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.layouts import column, row
from bokeh.io import show, output_notebook
import quandl

这是获取数据的功能:

def get_data(symbol):
    dictionary = {}
    data = quandl.get('WIKI/' + symbol, collapse = 'annual', returns='numpy')
    df = pd.DataFrame(data)
    dictionary['date'] = list(df.Date.values)
    dictionary['high'] = list(df.High.values)
    return dictionary

这是情节的一个功能:

def modify_doc(doc):
    symbol = 'AAWW'
    source = ColumnDataSource(data = get_data(symbol))

    p = figure(x_axis_type='datetime', title='Stock Price', plot_height=350, plot_width=800)
    p.xgrid.grid_line_color=None
    p.ygrid.grid_line_alpha=0.5
    p.xaxis.axis_label = 'year'
    p.yaxis.axis_label = 'close'

    r = p.line(source.data['date'], 
               source.data['high'], 
               line_color = 'navy')

    select = Select(title="Color", value="navy", options=COLORS)
    input = TextInput(title="Ticker Symbol", value=symbol)

    def update_symbol(attrname, old, new):
        source.data = get_data(input.value)
    input.on_change('value', update_symbol)

    layout = column(row(input, width=400), row(p))

    doc.add_root(layout)

show(modify_doc)

我认为当输入新符号时,情节会更新,但它会保持不变。

有什么想法吗?

python-3.x bokeh
2个回答
1
投票

您的代码看起来像Bokeh服务器应用程序,但您使用show()对我来说不太好看。您还尝试通过将新数据分配给源来更新图形,但是您没有将源传递给图形对象,因此它不会产生任何影响。如果此代码适合您,您能尝试一下吗? (应该适用于Bokeh v1.0.4)

import random
import pandas as pd
from tornado.ioloop import IOLoop
from bokeh.server.server import Server
from bokeh.application import Application
from bokeh.application.handlers.function import FunctionHandler
from bokeh.plotting import figure, ColumnDataSource
from bokeh.models.widgets import TextInput
from bokeh.layouts import column, row

def make_document(doc):
    symbol = 'AAWW'

    def get_data(symbol):
        dictionary = {}
        data = quandl.get('WIKI/' + symbol, collapse = 'annual', returns = 'numpy')
        df = pd.DataFrame(data)
        dictionary['date'] = list(df.Date.values)
        dictionary['high'] = list(df.High.values)
        return dictionary

    source = ColumnDataSource(data = get_data(symbol))

    p = figure(x_axis_type = 'datetime', title = 'Stock Price', plot_height = 350, plot_width = 800)
    p.xgrid.grid_line_color = None
    p.ygrid.grid_line_alpha = 0.5
    p.xaxis.axis_label = 'year'
    p.yaxis.axis_label = 'close'

    r = p.line(x = 'date',
               y = 'high',
               source = source,
               line_color = 'navy')

    input = TextInput(title = "Ticker Symbol", value = symbol)

    def update_symbol(attrname, old, new):
        source.data = get_data(input.value)

    input.on_change('value', update_symbol)

    layout = column(row(input, width = 400), row(p))
    doc.add_root(layout)

io_loop = IOLoop.current()
server = Server({'/myapp': Application(FunctionHandler(make_document))}, port = 5001, io_loop = io_loop)
server.start()
server.show('/myapp')
io_loop.start()

基本上主要的变化是:

r = p.line(x = 'date',
           y = 'high',
           source = source,
           line_color = 'navy')

0
投票

基于我从Tony得到的答案,我只需更改一行代码:

r = p.line(x = 'date',
           y = 'high',
           source = source,
           line_color = 'navy')
© www.soinside.com 2019 - 2024. All rights reserved.