1说明: 1。1MPLD3mpld3matplotlibD3js。 1。2是基于当前python下面最流行的图形图像库matplotlib和最知名的javascript库D3js上开发得到的。 1。3用于创建web交互式数据可视化。 1。4mpld3BringingMatplotlibtotheBrowser。 2准备: 2。1官网:https:pypi。orgprojectmpld3https:github。commpld3mpld3http:mpld3。github。io很慢很卡,我没上去 2。2安装:pipinstallmpld3其实不用安装,新版的matplotlib自带的 2。3环境: 华为笔记本电脑、深度deepinlinux操作系统、谷歌浏览器、python3。8和微软vscode编辑器。 3折线图: 3。1代码:折线图从模块导入就知道,mpld3是matplotlib自带的importmatplotlib。pyplotasplt,mpld3importmpld3也可以这样导出这是图片大小设置,建议这样设置大小,默认太大不好看figplt。figure(figsize(3,2))figplt。figure(num1,figsize(3,2))datax〔3,1,4,1,5〕plt。plot(〔3,1,4,1,5〕,ks,mecw,mew5,ms20)r。d代表red。p红色。。。菱形cdplt。plot(datax,r。d,mecw,mew5,ms20)mpld3。show() 3。2操作和效果图: 4图和图例的交互式: 4。1代码:importmatplotlib。pyplotasplt,mpld3importnumpyasnpimportpandasaspdimportmpld3这种导出注释掉,放在matplotlib的第一行那样导出frommpld3importplugins数据np。random。seed(9615)N100支持中文dfpd。DataFrame((。1(np。random。random((N,5))。5))。cumsum(0),columns〔a组,b组,c组,d组,e组〕,)折线图和交互式组合plotlineconfidenceintervalfig,axplt。subplots(figsize(3,2))等价于下面2行figplt。figure(figsize(3,2))axfig。addsubplot(1,1,1)以上这样设置图片大小成功ax。grid(True,alpha0。3)forkey,valindf。iteritems():l,ax。plot(val。index,val。values,labelkey)ax。fillbetween(val。index,val。values。5,val。values1。5,colorl。getcolor(),alpha。4)defineinteractivelegendhandles,labelsax。getlegendhandleslabels()returnlinesandlabelsinteractivelegendplugins。InteractiveLegendPlugin(zip(handles,ax。collections),labels,alphaunsel0。5,alphaover1。5,startvisibleTrue)图片和图例的交互式插件的联系设置plugins。connect(fig,interactivelegend)ax。setxlabel(x)ax。setylabel(y)图的标题名和大小ax。settitle(Interactivelegend,size20)mpld3。show() 4。2图: 4。3小插曲: 这里中文是支持的,这个和我曾经的matplotlib中文支持有点不一样,可以参考我的另外一篇文章,里面有提到。 《NetworkX:是一个简单、多样化、能快速生成网络图形的Python库》,这篇文章的第6点:中文显示,简单且一劳永逸。 5散点图: 5。1代码:Scatter图importmatplotlib。pyplotasplt,mpld3importnumpyasnp方法一,facecolor背景颜色fig,axplt。subplots(subplotkwdict(facecolorEEEEEE),figsize(3,2))fig,axplt。subplots(subplotkwdict(facecolorlightpink),figsize(3,2))fig,axplt。subplots()采用默认方法二figplt。figure(figsize(3,2))axfig。addsubplot(1,1,1)方法三fig,axplt。subplots(1,1,figsize(3,2))N100scatterax。scatter(np。random。normal(sizeN),np。random。normal(sizeN),cnp。random。random(sizeN),s1000np。random。random(sizeN),alpha0。3,cmapplt。cm。jet)网格设置ax。grid(colorwhite,linestylesolid)图标标题名ax。settitle(ScatterPlot(withtooltips!),size20)labels〔point{0}。format(i1)foriinrange(N)〕显示鼠标小提示tooltipmpld3。plugins。PointLabelTooltip(scatter,labelslabels)mpld3。plugins。connect(fig,tooltip)mpld3。show() 5。2图: 6面积曲线图: 6。1代码:importmatplotlib。pyplotasplt,mpld3importnumpyasnpxnp。linspace(0,10,200)dataobj{x:x,y1:2x1,y2:3x1。2,mean:0。5xnp。cos(2x)2。5x1。1}fig,axplt。subplots()默认大小fig,axplt。subplots(1,1,figsize(3,2))填充两条线之间的颜色ax。fillbetween(x,y1,y2,coloryellow,datadataobj)ax。plot(x,mean,colorblack,datadataobj)mpld3。show() 6。2图: 7拖拽drag: 7。1结合d3。js的JavaScript的代码:importnumpyasnpimportmatplotlib。pyplotaspltimportmatplotlibasmplimportmpld3frommpld3importplugins,utilsclassDragPlugin(plugins。PluginBase):JAVASCRIPTmpld3。registerplugin(drag,DragPlugin);DragPlugin。prototypeObject。create(mpld3。Plugin。prototype);DragPlugin。prototype。constructorDragPDragPlugin。prototype。requiredProps〔id〕;DragPlugin。prototype。defaultProps{}functionDragPlugin(fig,props){mpld3。Plugin。call(this,fig,props);mpld3。insertcss(fig。figidpath。dragging,{fillopacity:1。0!important,strokeopacity:1。0!important});};DragPlugin。prototype。drawfunction(){varobjmpld3。getelement(this。props。id);vardragd3。drag()。subject(function(d){return{x:obj。ax。x(d〔0〕),y:obj。ax。y(d〔1〕)};})。on(start,dragstarted)。on(drag,dragged)。on(end,dragended);obj。elements()。data(obj。offsets)。style(cursor,default)。call(drag);functiondragstarted(d){d3。event。sourceEvent。stopPropagation();d3。select(this)。classed(dragging,true);}functiondragged(d,i){d〔0〕obj。ax。x。invert(d3。event。x);d〔1〕obj。ax。y。invert(d3。event。y);d3。select(this)。attr(transform,translate(〔d3。event。x,d3。event。y〕));}functiondragended(d){d3。select(this)。classed(dragging,false);}}definit(self,points):ifisinstance(points,mpl。lines。Line2D):suffixptselse:suffixNoneself。dict{type:drag,id:utils。getid(points,suffix)}fig,axplt。subplots(1,1,figsize(3,2))np。random。seed(0)pointsax。plot(np。random。normal(size20),np。random。normal(size20),or,alpha0。5,markersize50,markeredgewidth1)ax。settitle(ClickandDrag,fontsize18)plugins。connect(fig,DragPlugin(points〔0〕))mpld3。show() 7。2效果图: 8再附一张图,代码省略: 自己整理并分享出来 喜欢的人,请点赞、关注、评论、转发和收藏。