当前位置: 首页 > 后端技术 > Python

交互式可视化Python库——Bokeh

时间:2023-03-26 01:43:50 Python

Bokeh基础知识Bokeh是一个用于浏览器渲染功能的交互式可视化Python库。这是Bokeh与其他可视化库的核心区别。Bokeh绘制步骤①获取数据②构建canvasfigure()③添加图层,绘制线、圆、方、散点、multi_line等;parameterscolor,legend④自定义视觉属性⑤选择性显示折线数据,创建复选框激活显示,复选框(checkbox)导入库和数据示例1.散点图importnumpyasnpiimportbokehfrombokeh.layoutsimportgridplotfrombokeh.plottingimportfigure,output_file,show#output_file("patch.html")#输出网页形式p=figure(plot_width=100,plot_height=100)#dataN=9x=np.linspace(-2,2,N)y=x**2sizes=np.linspace(10,20,N)xpts=np.array([-0.09,-0.12,0.0,0.12,0.09])ypts=np.array([-0.1,0.02,0.1,0.02,-0.1])p=figure(title="annular_wedge")p.annular_wedge(x,y,10,20,0.3,4.1,color="#8888ee",inner_radius_units="screen",outer_radius_units="screen")#设置在notebook中输出plotoutput_notebook()show(p)_20200316133935.png)2.来自bokeh.sampledata的多类散点图.iris从bokeh.plottingi导入花mportfigurefrombokeh.ioimportshow,output_notebook#色色colormap={'setosa':'red','versicolor':'green','virginica':'blue'}colors=[colormap[x]forxinflowers['species']]#canvasp=figure(title='TrisMorphology')#drawing#flowers['petal_length']为x,flowers['petal_width']为y,fill_alpha=0.3为填充透明度p.circle(flowers['petal_length'],flowers['petal_width'],color=colors,fill_alpha=0.3,size=10)#Displayoutput_notebook()show(p)_20200316133941.png)3.值的大小用散点图的大小表示importnumpyasnpfrombokeh.sampledata.irisimportflowersfrombokeh.plottingimportfigurefrombokeh.ioimportshow,output_notebookx=[1,2,3,4]y=[5,7,9,12]sizes=np.array(y)+10#bubblesizep=figure(title='气泡图')p=figure(plot_width=300,plot_height=300)p.scatter(x,y,marker="circle",size=sizes,color="navy")output_notebook()show(p)_20200316134035.png)4.linegraphfrombokeh.layoutsimportcolumn,gridplotfrombokeh.modelsimportBoxSelectTool,Divfrombokeh.plottingimportfigurefrombokeh.ioimportshow,输出_notebook#datax=[1,2,3,4,5,6,7]y=[6,7,2,4,5,10,4]#canvas:轴标签,画布大小p=figure(title="lineexample",x_axis_label='x',y_axis_label='y',width=400,height=400)#绘图:数据,图例,线宽p.line(x,y,legend="Temp.",line_width=2)#LineChart#Displayoutput_notebook()show(p)_20200316134039.png)5.同时显示不同的函数,以散点和折线的形式#数据,同时显示不同的函数,以散点和折线的形式Modex=[0.1,0.5,1.0,1.5,2.0,2.5,3.0]y0=[i**2foriinx]y1=[10**iforiinx]y2=[10**(i**2)foriinx]#创建画布p=figure(tools="pan,box_zoom,reset,save",y_axis_type="log",title="logaxisexample",x_axis_label='sections',y_axis_label='particles',width=700,height=350)#y轴类型:logindexorlinearlinear#添加图层,绘制p.line(x,x,legend="y=x")p.circle(x,x,legend="y=x",fill_color="white",size=8)p.line(x,y0,legend="y=x^2",line_width=3)p.line(x,y1,legend="y=10^x",line_color="red")p.circle(x,y1,legend="y=10^x",fill_color="red",line_color="red",size=6)p.line(x,y2,legend="y=10^x^2",line_color="orange",line_dash="44")#显示output_notebook()show(p)_20200316134107.png)6.不同颜色不同形状代表不同类别的事物#数据,同时显示不同的函数,形式为散点图和折线x=[0.1,0.5,1.0,1.5,2.0,2.5,3.0]y0=[i**2foriinx]y1=[10**iforiinx]y2=[10**(i**2)foriinx]#创建画布p=figure(tools="pan,box_zoom,reset,save",y_axis_type="log",title="logaxisexample",x_axis_label='sections',y_axis_label='particles',width=700,height=350)#y轴类型:logindexorlinearlinear#添加图层,绘制p.line(x,x,legend="y=x")p.circle(x,x,legend="y=x",fill_color="white",size=8)p.line(x,y0,legend="y=x^2",line_width=3)p.line(x,y1,legend="y=10^x",line_color="red")p.circle(x,y1,legend="y=10^x",fill_color="red",line_color="red",size=6)p.line(x,y2,legend="y=10^x^2",line_color="orange",line_dash="44")#showoutput_notebook()show(p)7.不同的功能设置创建复选框库选择性显示x=np.linspace(0,4*np.pi,100)#Canvasp=figure()#折线属性props=dict(line_width=4,line_alpha=0.7)#画出3个函数序列l0=p.line(x,np.sin(x),color=Viridis3[0],legend="第0行",**props)l1=p.line(x,4*np.cos(x),color=Viridis3[1],legend="第1行",**props)l2=p.line(x,np.tan(x),color=Viridis3[2],legend="Line2",**props)#复选框激活显示,复选框(checkbox),三个函数序列可以选择性显示checkbox=CheckboxGroup(labels=["Line0","Line1","Line2"],active=[0,1,2],width=100)#checkbox.callback=CustomJS(args=dict(l0=l0,l1=l1,l2=l2,checkbox=checkbox),code="""l0.visible=0incheckbox.active;l1.visible=1incheckbox.active;l2.visible=2incheckbox.active;""")#Addlayerlayout=row(checkbox,p)output_notebook()#Displayshow(layout)_20200316134133.png)8.收盘价时序图趋势和散点图importnumpyasnpfrombokeh.plottingimportfigurefrombokeh.ioimportshow,output_notebookfrombokeh.layoutsimportrow#row()用于将同一张图中的多张图片排成行frombokeh.palettesimportViridis3frombokeh.modelsimportCheckboxGroup,CustomJS#CheckboxGroup创建复选框库#dataaapl=np.array(AAPL['adj_close'])aapl_dates=np.array(AAPL['date'],dtype=np.datetime64)window_size=30window=np.ones(window_size)/float(window_size)aapl_avg=np.卷积(aapl,window,'same')#canvasp=figure(width=800,height=350,x_axis_type="datetime")#layerp.circle(aapl_dates,aapl,size=4,color='darkgrey',alpha=0.2,legend='close')#Scatterplotp.line(aapl_dates,aapl_avg,color='red',legend='avg')#Polylinetimingdiagram#自定义视觉属性p.title.text="AAPLOne-MonthAverage"p.legend.location="top_left"p.grid.grid_line_alpha=0p.xaxis.axis_label='日期'p.yaxis.axis_label='价格'p.ygrid.band_fill_color="gray"p.ygrid.band_fill_alpha=0.1p.legend.click_policy="hide"#点击图例显示隐藏数据#显示结果output_notebook()show(p)_20200316134143.png)