python基础笔记(五)_Bokeh基础语法

交互式图表:Bokeh

绘图空间基本设置

  • 创建空间
    • notebook中创建
      • from bokeh.plotting import figure,show 导入图表绘制、图标展示模块
      • from bokeh.io import output_notebook 导入notebook绘图模块
      • output_notebook() notebook绘图命令
    • 非notebook中创建
      • from bokeh.plotting import show,figure,output_file
      • output_file → 非notebook中创建绘图空间
  • 创建图表工具 figure()
    • tools 设置工具栏,默认全部显示;
    • toolbar_location   工具栏位置:"above","below","left","right"
    • x_axis_label / y_axis_label X,Y轴label
    • x_range / y_range X,Y轴范围
    • title 设置图表title
      • p.title.text_color
      • p.title.text_font
      • p.title.text_font_style
      • p.title.background_fill_color
  • 颜色设置
  • 图表边框线参数设置
    • p.outline_line_width = 7 # 边框线宽
    • p.outline_line_color = 'white'          # 边框线颜色
    • p.outline_line_alpha = 0.5 # 边框线透明度
  • 设置绘图空间背景
    • p.background_fill_color = 'beige'         # 绘图空间背景颜色
    • p.background_fill_alpha = 0.5 # 绘图空间背景透明度
  • 设置外边界背景
    • p.border_fill_color = "whitesmoke"    # 外边界背景颜色
    • p.min_border_left = 80 # 外边界背景 - 左边宽度
    • p.min_border_right = 80 # 外边界背景 - 右边宽度
    • p.min_border_top = 10 # 外边界背景 - 上宽度
    • p.min_border_bottom = 10 # 外边界背景 - 下宽度
  • 轴线设置
    • p.xaxis.axis_label = 'Temp'  # 设置x轴 标签
    • p.xaxis.axis_line_color = 'red'  # 设置x轴 线宽
    • p.xaxis.axis_line_width = 4  # 设置x轴 轴线颜色
    • p.yaxis.axis_label = "Pressure"   # 设置y轴 标签
    • p.yaxis.major_label_text_color = "orange"   # 设置y轴 字体颜色
    • p.yaxis.major_label_orientation = "vertical"   # 设置y轴 字体角度
    • p.axis.minor_tick_in = 3 # 刻度往绘图区域内延伸长度
    • p.axis.minor_tick_out = 5 # 刻度往绘图区域外延伸长度
    • p.xaxis.bounds = (2,4) # 设置轴线范围
  • Grid - 格网设置
    • p.xgrid.grid_line_color = None 颜色设置,None时则不显示
    • p.ygrid.grid_line_color = 'red'  #设置网格颜色
    • p.ygrid.grid_line_alpha = 0.5   #设置网格透明度
    • p.ygrid.grid_line_dash = [3,6]   #设置网格虚线
    • p.xgrid.minor_grid_line_color = 'navy'     #设置网格次轴线
    • p.xgrid.minor_grid_line_alpha = 0.5
    • p.ygrid.band_fill_alpha = 0.1     #设置网格透明度
    • p.ygrid.band_fill_color = 'green'
    • p.grid.bounds = (-1,1) # 设置填充边界
  • Legend - 图例设置
    • p.legend.location
      • 设置图例位置:"top_left"、"top_center"、"top_right" (the default)、"center_right"、"bottom_right"、"bottom_center"
        # "bottom_left"、"center_left"、"center"
    • p.legend.orientation;#设置图例排列方向:"vertical" (默认)or "horizontal"
    • p.legend.label_text_font = "times"  ;#设置图例文字
    • p.legend.label_text_font_style = "italic"  # 斜体  ;#设置图例风格
    • p.legend.label_text_color = "black"  ;#设置图例颜色
    • p.legend.label_text_font_size = '12pt'  ;#设置图例字体大小
    • p.legend.border_line_width = 2  ;#设置图例外边线宽度
    • p.legend.border_line_color = "black"  ;#设置图例外边线颜色
    • p.legend.border_line_alpha = 0.5  ;#设置图例外边线透明度

图表辅助参数设置

  • 辅助标注 - 线
    • 导入Span模块 from bokeh.models.annotations import Span
    • location=1 # 设置位置,对应坐标值
    • dimension = 'width'                # 设置方向,width为横向,height为纵向 
    • line_color = 'olive',line_width = 4         # 设置线颜色、线宽
    • p.add_layout(upper) 绘制辅助线upper
  • 辅助标注 - 矩形
    • 导入BoxAnnotation模块 from bokeh.models.annotations import BoxAnnotation
    • top=0.6, bottom=-0.3, left=7, right=12, # 设置矩形四边位置
    • fill_alpha=0.1, fill_color='navy'        # 设置透明度、颜色
    • p.add_layout(upper) 绘制辅助矩形
  • 绘图注释
    • 导入Label模块 from bokeh.models.annotations import Label
    • x=5,y=7 # 标注注释位置
    • x_offset = 20 # x偏移,同理y_offset
    • text = 'Second Point'          # 注释内容
    • text_font_size = '10pt'       # 字体大小
    • border_line_color = 'red'
    • background_fill_color = 'gray'
    • background_fill_alpha = 0.5
    • p.add_layout(label) 绘制注释
  • 注释箭头
    • from bokeh.models.annotations import Arrow
    • from bokeh.models.arrow_heads import OpenHead, NormalHead, VeeHead # 三种箭头类型
    • p.add_layout() 绘制箭头
    • show(p)
  • 调色盘
    • import bokeh.palettes as bp
    • from bokeh.palettes import brewer
    • bp.__palettes__ 所有调色板名称
    • bp.Blues 蓝色调色盘颜色
    • colori = brewer['YlGn'][n     调色盘解析为n个颜色

各类图表绘制

  • 散点图
    • p.circle(x,y) 设置基本参数
    • color 是线+填充的颜色,可分别单独设置
    • 设置不同大小、颜色和透明度
    • 设置不同符号
  • 折线图
    • from bokeh.models import ColumnDataSource 导入ColumnDataSource模块
    • source = ColumnDataSource(data=df) 转化为ColumnDataSource对象
    • p.line(x,y,source) 设置x,y值, source → 数据源,绘制单线图
    • p.multi_line(x1,x2,y1,y2) 设置x,y的值,其它参数可同时设置,也可分别设置,传入列表
  • 面积图
    • 单维度面积图
      • s.iloc[0] = 0
      • s.iloc[-1] = 0
      • p.patch(x,y) 把所有点连接成一个闭合面
    • 面积堆叠图
  • 柱状图
    • 单系列柱状图
      • 竖向 p.hbar()
        • x=[1,2,3],width = 0.5,bottom = 0,top = [1.2,2.5,3.7], # x:横轴坐标,width:宽度,bottom:底高度,top:顶高度
        • color = ['red','green','blue'],alpha = 0.5,                # 整体颜色设置,也可单独设置 → color="firebrick"
        • line_width = 2,line_color = 'black',line_alpha = 0.5,line_dash = [6,4],         # 单独设置线参数
        • fill_color = 'red',fill_alpha = 0.5)                   # 单独设置填充颜色参数
        • show(p)
      • 横向 p.vbar()
        • y=[1,2,3],height = 0.5,right = [1.2,2.5,3.7],left = 0,     # y:纵轴坐标,height:厚度,left:左边最小值,right:右边最大值
        • color = ['red','green','blue'],alpha = 0.5
      • 分类设置标签
        • 标签名以列表形式传入
    • 多系列柱状图
      • 分别绘制
  • 堆叠图
    • p.vbar_stack()
    • years, # 设置堆叠值,这里source中包含了不同年份的值,years变量用于识别不同堆叠层
    •  x='fruits',     # 设置x坐标
    • source=source,需转换为ColumnDataSource对象
  • 直方图
    • np.histogram(data, bins=10, range=None, weights=None, density=None)
      • 返回值1 - hist:每个箱子的统计值(top)
      • 返回值2 - edges:每个箱子的位置坐标,这里n个bins将会有n+1个edges
    • p.quad() 绘图
    • top=hist, bottom=0, left=edges[:-1], right=edges[1:], # 分别代表每个柱子的四边值

绘图表达进阶操作

  • 轴线标签设置
    • 设置字符串:p.figure(x_range = name)
    • 时间序列设置:p = figure(x_axis_type='datetime')
    • 设置对数坐标轴:p.figure(y_axis_type='log')
  • 浮动设置 jitter
  • 多图表设置
    • from bokeh.layouts import gridplot
    • 分别绘制不同的figure图表
    • p = gridplot([[s1, s2, s3]]) 组合图表

ToolBar工具栏设置

  • 位置设置
  • 移动、放大缩小、存储、刷新
  • 选择
    • TOOLS = '''
              box_select,lasso_select,
              reset
              '''
  • 提示框、十字线
    • from bokeh.models import HoverTool
      • hover = HoverTool(tooltips=[
      •                             ("index", "$index"),
      •                             ("(x,y)", "($x, $y)"),
      •                             ("A", "@A"),
      •                             ("B", "@B"),
      •                             ("type", "@type"),
      •                             ("color", "@color"),
        • # 设置标签显示内容
        • # $index:自动计算 → 数据index
        • # $x:自动计算 → 数据x值
        • # $y:自动计算 → 数据y值
        • # @A:显示ColumnDataSource中对应字段值
    • tools=[hover,'box_select,reset,wheel_zoom,pan,crosshair']   注意书写方式

其他交互工具设置

  • p.legend.click_policy="hide"   设置图例,点击隐藏
  • p.legend.click_policy="mute"   设置图例,点击消隐
    • muted_color=color, 设置消隐后的显示颜色
    • muted_alpha=0.2 设置消隐后的透明度
  • 图表分页
    • from bokeh.models.widgets import Panel, Tabs 导入panel,tabs模块
  • tab1 = Panel(child=p1, title="circle")
    • child → 页码
    • title → 分页名称
  • tabs = Tabs(tabs=[ tab1, tab2 ]) 设置分页图表
  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值