開始先一波標(biāo)準(zhǔn)導(dǎo)入
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)
針對(duì)畫布設(shè)置可以通過屬性.outline_line_width
, .outline_line_alpha
, .outline_line_color
等進(jìn)行配置莫鸭。
類似的還有.background_fill_color
, .border_fill_color
等等
總而言之员舵, 針對(duì)畫布的設(shè)置, 都通過figure
下的參數(shù)進(jìn)行配置徘钥。
標(biāo)記符號(hào)設(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)
針對(duì)標(biāo)記的設(shè)置变骡, 就是配置當(dāng)前標(biāo)記下的.glyph
對(duì)象下的屬性, 如上例子配置標(biāo)記的大小就使用r.glyph.size = 50
; 配置標(biāo)記邊緣虛線樣式就使用r.glyph.line_dash = [5, 1]
, 意思是每6個(gè)單位的邊緣線离赫, 有一個(gè)單位為透明的。
總而言之塌碌, 與畫布設(shè)置如出一轍渊胸, 設(shè)置標(biāo)記就使用標(biāo)記下的配置項(xiàng)。
選擇與非選擇下的動(dòng)態(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)
既然我們選擇的是圖片上的標(biāo)記符號(hào)台妆, 所以隨著選擇的改變翎猛, 展示的變化也是標(biāo)記符號(hào)本身。
按照一貫的套路接剩, 這種配置也應(yīng)該配入“標(biāo)記符號(hào)”切厘。
上一段代碼展示的配置方法是在標(biāo)記符號(hào)初始化的時(shí)候, 以參數(shù)的形式進(jìn)行配置懊缺。
我們也可以在初始化之后疫稿, 再配置, 如下一段代碼:
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)
懸停動(dòng)態(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)
上面案例的本質(zhì)是:
1鹃两、畫布準(zhǔn)備與之前的樣例一樣
2遗座、作圖時(shí), 首先畫一條虛線俊扳, 固定的途蒋, 沒有動(dòng)態(tài)反應(yīng)
3、在與虛線相同的位置上馋记, 再畫一層很透明的圓形標(biāo)記号坡。 這層圓形標(biāo)記會(huì)對(duì)懸浮指針作出反應(yīng)。
對(duì)懸停起反應(yīng)的標(biāo)記符號(hào)為:
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開頭的配置項(xiàng)即普通狀態(tài)下的樣式抗果, 右側(cè)hover開頭的配置項(xiàng)即被懸停指定標(biāo)記所展現(xiàn)的樣式。
坐標(biāo)軸
坐標(biāo)軸的樣式設(shè)計(jì)奸晴, 也是在配置畫布的時(shí)候進(jìn)行配置冤馏。 比如:
p.xaxis.axis_label = "Temperature"
p.axis.major_label_text_color = "orange"
坐標(biāo)軸參數(shù)
針對(duì)坐標(biāo)軸的參數(shù)很多,不過通過配置項(xiàng)的前綴寄啼, 我們可以將他們大致歸類:
-
axis 坐標(biāo)軸(線)配置項(xiàng)逮光, 例如:
axis_line_width
-
axis_label 坐標(biāo)軸(文字)配置項(xiàng)代箭, 例如:
axis_label_text_color
,axis_label_standoff
-
major_label 坐標(biāo)軸(文字)配置項(xiàng), 例如:
major_label_text_font_size
,major_label_orientation
-
major_tick 坐標(biāo)軸(線)配置項(xiàng)涕刚, 例如:
major_tick_line_dash
,major_tick_in
,major_tick_out
-
minor_tick 坐標(biāo)軸(線)配置項(xiàng)嗡综, 例如:
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)
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)
總而言之, 以p.axis開頭的配置杜漠, 是針對(duì)橫豎坐標(biāo)同時(shí)作用的配置极景; 以p.xaxis開頭的配置都是針對(duì)x軸進(jìn)行的配置;同樣的驾茴, 以p.yaxis開頭的配置都是針對(duì)y軸的配置盼樟。
再之后的配置項(xiàng),以axis開頭的都是針對(duì)坐標(biāo)軸本身的配置锈至。 比如p.xaxis.axis_label
x軸的標(biāo)題晨缴。帶有l(wèi)abel的標(biāo)簽是針對(duì)坐標(biāo)軸刻度的配置項(xiàng),比如p.yaxis.major_label_orientation
y坐標(biāo)刻度的朝向峡捡。
刻度配置項(xiàng)
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)
上面列子可以看出击碗, 時(shí)間戳?xí)r間默認(rèn)為月份/日期,但我們可以通過配置格式p.xaxis.formatter.days = '%m/%d/%Y'
使得x軸的時(shí)間顯示為月份/日期/年份们拙。 同時(shí)由于配置項(xiàng)p.xaxis.major_label_orientation = pi/3
使得時(shí)間刻度的顯示傾斜1/3個(gè)π的角度稍途。
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)
上例子中, 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)
p.xgrid.grid_line_color = None
意味著x軸刻度延長(zhǎng)方向、豎向網(wǎng)格線不顯示
p.ygrid.grid_line_alpha = 0.5
意味著y軸刻度延長(zhǎng)方向射沟,橫向網(wǎng)格線半透明
p.ygrid.grid_line_dash = [6, 4]
意味著y軸刻度延長(zhǎng)方向殊者,橫向網(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)
p.xgrid.grid_line_color = None
x軸刻度延長(zhǎng)線、豎向網(wǎng)格線不顯示
p.ygrid.band_fill_alpha = 0.1
y軸刻度延長(zhǎng)線验夯、向橫向網(wǎng)格填充顏色猖吴,0.1的透明度
p.ygrid.band_fill_color = "navy"
填充的顏色為深藍(lán)色