导入代码库

  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. # 工具栏 tools
  2. # 设置位置
  3. p1 = figure(plot_width = 300, plot_height = 300,
  4. toolbar_location = 'above') # 工具栏位置:"above","below","left","right"
  5. p1.circle(np.random.randn(100),np.random.randn(100))
  6. p2 = figure(plot_width=300, plot_height=300,
  7. toolbar_location="below",
  8. toolbar_sticky=False)
  9. # 工具栏位置设置为"below"时,可增加toolbar_sticky参数使得toolsbar不被遮挡
  10. p2.circle(np.random.randn(100),np.random.randn(100))
  11. show(p1)
  12. show(p2)

图片.png


移动、放大缩小、存储、刷新

  1. # 工具栏 tools
  2. # 移动、放大缩小、存储、刷新
  3. TOOLS = '''
  4. pan, xpan, ypan,
  5. box_zoom,
  6. wheel_zoom, xwheel_zoom, ywheel_zoom,
  7. zoom_in, xzoom_in, yzoom_in,
  8. zoom_out, xzoom_out, yzoom_out,
  9. save,reset
  10. '''
  11. p = figure(plot_width=800, plot_height=400,toolbar_location="above",
  12. tools = TOOLS)
  13. # 添加toolbar
  14. # 这里tools = '' 则不显示toolbar
  15. p.circle(np.random.randn(500),np.random.randn(500))
  16. show(p)

图片.png


选择

  1. # 工具栏 tools
  2. # 选择
  3. TOOLS = '''
  4. box_select,lasso_select,
  5. reset
  6. '''
  7. p = figure(plot_width=800, plot_height=400,toolbar_location="above",
  8. tools = TOOLS)
  9. # 添加toolbar
  10. p.circle(np.random.randn(500),np.random.randn(500))
  11. show(p)

图片.png


提示框、十字线

  1. # 工具栏 tools
  2. # 提示框、十字线
  3. from bokeh.models import HoverTool
  4. # 用于设置显示标签内容
  5. df = pd.DataFrame({'A':np.random.randn(500)*100,
  6. 'B':np.random.randn(500)*100,
  7. 'type':np.random.choice(['pooh', 'rabbit', 'piglet', 'Christopher'],500),
  8. 'color':np.random.choice(['red', 'yellow', 'blue', 'green'],500)})
  9. df.index.name = 'index'
  10. source = ColumnDataSource(df)
  11. print(df.head())
  12. # 创建数据 → 包含四个标签
  13. hover = HoverTool(tooltips=[
  14. ("index", "$index"),
  15. ("(x,y)", "($x, $y)"),
  16. ("A", "@A"),
  17. ("B", "@B"),
  18. ("type", "@type"),
  19. ("color", "@color"),
  20. ])
  21. # 设置标签显示内容
  22. # $index:自动计算 → 数据index
  23. # $x:自动计算 → 数据x值
  24. # $y:自动计算 → 数据y值
  25. # @A:显示ColumnDataSource中对应字段值
  26. p1 = figure(plot_width=800, plot_height=400,toolbar_location="above",
  27. tools=[hover,'box_select,reset,wheel_zoom,pan,crosshair']) # 注意这里书写方式
  28. # 如果不设置标签,就只写hover,例如 tools='hover,box_select,reset,wheel_zoom,pan,crosshair'
  29. p1.circle(x = 'A',y = 'B',source = source,size = 10,alpha = 0.3, color = 'color')
  30. show(p1)
  31. p2 = figure(plot_width=800, plot_height=400,toolbar_location="above",
  32. tools=[hover,'box_select,reset,wheel_zoom,pan'])
  33. p2.vbar(x = 'index', width=1, top='A',source = source)
  34. show(p2)
  35. print(hover)

图片.png图片.png