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

python可视化分析全国疫情

时间:2023-03-26 18:25:48 Python

很长一段时间,python下都是用matplotlib制作图片。优点是使用广泛,文档容易查找,但制作出来的图片略显丑陋。pyecharts是百度基于python的echarts实现,直接在python中可以很方便的调用。它的输出非常漂亮。但由于版本原因,部分方法可能与旧版本有所不同。一、安装我这里使用的python版本是python3.7,pyecharts也是最新的。使用pip安装比较简单。pipinstallpyechartspipinstallpyecharts-jupyter-installer注意第二个包是集成jupyter的,方便直接在页面上画图。如果没有安装第二个包或者参数不对,会出现如下错误:Javascripterroraddingoutput!ReferenceError:echartsisnotdefined有关详细信息,请参阅浏览器Javascript控制台。此外,国内地图与之前的版本不同。单独分开,如果需要使用,需要使用如下命令安装:pypkgpipinstallecharts-china-misc-pypkgpipinstallecharts-united-kingdom-pypkg安装完成后可以使用pipshowpyecharts查看安装位置。2.Jupyter运行并输出图。我这里使用的jupyter是在页面上直接调用使用的。测试代码如下:frompyecharts.chartsimportBarfrompyechartsimportoptionsasoptsbar=(Bar().add_xaxis(["shirt","sweater","tie","pants","windbreaker","highheels"","袜子"]).add_yaxis("商人A",[114,55,27,101,125,27,105]).add_yaxis("商人B",[57,134,137,129,145,60,49]).set_global_opts(title_opts=opts.TitleOpts(title="某商场销售情况")))bar.render_notebook()#bar.render()#使用render方法会生成一个html页面并运行它直接在页面上。结果如下:另外,jupyter页面无法显示图片,除了上面提到的类名外,可能是缺少html5lib包。还需要注意的是,jupter和jupterlab是两个不同的产品,后者是jupter的未来版本。引入时使用的代码不同。详情参见官方示例。Python可视化分析全国疫情代码frompyechartsimportoptionsasoptsfrompyecharts.chartsimportGeofrompyecharts.globalsimportChartType,SymbolTypeimportrequestsimportjsonres=json.loads(requests.get('http://www.dzyong.top:3005/yiqing/province/').text)print(res)province=[p['provinceName']forpinres['data']]val1=[p['confirmedNum']forpinres['data']]val2=[p['curesNum']forpinres['data']]val3=[p['deathsNum']forpinres['data']]geo=Geo()geo.add_schema(maptype="china")geo.add("geo",[list(z)forzinzip(province,val1)],type_=ChartType.EFFECT_SCATTER,)geo.set_series_opts(label_opts=opts.LabelOpts(is_show=False,background_color='black',color='green'))geo.set_global_opts(visualmap_opts=opts.VisualMapOpts(is_piecewise=True,min_=0,max_=30000),title_opts=opts.TitleOpts(title="全国实时data"))geo.render('全国实时数据.html')以上就是本次分享的全部内容。现在想学编程的可以去Python技术大本营指导一下。欢迎大家~