数据呈现丨装X利器来袭,Python可视化库Bokeh助你俘获小姐姐的心
The following article is from Python数据之道 Author Lemonbit
什么是Bokeh
Bokeh是一个专门针对Web浏览器的呈现功能的交互式可视化Python库。这是Bokeh与其它可视化库最核心的区别。
matplotlib和seaborn都是面向过程的,在数据分析过程中可以呈现;Bokeh是在最后的结果呈现,可做动图,可只打开一张表,也可以做仪表盘的排版,可做图表的联动。
matplotlib是基于numpy和pandas做图表可视化工具包,而seaborn又是基于matplotlib做的。
大展身手
本次运行环境为:
win10
jupyter notebook
python 3.7
bokeh 1.2.0
Bokeh 中绘图的一般步骤:
1、加载 bokeh 库,声明在 notebook 或 html 文件中显示或输出绘制的图表
2、绘制图表框架 figure()
3、在 figure 上绘制具体的图形,比如 circle,line,bar等
4、显示图片,show()
from bokeh.plotting import figure, output_notebook, show
from bokeh.layouts import gridplot
import numpy as np
output_notebook()
np.random.seed(15)
x=np.random.randint(1,20,size=6)
y=np.random.randint(20,50,size=6)
print(x)
print(y)
circle, circle_cross, circle_x, cross
p1 = figure(title='circle')
p1.circle(x,y,size=20, color='#0071c1')
p2 = figure(title='circle_cross')
p2.circle_cross(x,y,size=20, color='#0071c1',fill_alpha=0.2, line_width=2)
p3 = figure(title='circle_x')
p3.circle_x(x,y,size=20, color='#0071c1',fill_alpha=0.2, line_width=2)
p4 = figure(title='cross')
p4.cross(x,y,size=20, color='#0071c1', line_width=2)
grid=gridplot([p1,p2,p3,p4],ncols=2, plot_width=300,plot_height=300)
show(grid)
左右滑动查看更多
图示如下:
diamond, diamond_cross, asterisk, x
p1 = figure(title='diamond')
p1.diamond(x,y,size=20, color='#0071c1')
p2 = figure(title='diamond_cross')
p2.diamond_cross(x,y,size=20, color='#0071c1',fill_alpha=0.2, line_width=2)
p3 = figure(title='asterisk')
p3.asterisk(x,y,size=20, color='#0071c1',fill_alpha=0.2, line_width=2)
p4 = figure(title='x')
p4.x(x,y,size=20, color='#0071c1', line_width=2)
grid=gridplot([p1,p2,p3,p4],ncols=2, plot_width=300,plot_height=300)
show(grid)
左右滑动查看更多square, square_cross, square_x
p1 = figure(title='square')
p1.square(x,y,size=20, color='#0071c1')
p2 = figure(title='square_cross')
p2.square_cross(x,y,size=20, color='#0071c1',fill_alpha=0.2, line_width=2)
p3 = figure(title='square_x')
p3.square_x(x,y,size=20, color='#0071c1',fill_alpha=0.2, line_width=2)
grid=gridplot([p1,p2,p3],ncols=2, plot_width=300,plot_height=300)
show(grid)
左右滑动查看更多
triangle, inverted_triangle
p3 = figure(title='triangle')
p3.triangle(x,y,size=20, color='#0071c1', line_width=2)
p4 = figure(title='inverted_triangle')
p4.inverted_triangle(x,y,size=20, color='#0071c1', line_width=2)
grid=gridplot([p3,p4],ncols=2, plot_width=300,plot_height=300)
show(grid)
左右滑动查看更多annulus, ellipse, wedge, oval
annulus, 绘制圆环
wedge, 楔形图
ellipse, 绘制椭圆
oval,绘制椭圆
p1 = figure(title='annulus')
p1.annulus(x,y,color='#0071c1', inner_radius=0.8, outer_radius=1.2)
p2 = figure(title='wedge')
p2.wedge(x,y,color='#0071c1', radius=0.8, start_angle=0.5, end_angle=4.5, direction='anticlock')
p3 = figure(title='ellipse')
p3.ellipse(x,y,color='#0071c1', width=2, height=3.6, angle=30)
p4 = figure(title='oval')
p4.oval(x,y,color='#0071c1', width=2, height=3.6, angle=30)
grid=gridplot([p1,p2,p3,p4],ncols=2, plot_width=300,plot_height=300)
show(grid)
左右滑动查看更多hbar, vbar
p2 = figure(title='hbar')
p2.hbar(y=y, height=0.3, left=0, right=x, color='#0071c1')
p3 = figure(title='vbar')
p3.vbar(x=x, width=0.3, bottom=0, top=y, color='#0071c1')
grid=gridplot([p2,p3],ncols=2, plot_width=300,plot_height=300)
show(grid)
左右滑动查看更多图示如下:
line, multi_line
p1 = figure(title='line')
p1.line(x,y,color='#0071c1', line_width=2)
p1.circle(x,y,size=10,color='#0071c1',fill_color='white')
from bokeh.plotting import figure, output_file, show
p2 = figure(title='multi_line')
p2.multi_line(xs=[[1, 2, 3], [2, 3, 4]], ys=[[6, 7, 2], [4, 5, 7]],
color=['red','green'])
grid=gridplot([p1,p2],ncols=2, plot_width=300,plot_height=300)
show(grid)
左右滑动查看更多来看看关于 multi_line 的一个稍微复杂一些的例子,如下:
from bokeh.palettes import Spectral11
import pandas as pd
df = pd.DataFrame(data=np.random.rand(5,3), columns = ('a', 'b' ,'c'),
index = pd.DatetimeIndex(start='01-01-2018',periods=5, freq='d'))
df
左右滑动查看更多numlines=len(df.columns)
mypalette=Spectral11[0:numlines]
p = figure(width=500, height=300, x_axis_type="datetime")
p.multi_line(xs=[df.index.values]*numlines,
ys=[df[name].values for name in df],
line_color=mypalette,
line_width=2)
show(p)
左右滑动查看更多图示如下:
patch, patches
patch(x, y, **kwargs)
patches(xs, ys, **kwargs)
p1 = figure(title='patch')
p1.patch(x,y,color='#0071c1')
p2 = figure(title='patches')
p2.patches(xs=[[1, 2, 3, 4], [2, 3, 4]], ys=[[6, 7, 2, 3], [4, 5, 7]],
color=['red','green'])
grid=gridplot([p1,p2],ncols=2, plot_width=300,plot_height=300)
show(grid)
左右滑动查看更多
quad, quadratic
quad(left, right, top, bottom, **kwargs),四方的
quadratic(x0, y0, x1, y1, cx, cy, **kwargs), 二次方程的
p1 = figure(title='quad')
p1.quad(left=[1,3,5],right=[2,4,7], top=[3,7,8], bottom=[2,3,4], color='#0071c1')
p2 = figure(title='quadratic')
p2.quadratic(x0=[1,3,5],x1=[2,4,7], y1=[3,7,8], y0=[2,3,4], cx=8 , cy=2, color='#0071c1')
grid=gridplot([p1,p2],ncols=2, plot_width=300,plot_height=300)
show(grid)
左右滑动查看更多ray, rect, segment, step
ray(x, y, length, angle, **kwargs) , 射线
rect(x, y, width, height, angle=0.0, dilate=False, **kwargs) , 矩形
segment(x0, y0, x1, y1, **kwargs) , 分段,段落
p1 = figure(title='ray', x_range=[1,3.6])
p1.ray(x=[1, 2, 3], y=[1, 2, 3], length=45, angle=20, color=["#0071c1", 'black', 'red'],
line_width=[2,2,5])
p2 = figure(title='rect')
p2.rect(x=[1, 2, 3], y=[1, 8, 3], width=0.3, height=[5,10,20],angle=[0,10,0], color="#0071c1")
p3= figure(title='segment')
p3.segment(x0=[1,3,5],x1=[2,4,7], y1=[3,7,8], y0=[2,3,4], color='#0071c1', line_width=3)
p4 = figure(title='step')
p4.step(x, y, color="#0071c1",line_width=2)
grid=gridplot([p1,p2,p3,p4],ncols=2, plot_width=300,plot_height=300)
show(grid)
左右滑动查看更多
综合小结
不看广告看疗效,谁用谁知道
►往期推荐
回复【Python】👉简单有用易上手
回复【学术前沿】👉机器学习丨大数据
回复【数据资源】👉公开数据
回复【可视化】👉你心心念念的数据呈现
回复【老姚专栏】👉老姚趣谈值得一看
►一周热文
学术前沿丨当计量经济学遭遇机器学习(四):高维回归之LASSO
Python数据之道
数据Seminar
这里是大数据、分析技术与学术研究的三叉路口
欢迎扫描👇二维码添加关注