PythonStreamlit库再学习st。slider和数
1说明:
1。1是一个开源Python库,可轻松构建用于机器学习的漂亮应用程序。
1。2我的第一篇文章,对其介绍、安装、初步相关使用做了详细的介绍:
《Streamlit是python的一个机器学习、数据科学、应用开发框架》
1。3纠正末尾gif的st。slider图:是下面的。
2数据可视化作图:
2。1Streamlit当前支持许多用于绘图的库,其中就有Plotly,Bokeh,Matplotlib,Altair和Vega图表。
2。2plotly法:
2。2。1代码:importstreamlitasstst。title(数据可视化作图)st。header(折线图)导入plotly并作图importplotly。graphobjsasgotrace0go。Scatter(x〔1,2,3,4〕,y〔10,15,13,17〕)trace1go。Scatter(x〔1,2,3,4〕,y〔16,5,11,9〕)data〔trace0,trace1〕写入数据并显示图st。write(data)用st。write
2。2。2效果图:
2。2。3代码:3Dimportstreamlitasstimportplotly。graphobjsasgoimportnumpyasnp显示标题st。header(3Dplot)随机生成散点坐标轴x,y,znp。random。multivariatenormal(np。array(〔0,0,0〕),np。eye(3),400)。transpose()trace1go。Scatter3d(xx,yy,zz,modemarkers,markerdict(size12,colorz,setcolortoanarraylistofdesiredvaluescolorscaleViridis,chooseacolorscaleopacity0。8,),)data〔trace1〕layoutgo。Layout(margindict(l0,r0,b0,t0))figgo。Figure(datadata,layoutlayout)st。write(fig)streamlitrun6plotly3D。py
2。2。4操作效果图:
2。3matplotlib法
2。3。1代码:importstreamlitasstst。title(数据可视化作图)st。header(matplotlibScatter)importmatplotlib。pyplotaspltfplt。figure()附加学习python的列表及列表推导式x〔xforxinrange(9)〕x1x〔1:〕x1〔1,2,3,4,5,6,7,8〕等于上面2个y〔3,6,2,7,4,8,5,3〕plt。scatter(x1,y)st。plotlychart(f)老版matplotlib的格式st。write(f)新版matplotlib
2。3。2操作和效果图:
2。4altair法
2。4。1代码:importstreamlitasstimportaltairasaltfromvegadatasetsimportdatasourcedata。cars()设置刷子,交互性brushalt。selection(typeinterval)pointsalt。Chart()。markpoint()。encode(xHorsepower:Q,yMilesperGallon:Q,coloralt。condition(brush,Origin:N,alt。value(lightgray)))。addselection(brush)barsalt。Chart()。markbar()。encode(yOrigin:N,colorOrigin:N,xcount(Origin):Q)。transformfilter(brush)把点图和柱状图挂在一起bbbalt。vconcat(points,bars,datasource)一起启动服务器bbb。serve(0。0。0。0,8888)用altair启动服务器自动浏览器打开st。write(bbb)streamlitrun7altair。py用终端启动文件,没有上面有优势
2。4。2操作效果图:
3st。fileuploader和st。button
3。1代码:importstreamlitasstimportpandasaspduploadedfilest。fileuploader(ChooseaCSVfile,typecsv)ifuploadedfileisnotNone:datapd。readcsv(uploadedfile)ifst。button(Sayhello):st。write(Whyhellothere)else:st。write(Goodbye)
3。2图
基础知识基本讲解完成
感兴趣的可以开发自己的app