-重点讲分分页
① 筛选数据 - 显示/隐藏
② 交互小工具,参考文档:http://bokeh.pydata.org/en/latest/docs/user_guide/interaction/widgets.html#pretext
1.导入相关模块
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模块
2.筛选数据 - 隐藏
# 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 = '点击图例来隐藏数据'
print( df.columns.tolist())
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)
3.筛选数据 - 消隐(隐藏的颜色也可以设置)
# 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)
4.交互小工具-图表分页
rom 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)