导入函数库

  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. #导入output_notebook绘图模块
  11. from bokeh.plotting import figure, show
  12. from bokeh.models import ColumnDataSource
  13. #导入图表绘制、图表展示模块
  14. #导入ColumnDataSource模块

轴线标签设置

设置子字符串

  1. # 1、轴线标签设置
  2. # 设置字符串
  3. df = pd.DataFrame({'score' : [98, 86, 74, 67, 87]}, index = ['小明', '小王', '小张', '小红', '小红帽'])
  4. df.index.name = 'name'
  5. #创建数据
  6. source = ColumnDataSource(df)
  7. #将数据转换为ColumnDataSource对象
  8. name = df.index.tolist()
  9. p = figure(x_range = name, y_range = (60, 100), plot_height = 350, title = '考试成绩')
  10. #通过x_range设置横轴标签为名字,这里提取成list
  11. p.circle(x = 'name', y = 'score', source = source,
  12. size = 20, #设置点大小
  13. line_color = 'black', #设置点外边圈的颜色
  14. line_dash = [6, 4], #设置点外边圈的为虚线显示
  15. fill_color = 'red', #设置点填充颜色为红色
  16. fill_alpha = 0.7 #设置点填充颜色的透明度
  17. )
  18. show(p)

图片.png

时间序列设置

  1. # 1、轴线标签设置
  2. # 时间序列设置
  3. # Dataframe DatetimeIndex + x_axis_type
  4. from bokeh.sampledata.commits import data
  5. print(data.head())
  6. print(type(data.head()))
  7. DAYS = ['Sun', 'Sat', 'Fri', 'Thu', 'Wed', 'Tue', 'Mon']
  8. source = ColumnDataSource(data)
  9. #转化为ColumnDataSource对象
  10. p = figure(plot_width = 800, plot_height = 500,
  11. y_range = DAYS,
  12. x_axis_type = 'datetime',
  13. title = 'Commits by Time of Day (US/Central) 2012-2016')
  14. p.circle(x = 'time', y = 'day', source = source, alpha = 0.3)
  15. #生成散点图
  16. p.ygrid.grid_line_color = None
  17. #设置其他参数
  18. show(p)

图片.png

设置对数坐标轴

  1. # 1、轴线标签设置
  2. # 设置对数坐标轴
  3. x = [0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]
  4. y = [10**xx for xx in x ]
  5. #创建数据
  6. p = figure(plot_width = 400, plot_height = 300, y_axis_type = 'log')
  7. # y_axis_type="log" → 对数坐标轴
  8. p.line(x, y, line_width = 2)
  9. p.circle(x, y, size = 8, fill_color = 'white')
  10. show(p)

图片.png


浮动数据设置

设置浮动数据,需要用到Bokeh中一个Jitter的参数,其目的是为了将数据不重叠,将图中的点在一定范围内进行浮动显示,具体参数可以参考如下链接Bokeh浮动数据文档

  1. # 2、浮动设置
  2. # Jitter
  3. # 参考文档:https://bokeh.pydata.org/en/latest/docs/reference/transform.html
  4. from bokeh.sampledata.commits import data
  5. from bokeh.transform import jitter
  6. DAYS = ['Sun', 'Sat', 'Fri', 'Thu', 'Wed', 'Tue', 'Mon']
  7. source = ColumnDataSource(data)
  8. #转化为ColumnDataSource对象
  9. p = figure(plot_width = 600, plot_height = 400,
  10. y_range = DAYS, #设置图表的y轴刻度分类
  11. x_axis_type = 'datetime', #设置x轴类型--时间序列
  12. title = 'Commits by Time of Day (US/Central) 2012-2016',
  13. tools = 'hover' #为每一个散点做一个表示,鼠标放在散点上可查看信息
  14. )
  15. p.circle(x = 'time',
  16. y = jitter('day', width = 0.6, range = p.y_range),
  17. source = source, alpha = 0.3)
  18. # jitter参数 → 'day':第一参数,这里指y的值,width:间隔宽度比例,range:分类范围对象,这里和y轴的分类一致
  19. p.ygrid.grid_line_color = None
  20. #设置其他参数
  21. show(p)

图片.png


多图表设置

  1. # 3、多图表设置
  2. # gridplot
  3. from bokeh.layouts import gridplot
  4. # 导入gridplot模块
  5. x = list(range(11))
  6. y0 = x
  7. y1 = [10-xx for xx in x]
  8. y2 = [abs(xx-5) for xx in x]
  9. # 创建数据
  10. s1 = figure(plot_width=250, plot_height=250, title=None)
  11. s1.circle(x, y0, size=10, color="navy", alpha=0.5)
  12. # 散点图1
  13. s2 = figure(plot_width=250, plot_height=250, x_range=s1.x_range, y_range=s1.y_range, title=None)
  14. s2.triangle(x, y1, size=10, color="firebrick", alpha=0.5)
  15. # 散点图2,设置和散点图1一样的x_range/y_range → 图表联动
  16. s3 = figure(plot_width=250, plot_height=250, x_range=s1.x_range, title=None)
  17. s3.square(x, y2, size=10, color="olive", alpha=0.5)
  18. # 散点图3,设置和散点图1一样的x_range/y_range → 图表联动
  19. p = gridplot([[s1, s2, s3]])
  20. #p = gridplot([[s1, s2], [None, s3]])
  21. # 组合图表
  22. show(p)
  23. #图表联动,放大缩小一个表,其他表也会发生相应的变化

图片.png

  1. # 3、多图表设置
  2. # gridplot
  3. x = list(range(-20, 21))
  4. y0 = [abs(xx) for xx in x]
  5. y1 = [xx**2 for xx in x]
  6. source = ColumnDataSource(data=dict(x=x, y0=y0, y1=y1))
  7. # 创建数据
  8. TOOLS = "box_select,lasso_select,help"
  9. left = figure(tools=TOOLS, plot_width=300, plot_height=300, title=None)
  10. left.circle('x', 'y0', source=source) # 散点图1
  11. right = figure(tools=TOOLS, plot_width=300, plot_height=300, title=None)
  12. right.circle('x', 'y1', source=source) # 散点图2
  13. # 共用一个ColumnDataSource
  14. p = gridplot([[left, right]])
  15. # 组合图表
  16. show(p)
  17. #可以通过使用同一组数据,实现图表的联动

图片.png