辅助标注 - 线

  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. #导入图表绘制、图表展示模块
  13. #辅助标记 - 线
  14. from bokeh.models.annotations import Span
  15. #导入Span模块
  16. x = np.linspace(0, 20, 200)
  17. y = np.sin(x)
  18. #创建x,y数据
  19. p = figure(y_range = (-1.5, 1.5)) #y_range设置y轴的范围
  20. p.line(x, y)
  21. #绘制曲线
  22. upper = Span(location = 1, #设置辅助线的位置,这设置辅助线在值为1的位置
  23. dimension = 'width', #设置辅助线的方向,width为横向,height为纵向
  24. line_color = 'red', #设置线的颜色
  25. line_width = 4 #设置线宽
  26. )
  27. p.add_layout(upper)
  28. #绘制辅助线1
  29. lower = Span(location = -1, #设置辅助线的位置
  30. dimension = 'width', #设置辅助线的方向
  31. line_color = 'blue', #设置辅助线的颜色
  32. line_width = 3, #设置辅助线的粗细,
  33. line_dash = [8, 4] #设置辅助线虚线显示
  34. )
  35. p.add_layout(lower)
  36. #绘制辅助线2
  37. show(p)

Bokeh中设置辅助线有自己的一套体系,需要用到Span模块,具体参数如下:
line:绘制线状图,参数为x、y,其是连点成线,将x和y定位的点连起来成一条线
y_range:设置y轴的可视化范围,上图中范围为(-1.5, 1.5),即图表y轴需要显示从-1.5到1.5之间的轴

Span模块是为了实现辅助线的绘制,穷参数如下:
location:设置辅助线的位置,例如当其职为1的时候,辅助线位于x = 1或者y = 1这条线上
dimesion:设置辅助线的显示方向,当其值为width时,辅助线横向显示,当其值为height时,辅助线竖向显示
line_color:设置辅助线的颜色
line_width:设置辅助线的粗细
line_dash:设置辅助线为虚线显示
add_layout:为了显示辅助线的函数
图片.png


辅助标注 - 矩形

  1. from bokeh.models.annotations import BoxAnnotation
  2. #导入BoxAnnotation模块
  3. x = np.linspace(0, 20, 200)
  4. y = np.sin(x)
  5. #创建x, y数据
  6. p = figure(y_range = (-2, 2))
  7. p.line(x, y)
  8. #绘制曲线
  9. upper = BoxAnnotation(bottom = 1, #设置矩形下方 的位置
  10. fill_alpha = 0.3, #设置矩形的透明度
  11. fill_color = 'olive', #设置矩形的颜色
  12. line_width = 2, #设置矩形框的粗细
  13. line_color = 'red', #设置矩形框的颜色
  14. line_dash = [6, 3] #设置矩形框虚线显示
  15. )
  16. p.add_layout(upper)
  17. #显示矩形框
  18. #设置辅助矩形1
  19. lower = BoxAnnotation(top = -1, #设置矩形上方位置
  20. fill_alpha = 0.2, #设置矩形的透明度
  21. fill_color = 'blue',
  22. line_width = 3, #设置矩形框的粗细
  23. line_color = 'green', #设置矩形框的颜色
  24. line_dash = [8, 4]) #设置矩形框虚线显示
  25. p.add_layout(lower)
  26. #显示矩形框
  27. #设置辅助矩形2
  28. center = BoxAnnotation(top = 0.6, bottom = -0.4, left = 7, right = 12, #设置矩形四个边的位置
  29. fill_alpha = 0.3,
  30. fill_color = 'orange'
  31. )
  32. p.add_layout(center)
  33. show(p)

在Bokeh中,辅助标注也可以做一个矩形,需要使用BoxAnnotation模块来实现,参数如下:
bottom:设置矩形下边的位置
top:设置矩形上边的位置
left:设置矩形左边的位置
right:设置矩形右边的位置
fill_alpha:设置矩形的透明度
fill_color:设置举行的颜色
line_width:设置矩形边的粗细
line_color:设置矩形边线的颜色
line_dash:设置矩形边线虚线显示
图片.png


绘图注释

  1. from bokeh.models.annotations import Label
  2. #导入Label模块
  3. p = figure(x_range = (0, 10), y_range = (0, 10))
  4. p.circle([2, 5, 8], [4, 7, 6], color = 'olive', size = 10)
  5. #绘制散点图
  6. label = Label(x = 5, y = 7, #注释的点
  7. x_offset = 12, #x偏移量,同样的还有y_offset
  8. text = 'Second Point', #注释内容
  9. text_font_size = "12pt", #字体大小
  10. border_line_color = 'red', #注释框颜色
  11. background_fill_color = 'gray', #注释的背景颜色
  12. background_fill_alpha = 0.5 #注释背景的透明度
  13. )
  14. p.add_layout(label)
  15. show(p)

Label模块可以用来做注释,如下图可见,参数如下:
x,y:设置注释所在的点
x_offset、y_offset:设置偏移量
text:注释内容
text_font_size:设置注释内容字体的大小
border_line_color:设置注释框的颜色
background_fill_color:设置注释框的背景颜色
background_fill_alpha:设置注释框背景颜色的透明度
图片.png


注释箭头

  1. #注释箭头
  2. from bokeh.models.annotations import Arrow
  3. from bokeh.models.arrow_heads import OpenHead, NormalHead, VeeHead #三种箭头类型
  4. #导入相关模块
  5. p = figure(plot_width = 600, plot_height = 600)
  6. p.circle(x = [0, 1, 0.5], y = [0, 0, 0.7], radius = 0.1, color = ['navy', 'yellow', 'red'], fill_alpha = 0.5)
  7. #创建散点图
  8. #radius圆的半径
  9. p.add_layout(Arrow(end = OpenHead(line_color = 'green', line_width = 4), #设置箭头类型
  10. x_start = 0, y_start = 0, x_end =1, y_end = 0 #设置箭头的起始位置和终点
  11. ))
  12. #绘制箭头1
  13. p.add_layout(Arrow(end = NormalHead(line_color = 'red', line_width = 2), #设置箭头类型
  14. x_start = 1, y_start = 0, x_end = 0.5, y_end = 0.7 #设置箭头的起始位置和终点
  15. ))
  16. #绘制箭头2
  17. p.add_layout(Arrow(end = VeeHead(line_color = 'blue', line_width = 2), #设置箭头类型
  18. x_start = 0.5, y_start = 0.7, x_end = 0, y_end = 0
  19. ))
  20. #绘制箭头3
  21. show(p)

绘制箭头类型有三种,分别是OpenHead、NormalHead、VeeHead,其参数如下:
line_color:线头的颜色
line_width:箭头线的粗细
x_start、y_start:箭头的起始位置
x_end、y_end:箭头的终点
图片.png


调色盘

  1. # 调色盘
  2. # 颜色参考文档:http://bokeh.pydata.org/en/latest/docs/reference/palettes.html
  3. # ColorBrewer:http://colorbrewer2.org/#type=sequential&scheme=BuGn&n=3
  4. import bokeh.palettes as bp
  5. from bokeh.palettes import brewer
  6. print('所有调色板名称:\n',bp.__palettes__)
  7. print('-------')
  8. # 查看所有调色板名称
  9. print('蓝色调色盘颜色:\n',bp.Blues)
  10. print('-------')
  11. # 查看蓝色调色盘颜色
  12. n = 8
  13. colori = brewer['YlGn'][n]
  14. print('YlGn调色盘解析为%i个颜色,分别为:\n' % n, colori)
  15. # 调色盘解析 → 不同颜色解析最多颜色有限