代码之家  ›  专栏  ›  技术社区  ›  RobD

未与Show(P)或P.Show()一起显示的Bokeh图

  •  0
  • RobD  · 技术社区  · 6 年前

    尝试用此代码显示Bokeh图表,如果使用 show(p)

    AttributeError: 'Figure' object has no attribute 'show'
    

    我怎么修这个?

    from math import pi
    import pandas as pd
    from bokeh.plotting import figure, show, output_notebook
    from bokeh.models.annotations import Title
    from nsepy import get_history
    from datetime import date
    from datetime import datetime
    from pykalman import KalmanFilter
    
    df  =  get_history(symbol="TCS", start = date(2018,1,1),end = date(2018,7,22))
    print(df)
    kf = KalmanFilter(transition_matrices = [1],
                      observation_matrices = [1],
                      initial_state_mean = df['Close'].values[0],
                      initial_state_covariance = 1,
                      observation_covariance=1,
                      transition_covariance=.01)
    state_means,_ = kf.filter(df[['Close']].values)
    state_means = state_means.flatten()
    
    df["date"] = pd.to_datetime(df.index)
    
    mids = (df.Open + df.Close)/2
    spans = abs(df.Close-df.Open)
    
    inc = df.Close > df.Open
    dec = df.Open > df.Close
    w = 12*60*60*1000 # half day in ms
    
    output_notebook()
    
    TOOLS = "pan,wheel_zoom,box_zoom,reset,save"
    #This causes an exception tol with p.show() no show in figure
    p = figure(x_axis_type="datetime", tools=TOOLS, plot_width=1000, toolbar_location="left",y_axis_label = "Price",
               x_axis_label = "Date")
    p.segment(df.date, df.High, df.date, df.Low, color="black")
    p.rect(df.date[inc], mids[inc], w, spans[inc], fill_color='green', line_color="green")
    p.rect(df.date[dec], mids[dec], w, spans[dec], fill_color='red', line_color="red")
    p.line(df.date,state_means,line_width=1,line_color = 'blue',legend="Kalman filter")
    t = Title()
    t.text = 'Kalman Filter Estimation'
    p.title = t
    p.xaxis.major_label_orientation = pi/4
    p.grid.grid_line_alpha=0.3
    p.show() #Throws attribute error show does not exist
    #show(p) #Nothing happens on this
    
    2 回复  |  直到 5 年前
        1
  •  1
  •   bigreddot    6 年前

    没有 show 在博克赫的地块方法,从来没有。有一个 显示 可以将绘图(或绘图和小部件的布局)传递给的函数。

    from bokeh.io import output_file, show
    from bokeh.plotting import figure
    
    p = figure(...)
    p.circle(...)
    
    output_file("foo.html")
    
    show(p)
    

    这是 Quickstart: Getting Started 在整个文档中的数百个示例中也重复了此模式。

        2
  •  0
  •   Sean Fallon    5 年前
    from math import pi
    import pandas as pd
    from bokeh.plotting import figure, show, output_file
    from bokeh.models.annotations import Title
    from nsepy import get_history
    from datetime import date
    from datetime import datetime
    from pykalman import KalmanFilter
    
    df  =  get_history(symbol="TCS", start = date(2018,1,1),end = date(2018,7,22))
    print(df)
    kf = KalmanFilter(transition_matrices = [1],
                      observation_matrices = [1],
                      initial_state_mean = df['Close'].values[0],
                      initial_state_covariance = 1,
                      observation_covariance=1,
                      transition_covariance=.01)
    state_means,_ = kf.filter(df[['Close']].values)
    state_means = state_means.flatten()
    
    df["date"] = pd.to_datetime(df.index)
    
    mids = (df.Open + df.Close)/2
    spans = abs(df.Close-df.Open)
    
    inc = df.Close > df.Open
    dec = df.Open > df.Close
    w = 12*60*60*1000 # half day in ms
    
    #output_notebook()
    #please note in the import statement above, I have changed it from 
    output_notebook to output_file
    
    output_file=("TCS.html", title = "Kalman Filter Estimation", mode="cdn")
    TOOLS = "pan,wheel_zoom,box_zoom,reset,save"
    #This causes an exception tol with p.show() no show in figure
    p = figure(x_axis_type="datetime", tools=TOOLS, plot_width=1000, 
    toolbar_location="left",y_axis_label = "Price",
               x_axis_label = "Date")
    p.segment(df.date, df.High, df.date, df.Low, color="black")
    p.rect(df.date[inc], mids[inc], w, spans[inc], fill_color='green', 
    line_color="green")
    p.rect(df.date[dec], mids[dec], w, spans[dec], fill_color='red', line_color="red")
    p.line(df.date,state_means,line_width=1,line_color = 'blue',legend="Kalman filter")
    
    #t = Title()
    #t.text = 'Kalman Filter Estimation'
    #p.title = t
    
    p.xaxis.major_label_orientation = pi/4
    p.grid.grid_line_alpha=0.3
    p.show()
    

    这将在Google或Edge中打开HTML文件,或者打开您设置的默认浏览器。