散景条形图的空图
Empty plot by Bokeh Bar-chart
我有一个包含自变量(国家/地区)的数据框,我将有一个交互式面板,以便能够根据国家/地区在条形图中 select。条形图将显示 selected 国家/地区每个行业的数量。但是我的代码显示了 selection 面板,它只显示了一个空图!!!并且改变 selection 没有任何区别。
您可以在这里找到代码:
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_range
:x_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)
我有一个包含自变量(国家/地区)的数据框,我将有一个交互式面板,以便能够根据国家/地区在条形图中 select。条形图将显示 selected 国家/地区每个行业的数量。但是我的代码显示了 selection 面板,它只显示了一个空图!!!并且改变 selection 没有任何区别。
您可以在这里找到代码:
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_range
:x_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)