1说明: 1。1Bokeh是专门针对Web浏览器的交互式、可视化Python绘图库。 1。2Bokeh,可以做出像D3。js简洁漂亮的交互可视化效果,但是使用难度低于D3。js。 1。3不需要使用Javascript。 2官网: https:docs。bokeh。orgenlatesthttps:github。combokehbokeh 3安装: pipinstallbokeh本机安装sudopip3。8installbokeh 4环境: 华为笔记本电脑、深度deepinlinux操作系统、python3。8和微软vscode编辑器。 5静态基本作图: 5。1柱状图: 5。1。1代码:frombokeh。ioimportoutputfile,showfrombokeh。plottingimportfigure数据,支持中文fruits〔苹果,Pears,Nectarines,Plums,Grapes,Strawberries〕counts〔5,3,4,2,4,6〕绘图pfigure(xrangefruits,plotheight350,titleFruitCounts,toolbarlocationNone,tools)柱状图,vbar是指垂直柱状图p。vbar(xfruits,topcounts,width0。9)导出文件:文件名和指定路径,注意没有这一行,也会自动在代码所在的生成同名的html文件outputfile(homexgjDesktopbokehbarbasic。html)展示图show(p) 5。1。2图: 5。2折线图 5。2。1代码:frombokeh。ioimportoutputfile,showfrombokeh。plottingimportfigure数据,支持中文fruits〔苹果,Pears,Nectarines,Plums,Grapes,Strawberries〕counts〔5,3,4,2,4,6〕绘图pfigure(xrangefruits,plotheight350,titleFruitCounts,toolbarlocationNone,tools)柱状图p。line(xfruits,ycounts)展示图show(p) 5。2。2图: 5。3散点图: 5。3。1代码:frombokeh。ioimportoutputfile,showfrombokeh。plottingimportfigure,outputfile,show同上数据,支持中文fruits〔苹果,Pears,Nectarines,Plums,Grapes,Strawberries〕counts〔5,3,4,2,4,6〕绘图pfigure(xrangefruits,plotheight350,titleFruitCounts,toolbarlocationNone,tools)柱状图p。scatter(xfruits,ycounts,size20,fillcolor74add1)展示图show(p) 5。3。2图: 基本作图方便,优美;比matplotlib简单,暂时介绍到这里 6高级作图: 6。1jsevents:调用js事件 6。2代码:importnumpyasnpfrombokehimporteventsfrombokeh。ioimportoutputfile,showfrombokeh。layoutsimportcolumn,rowfrombokeh。modelsimportButton,CustomJS,Divfrombokeh。plottingimportfigure定义js和事件defdisplayevent(p,attributes〔〕):stylefloat:clear:fontsize:13pxreturnCustomJS(argsdict(pp),varargs〔〕;for(vari0;iattrs。i){varvalJSON。stringify(cbobj〔attrs〔i〕〕,function(key,val){returnval。toFixed?Number(val。toFixed(2)):})args。push(attrs〔i〕val)}varlinespanstylerbcbobj。eventnameb(args。join(,))vartextp。text。concat(line);varlinestext。split(n)if(lines。length35)lines。shift();p。textlines。join(n);(attributes,style))数据N4000xnp。random。random(sizeN)100ynp。random。random(sizeN)100radiinp。random。random(sizeN)1。5colors〔02x02x02x(int(r),int(g),150)forr,ginzip(502x,302y)〕pfigure(toolspan,wheelzoom,zoomin,zoomout,reset,tap,lassoselect,boxselect)调用散点图p。scatter(x,y,radiusradii,fillcolorcolors,fillalpha0。6,linecolorNone)容器实例化,宽pDiv(width1000)buttonButton(labelButton,buttontypesuccess,width300)layoutcolumn(button,row(p,p))注册事件回调按钮事件button。jsonevent(events。ButtonClick,displayevent(p))LOD事件p。jsonevent(events。LODStart,displayevent(p))p。jsonevent(events。LODEnd,displayevent(p))Pointevents点事件pointattributes〔x,y,sx,sy〕p。jsonevent(events。Tap,displayevent(p,attributespointattributes))p。jsonevent(events。DoubleTap,displayevent(p,attributespointattributes))p。jsonevent(events。Press,displayevent(p,attributespointattributes))p。jsonevent(events。PressUp,displayevent(p,attributespointattributes))Mousewheeleventp。jsonevent(events。MouseWheel,displayevent(p,attributespointattributes〔delta〕))Mousemove,enterandleavep。jsonevent(events。MouseMove,displayevent(p,attributespointattributes))p。jsonevent(events。MouseEnter,displayevent(p,attributespointattributes))p。jsonevent(events。MouseLeave,displayevent(p,attributespointattributes))Paneventspanattributespointattributes〔deltax,deltay〕p。jsonevent(events。Pan,displayevent(p,attributespanattributes))p。jsonevent(events。PanStart,displayevent(p,attributespointattributes))p。jsonevent(events。PanEnd,displayevent(p,attributespointattributes))Pincheventspinchattributespointattributes〔scale〕p。jsonevent(events。Pinch,displayevent(p,attributespinchattributes))p。jsonevent(events。PinchStart,displayevent(p,attributespointattributes))p。jsonevent(events。PinchEnd,displayevent(p,attributespointattributes))Selectioneventsp。jsonevent(events。SelectionGeometry,displayevent(p,attributes〔geometry,final〕))show(layout) 6。3效果图: 6。4图形总体 6。4。1代码:frombokeh。core。enumsimportMarkerTypefrombokeh。layoutsimportrowfrombokeh。modelsimportColumnDataSource,Panel,Tabsfrombokeh。plottingimportfigure,outputfile,showfrombokeh。sampledata。irisimportflowerssourceColumnDataSource(flowers)defmakeplot(title,marker,backend):pfigure(titletitle,plotwidth350,plotheight350,outputbackendbackend)p。scatter(petallength,petalwidth,sourcesource,colorblue,fillalpha0。2,size12,markermarker)returnptabs〔〕formarkerinMarkerType:p1makeplot(marker,marker,canvas)p2makeplot(markerSVG,marker,svg)p3makeplot(markerGL,marker,webgl)tabs。append(Panel(childrow(p1,p2,p3),titlemarker))outputfile(markercompare。html,titleCompareregular,SVG,andWebGLmarkers)show(Tabs(tabstabs)) 6。4。2效果图 一般基本作图是小白和普通人需要的掌握的,下次重点讲 7机器学习:scikitlearnproject 7。1代码:importnumpyasnpfromsklearnimportcluster,datasetsfromsklearn。preprocessingimportStandardScalerfrombokeh。layoutsimportcolumn,rowfrombokeh。plottingimportfigure,outputfile,showprint(Thisexamplemaytakeseveralsecondstorunbeforedisplaying。)print(该示例展示前需要等待几秒。)N50000PLOTSIZE400generatedatasets。np。random。seed(0)noisycirclesdatasets。makecircles(nsamplesN,factor。5,noise。04)noisymoonsdatasets。makemoons(nsamplesN,noise。05)centers〔(2,3),(2,3),(2,3),(2,3)〕blobs1datasets。makeblobs(centerscenters,nsamplesN,clusterstd0。4,randomstate8)blobs2datasets。makeblobs(centerscenters,nsamplesN,clusterstd0。7,randomstate8)colorsnp。array(〔xforxin(00f,0f0,f00,0ff,f0f,ff0)〕)colorsnp。hstack(〔colors〕20)createclusteringalgorithmsdbscancluster。DBSCAN(eps。2)birchcluster。Birch(nclusters2)meanscluster。MiniBatchKMeans(nclusters2)spectralcluster。SpectralClustering(nclusters2,eigensolverarpack,affinitynearestneighbors)affinitycluster。AffinityPropagation(damping。9,preference200)changehere,toselectclusteringalgorithm(note:spectralisslow)algorithmdbscanSELECTALGplots〔〕fordatasetin(noisycircles,noisymoons,blobs1,blobs2):X,ydatasetXStandardScaler()。fittransform(X)predictclustermembershipsalgorithm。fit(X)ifhasattr(algorithm,labels):ypredalgorithm。labels。astype(np。int)else:ypredalgorithm。predict(X)pfigure(outputbackendwebgl,titlealgorithm。class。name,plotwidthPLOTSIZE,plotheightPLOTSIZE)p。scatter(X〔:,0〕,X〔:,1〕,colorcolors〔ypred〕。tolist(),alpha0。1,)plots。append(p)generatelayoutfortheplotslayoutcolumn(row(plots〔:2〕),row(plots〔2:〕))outputfile(clustering。html,titleclusteringwithsklearn)show(layout) 7。2效果图: 自己整理并分享出来 喜欢的就点赞、转发、评论、关注和收藏。