导入代码库
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
% matplotlib inline
import warnings
warnings.filterwarnings('ignore')
# 不发出警告
from bokeh.io import output_notebook
output_notebook()
# 导入notebook绘图模块
from bokeh.plotting import figure,show
from bokeh.models import ColumnDataSource
# 导入图表绘制、图标展示模块
# 导入ColumnDataSource模块
筛选数据
隐藏
# 筛选数据 - 隐藏
# legend.click_policy
from bokeh.palettes import Spectral4
# 导入颜色模块
df = pd.DataFrame({'A':np.random.randn(500).cumsum(),
'B':np.random.randn(500).cumsum(),
'C':np.random.randn(500).cumsum(),
'D':np.random.randn(500).cumsum()},
index = pd.date_range('20180101',freq = 'D',periods=500))
# 创建数据
p = figure(plot_width=800, plot_height=400, x_axis_type="datetime")
p.title.text = '点击图例来隐藏数据'
for col,color in zip(df.columns.tolist(),Spectral4):
p.line(df.index,df[col],line_width=2, color=color, alpha=0.8,legend = col)
p.legend.location = "top_left"
p.legend.click_policy="hide"
# 设置图例,点击隐藏
show(p)

消隐
# 筛选数据 - 消隐
# legend.click_policy
from bokeh.palettes import Spectral4
# 导入颜色模块
df = pd.DataFrame({'A':np.random.randn(500).cumsum(),
'B':np.random.randn(500).cumsum(),
'C':np.random.randn(500).cumsum(),
'D':np.random.randn(500).cumsum()},
index = pd.date_range('20180101',freq = 'D',periods=500))
# 创建数据
p = figure(plot_width=800, plot_height=400, x_axis_type="datetime")
p.title.text = '点击图例来隐藏数据'
for col,color in zip(df.columns.tolist(),Spectral4):
p.line(df.index,df[col],line_width=2, color=color, alpha=0.8,legend = col,
muted_color=color, muted_alpha=0.2) # 设置消隐后的显示颜色、透明度
p.legend.location = "top_left"
p.legend.click_policy="mute"
# 设置图例,点击隐藏
show(p)

交互小工具—图表分页
# 交互小工具
# 图表分页
from bokeh.models.widgets import Panel, Tabs
# 导入panel,tabs模块
p1 = figure(plot_width=500, plot_height=300)
p1.circle([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=20, color="navy", alpha=0.5)
tab1 = Panel(child=p1, title="circle")
# child → 页码
# title → 分页名称
p2 = figure(plot_width=500, plot_height=300)
p2.line([1, 2, 3, 4, 5], [4, 2, 3, 8, 6], line_width=3, color="navy", alpha=0.5)
tab2 = Panel(child=p2, title="line")
tabs = Tabs(tabs=[ tab1, tab2 ])
# 设置分页图表
show(tabs)

