导入代码库

  1. import numpy as np
  2. import pandas as pd
  3. import matplotlib.pyplot as plt
  4. % matplotlib inline
  5. import warnings
  6. warnings.filterwarnings('ignore')
  7. # 不发出警告
  8. from bokeh.io import output_notebook
  9. output_notebook()
  10. # 导入notebook绘图模块
  11. from bokeh.plotting import figure,show
  12. from bokeh.models import ColumnDataSource
  13. # 导入图表绘制、图标展示模块
  14. # 导入ColumnDataSource模块

筛选数据

隐藏

  1. # 筛选数据 - 隐藏
  2. # legend.click_policy
  3. from bokeh.palettes import Spectral4
  4. # 导入颜色模块
  5. df = pd.DataFrame({'A':np.random.randn(500).cumsum(),
  6. 'B':np.random.randn(500).cumsum(),
  7. 'C':np.random.randn(500).cumsum(),
  8. 'D':np.random.randn(500).cumsum()},
  9. index = pd.date_range('20180101',freq = 'D',periods=500))
  10. # 创建数据
  11. p = figure(plot_width=800, plot_height=400, x_axis_type="datetime")
  12. p.title.text = '点击图例来隐藏数据'
  13. for col,color in zip(df.columns.tolist(),Spectral4):
  14. p.line(df.index,df[col],line_width=2, color=color, alpha=0.8,legend = col)
  15. p.legend.location = "top_left"
  16. p.legend.click_policy="hide"
  17. # 设置图例,点击隐藏
  18. show(p)

图片.png


消隐

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

图片.png


交互小工具—图表分页

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

图片.png图片.png