美文网首页
二、Bokeh风格与主题

二、Bokeh风格与主题

作者: Bllose | 来源:发表于2021-03-12 07:59 被阅读0次

    开始先一波标准导入

    from bokeh.io import output_notebook, show
    from bokeh.plotting import figure   
    output_notebook()
    

    画布设置

    # create a new plot with a title
    p = figure(plot_width=400, plot_height=400)
    p.outline_line_width = 7
    p.outline_line_alpha = 0.3
    p.outline_line_color = "navy"
    
    p.circle([1,2,3,4,5], [2,5,8,2,7], size=10)
    
    show(p)
    
    bokeh_plot (5).png

    针对画布设置可以通过属性.outline_line_width, .outline_line_alpha, .outline_line_color 等进行配置。
    类似的还有.background_fill_color, .border_fill_color 等等
    总而言之, 针对画布的设置, 都通过figure下的参数进行配置。

    标记符号设置

    p = figure(plot_width=400, plot_height=400)
    
    # keep a reference to the returned GlyphRenderer
    r = p.circle([1,2,3,4,5], [2,5,8,2,7])
    
    r.glyph.size = 50
    r.glyph.fill_alpha = 0.2
    r.glyph.line_color = "firebrick"
    r.glyph.line_dash = [5, 1]
    r.glyph.line_width = 2
    
    show(p)
    
    bokeh_plot (6).png

    针对标记的设置, 就是配置当前标记下的.glyph对象下的属性, 如上例子配置标记的大小就使用r.glyph.size = 50; 配置标记边缘虚线样式就使用r.glyph.line_dash = [5, 1], 意思是每6个单位的边缘线, 有一个单位为透明的。
    总而言之, 与画布设置如出一辙, 设置标记就使用标记下的配置项。

    选择与非选择下的动态展示

    p = figure(plot_width=400, plot_height=400, tools="tap", title="Select a circle")
    renderer = p.circle([1, 2, 3, 4, 5], [2, 5, 8, 2, 7], size=50,
    
                        # set visual properties for selected glyphs
                        selection_color="firebrick",
    
                        # set visual properties for non-selected glyphs
                        nonselection_fill_alpha=0.2,
                        nonselection_fill_color="grey",
                        nonselection_line_color="firebrick",
                        nonselection_line_alpha=1.0)
    
    show(p)
    
    selection.PNG

    既然我们选择的是图片上的标记符号, 所以随着选择的改变, 展示的变化也是标记符号本身。
    按照一贯的套路, 这种配置也应该配入“标记符号”。
    上一段代码展示的配置方法是在标记符号初始化的时候, 以参数的形式进行配置。
    我们也可以在初始化之后, 再配置, 如下一段代码:

    from bokeh.models.glyphs import Circle, Square
     
    p = figure(plot_width=400, plot_height=400, tools="tap", title="Select a circle")
    renderer = p.circle([1, 2, 3, 4, 5], [2, 5, 8, 2, 7], size=50)
    renderer.selection_glyph = Circle(fill_alpha = 1, fill_color="yellow", line_color = None)
    renderer.nonselection_glyph = Circle(fill_alpha = 0.2, fill_color = "green", line_color = "yellow")
    show(p)
    
    selection_1.PNG

    悬停动态响应

    from bokeh.models.tools import HoverTool
    from bokeh.sampledata.glucose import data
    
    subset = data.loc['2010-10-06']
    
    x, y = subset.index.to_series(), subset['glucose']
    
    # Basic plot setup
    p = figure(width=600, height=300, x_axis_type="datetime", title='Hover over points')
    
    p.line(x, y, line_dash="4 4", line_width=1, color='gray')
    
    cr = p.circle(x, y, size=20,
                  fill_color="grey", hover_fill_color="firebrick",
                  fill_alpha=0.05, hover_alpha=0.3,
                  line_color=None, hover_line_color="white")
    
    p.add_tools(HoverTool(tooltips=None, renderers=[cr], mode='hline'))
    
    show(p)  
    
    Hover.png

    上面案例的本质是:
    1、画布准备与之前的样例一样
    2、作图时, 首先画一条虚线, 固定的, 没有动态反应
    3、在与虚线相同的位置上, 再画一层很透明的圆形标记。 这层圆形标记会对悬浮指针作出反应。

    对悬停起反应的标记符号为:

    cr = p.circle(x, y, size=20,
                  fill_color="grey", hover_fill_color="firebrick",
                  fill_alpha=0.05, hover_alpha=0.3,
                  line_color=None, hover_line_color="white")
    

    左侧没有hover开头的配置项即普通状态下的样式, 右侧hover开头的配置项即被悬停指定标记所展现的样式。

    坐标轴

    坐标轴的样式设计, 也是在配置画布的时候进行配置。 比如:

    p.xaxis.axis_label = "Temperature"
    p.axis.major_label_text_color = "orange"
    

    坐标轴参数

    针对坐标轴的参数很多,不过通过配置项的前缀, 我们可以将他们大致归类:

    • axis 坐标轴(线)配置项, 例如: axis_line_width
    • axis_label 坐标轴(文字)配置项, 例如: axis_label_text_color, axis_label_standoff
    • major_label 坐标轴(文字)配置项, 例如: major_label_text_font_size, major_label_orientation
    • major_tick 坐标轴(线)配置项, 例如: major_tick_line_dash, major_tick_in, major_tick_out
    • minor_tick 坐标轴(线)配置项, 例如: minor_tick_line_width, minor_tick_in, minor_tick_out
    from math import pi
    
    p = figure(plot_width=400, plot_height=400)
    p.x([1,2,3,4,5], [2,5,8,2,7], size=10, line_width=2)
    
    p.xaxis.major_label_orientation = pi/4
    p.yaxis.major_label_orientation = "vertical"
    
    show(p)
    
    bokeh_plot (7).png
    p = figure(plot_width=400, plot_height=400)
    p.asterisk([1,2,3,4,5], [2,5,8,2,7], size=12, color="olive")
    
    # change just some things about the x-axes
    p.xaxis.axis_label = "Temp"
    p.xaxis.axis_line_width = 3
    p.xaxis.axis_line_color = "red"
    
    # change just some things about the y-axes
    p.yaxis.axis_label = "Pressure"
    p.yaxis.major_label_text_color = "orange"
    p.yaxis.major_label_orientation = "vertical"
    
    # change things on all axes
    p.axis.minor_tick_in = -3
    p.axis.minor_tick_out = 6
    
    show(p)
    
    bokeh_plot (8).png

    总而言之, 以p.axis开头的配置, 是针对横竖坐标同时作用的配置; 以p.xaxis开头的配置都是针对x轴进行的配置;同样的, 以p.yaxis开头的配置都是针对y轴的配置。
    再之后的配置项,以axis开头的都是针对坐标轴本身的配置。 比如p.xaxis.axis_labelx轴的标题。带有label的标签是针对坐标轴刻度的配置项,比如p.yaxis.major_label_orientationy坐标刻度的朝向。

    刻度配置项

    from math import pi
    from bokeh.sampledata.glucose import data
    
    week = data.loc['2010-10-01':'2010-10-08']
    
    p = figure(x_axis_type="datetime", title="Glocose Range", plot_height=350, plot_width=800)
    p.xaxis.formatter.days = '%m/%d/%Y'
    p.xaxis.major_label_orientation = pi/3
    
    p.line(week.index, week.glucose)
    
    show(p)
    
    bokeh_plot (9).png

    上面列子可以看出, 时间戳时间默认为月份/日期,但我们可以通过配置格式p.xaxis.formatter.days = '%m/%d/%Y'使得x轴的时间显示为月份/日期/年份。 同时由于配置项p.xaxis.major_label_orientation = pi/3使得时间刻度的显示倾斜1/3个π的角度。

    from bokeh.models import NumeralTickFormatter
    
    p = figure(plot_height=300, plot_width=800)
    p.circle([1,2,3,4,5], [2,5,8,2,7], size=10)
    
    p.xaxis.formatter = NumeralTickFormatter(format="0.0%")
    p.yaxis.formatter = NumeralTickFormatter(format="$0.00")
    
    show(p)
    
    bokeh_plot (10).png

    上例子中, x, y 轴的刻度都是数字, 通过NumeralTickFormatter定义其格式。

    网格的设置

    p = figure(plot_width=400, plot_height=400)
    p.circle([1,2,3,4,5], [2,5,8,2,7], size=10)
    
    # change just some things about the x-grid
    p.xgrid.grid_line_color = None
    
    # change just some things about the y-grid
    p.ygrid.grid_line_alpha = 0.5
    p.ygrid.grid_line_dash = [6, 4]
    
    show(p)
    
    bokeh_plot (11).png

    p.xgrid.grid_line_color = None 意味着x轴刻度延长方向、竖向网格线不显示
    p.ygrid.grid_line_alpha = 0.5 意味着y轴刻度延长方向,横向网格线半透明
    p.ygrid.grid_line_dash = [6, 4] 意味着y轴刻度延长方向,横向网格线程虚线形式

    p = figure(plot_width=400, plot_height=400)
    p.circle([1,2,3,4,5], [2,5,8,2,7], size=10)
    
    # change just some things about the x-grid
    p.xgrid.grid_line_color = None
    
    # change just some things about the y-grid
    p.ygrid.band_fill_alpha = 0.1
    p.ygrid.band_fill_color = "navy"
    
    show(p)  
    
    bokeh_plot (12).png

    p.xgrid.grid_line_color = None x轴刻度延长线、竖向网格线不显示
    p.ygrid.band_fill_alpha = 0.1 y轴刻度延长线、向横向网格填充颜色,0.1的透明度
    p.ygrid.band_fill_color = "navy" 填充的颜色为深蓝色

    下一篇: 三、数据源的定义与转化

    相关文章

      网友评论

          本文标题:二、Bokeh风格与主题

          本文链接:https://www.haomeiwen.com/subject/himaqltx.html