# Dataframe DatetimeIndex + x_axis_type# 导入数据from bokeh.sampledata.commits import data
print(data.head())
DAYS=['Sun','Sat','Fri','Thu','Wed','Tue','Mon']
source=ColumnDataSource(data)
p=figure(plot_width=800, plot_height=600,
y_range=DAYS,# 设置图表的y轴刻度分类
x_axis_type='datetime',# 设置x轴类型 -> 时间序列
title="Commits by Time of Day 2012-2016")
p.circle(x='time', y='day', source=source, alpha=0.2)
p.ygrid.grid_line_color=None
show(p)
轴线标签设置 - 设置对数坐标轴
x=[0.1,0.5,1.0,1.5,2.0,2.5,3.0]
y=[10**i for i in x]
p=figure(plot_width=400, plot_height=400, y_axis_type='log')# y_axis_type='log' : 对数坐标轴#p=figure(plot_width=400, plot_height=400)
p.line(x, y, line_width=2)
p.circle(x,y,fill_color='white',size=8)
show(p)
浮动设置 : Jitter
from bokeh.transform import jitter
DAYS=['Sun','Sat','Fri','Thu','Wed','Tue','Mon']
source=ColumnDataSource(data)
p=figure(plot_width=800, plot_height=600,
y_range=DAYS,# 设置图表的y轴刻度分类
x_axis_type='datetime',# 设置x轴类型 -> 时间序列
title="Commits by Time of Day 2012-2016",
tools='hover'# 鼠标放到图上会显示点的数据)
p.circle(x='time',
y=jitter('day', width=0.6,range=p.y_range),
source=source, alpha=0.3)#jitter参数 -> 'day': 第一参数,这里指y的值,width:间隔宽度比例,range:分类范围对象,这里和y轴的分类一类
p.ygrid.grid_line_color=None
show(p)
多图表设置:gridplot
from bokeh.layouts import gridplot
x=list(range(11))
y0=x
y1=[10-i for i in x]
y2=[abs(i-5)for i in x]# 散点图1
s1=figure(plot_width=250, plot_height=250, title=None)
s1.circle(x, y0, size=10, color='navy', alpha=0.5)# 散点图2, 设置和散点图1一样的x_range/y_range -> 图表联动
s2=figure(plot_width=250, plot_height=250, x_range=s1.x_range, y_range=s1.y_range, title=None)
s2.triangle(x, y1, size=10, color='firebrick', alpha=0.5)# 散点图3, 设置和散点图1一样的x_range/y_range -> 图表联动
s3=figure(plot_width=250, plot_height=250,x_range=s1.x_range, y_range=s1.y_range, title=None)
s3.square(x, y2, size=10, color='firebrick', alpha=0.5)# 组合图表
p=gridplot([[s1 ,s2, s3]])
show(p)
多图表设置 - 示例2
x=list(range(-20,21))
y0=[abs(i)for i in x]
y1=[i**2for i in x]
source=ColumnDataSource(data=dict(x=x, y0=y0, y1=y1))
TOOLS="box_select,lasso_select,help,hover"
left=figure(tools=TOOLS, plot_width=300, plot_height=300, title=None)
left.circle('x','y0', source=source)# 散点图1
right=figure(tools=TOOLS, plot_width=300, plot_height=300, title=None)
right.circle('x','y1', source=source)# 散点图2# 共用一个ColumnDataSource
p=gridplot([[left, right]])
show(p)