【发布时间】:2016-12-31 08:13:42
【问题描述】:
我想绘制基于下拉小部件值的条形图。
代码
import pandas as pd
from bokeh.io import output_file, show
from bokeh.layouts import widgetbox
from bokeh.models.widgets import Dropdown
from bokeh.plotting import curdoc
from bokeh.charts import Bar, output_file,output_server, show #use output_notebook to visualize it in notebook
df=pd.DataFrame({'item':["item1","item2","item2","item1","item1","item2"],'value':[4,8,3,5,7,2]})
menu = [("item1", "item1"), ("item2", "item2")]
dropdown = Dropdown(label="Dropdown button", button_type="warning", menu=menu)
def function_to_call(attr, old, new):
df=df[df['item']==dropdown.value]
p = Bar(df, title="Bar Chart Example", xlabel='x', ylabel='values', width=400, height=400)
output_server()
show(p)
dropdown.on_change('value', function_to_call)
curdoc().add_root(dropdown)
问题
- 我收到流动错误“UnboundLocalError: local variable 'df' referenced before assignment”,尽管 df 已经创建。
- 如何在下拉菜单下方的网页中绘制条形图?解决 1. 中的问题后显示它的语法是什么?
【问题讨论】:
标签: python matplotlib plot data-visualization bokeh