簡體   English   中英

散景條形圖的空 plot

[英]Empty plot by Bokeh Bar-chart

我有一個 dataframe 包括一個自變量(國家),我將有一個交互式面板能夠在條形圖中 select,根據國家。 條形圖將顯示所選國家/地區每個行業的數量。 但是我的代碼顯示了選擇面板,它只顯示了一個空的 plot.!! 並且更改選擇沒有任何區別。

你可以在這里找到代碼:

import pandas as pd
from bokeh.plotting import figure
from bokeh.layouts import layout, widgetbox
from bokeh.models import ColumnDataSource, HoverTool, BoxZoomTool, ResetTool, PanTool
from bokeh.models.widgets import Select
from bokeh.models import WheelZoomTool, SaveTool, LassoSelectTool
from bokeh.io import show, output_notebook
output_notebook()

data = pd.DataFrame({'id':['001', '002', '003', '004','005', '006', '007', '008','009', '010', '011', '012','013', '014', '015', '016'],
        'country_code':['A', 'A', 'B', 'B','C', 'A', 'C', 'B','A', 'C', 'C', 'A','A', 'A', 'C', 'B'],
            'Industry':['M1', 'M2', 'M1', 'M3','M2', 'M3', 'M3', 'M3','M1', 'M2', 'M1', 'M1','M2', 'M1', 'M1', 'M3'],
           'Zone':['x', 'x', 'x', 't','y', 'z', 'z', 'z','t', 't', 'y', 'y','z', 't', 'z', 'x'],
           'count':[100,200,150,120,200,200,180,100,100,200,150,120,200,200,180,100]})


#Creating the list of country codes
Options = ["All"] + list(data['country_code'].unique())
Countries = Select(title = " Country Code", options = Options, value = "All")


TOOLS = [BoxZoomTool(), LassoSelectTool(), WheelZoomTool(), PanTool(), ResetTool(), SaveTool()]



def select():
    """ Use the current selections to determine which filters to apply to the
    data. Return a dataframe of the selected data
    """
    df = data

    # Determine which country has been selected
    country_val = Countries.value

    if country_val == "All":
        selected = pd.DataFrame(df.groupby(['Industry']).size().reset_index())
        selected.columns = ['Industry','count']
    else:
        selected = df[df['country_code'] == country_val]

    return selected

def update():
    "Get the selected data and update the data in the source"
    df_active = select()
    source = ColumnDataSource(df_active)
    return source

p = figure( plot_height = 300, title = "Inustry_code", tools = TOOLS)

p.vbar(x = 'Industry', top = 'count', width = .9, fill_alpha = .5, fill_color = 'red', line_alpha = .5, source = update())


show(Countries)
show(p)

您需要像這樣指定x_rangex_range = source.data['Industry'] 所以你的代碼可能是:

source = update()

p = figure( plot_height = 300, title = "Inustry_code", tools = TOOLS, x_range = source.data['Industry'])

p.vbar(x = 'Industry', top = 'count', width = .9, fill_alpha = .5, fill_color = 'red', line_alpha = .5, source = source)

show(Countries)
show(p)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM