二、Bokeh風格與主題

開始先一波標準導入

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

畫布設(shè)置

# 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

針對畫布設(shè)置可以通過屬性.outline_line_width, .outline_line_alpha, .outline_line_color 等進行配置。
類似的還有.background_fill_color, .border_fill_color 等等
總而言之, 針對畫布的設(shè)置, 都通過figure下的參數(shù)進行配置。

標記符號設(shè)置

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

針對標記的設(shè)置, 就是配置當前標記下的.glyph對象下的屬性, 如上例子配置標記的大小就使用r.glyph.size = 50; 配置標記邊緣虛線樣式就使用r.glyph.line_dash = [5, 1], 意思是每6個單位的邊緣線, 有一個單位為透明的。
總而言之, 與畫布設(shè)置如出一轍, 設(shè)置標記就使用標記下的配置項。

選擇與非選擇下的動態(tài)展示

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

既然我們選擇的是圖片上的標記符號, 所以隨著選擇的改變, 展示的變化也是標記符號本身。
按照一貫的套路, 這種配置也應(yīng)該配入“標記符號”。
上一段代碼展示的配置方法是在標記符號初始化的時候, 以參數(shù)的形式進行配置。
我們也可以在初始化之后, 再配置, 如下一段代碼:

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

懸停動態(tài)響應(yīng)

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

上面案例的本質(zhì)是:
1、畫布準備與之前的樣例一樣
2、作圖時, 首先畫一條虛線, 固定的, 沒有動態(tài)反應(yīng)
3、在與虛線相同的位置上, 再畫一層很透明的圓形標記。 這層圓形標記會對懸浮指針作出反應(yīng)。

對懸停起反應(yīng)的標記符號為:

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")

左側(cè)沒有hover開頭的配置項即普通狀態(tài)下的樣式, 右側(cè)hover開頭的配置項即被懸停指定標記所展現(xiàn)的樣式。

坐標軸

坐標軸的樣式設(shè)計, 也是在配置畫布的時候進行配置。 比如:

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

坐標軸參數(shù)

針對坐標軸的參數(shù)很多,不過通過配置項的前綴, 我們可以將他們大致歸類:

  • 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軸的標題。帶有l(wèi)abel的標簽是針對坐標軸刻度的配置項,比如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 軸的刻度都是數(shù)字, 通過NumeralTickFormatter定義其格式。

網(wǎng)格的設(shè)置

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軸刻度延長方向、豎向網(wǎng)格線不顯示
p.ygrid.grid_line_alpha = 0.5 意味著y軸刻度延長方向,橫向網(wǎng)格線半透明
p.ygrid.grid_line_dash = [6, 4] 意味著y軸刻度延長方向,橫向網(wǎng)格線程虛線形式

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軸刻度延長線、豎向網(wǎng)格線不顯示
p.ygrid.band_fill_alpha = 0.1 y軸刻度延長線、向橫向網(wǎng)格填充顏色,0.1的透明度
p.ygrid.band_fill_color = "navy" 填充的顏色為深藍色

下一篇: 三、數(shù)據(jù)源的定義與轉(zhuǎn)化

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容