




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、 Python 數(shù)據(jù)可視化編程指南01 前言Python 數(shù)據(jù)可視化利器,Bokeh、pyecharts 的用法,本文增加了 Plotly 的部分教程。02 推薦數(shù)據(jù)可視化的庫(kù)有挺多的,這里推薦幾個(gè)比較常用的:MatplotlibPlotlySeabornGgplotBokehPyechartPygal03 PlotlyPlotly 文檔地址:https:/plot.ly/python/#financial-charts使用方式:Plotly 有 online 和 offline 兩種方式,這里只介紹 offline 的。這是 Plotly 官方教程的一部分importplotly.plotl
2、yaspyimportnumpyasnpdata=dict(visible=False,line=dict(color=#00CED1,width=6),#配置線寬和顏色name=+str(step),x=np.arange(0,10,0.01),#x軸參數(shù)y=np.sin(step*np.arange(0,10,0.01)forstepinnp.arange(0,5,0.1)#y軸參數(shù)data10visible=Truepy.iplot(data,filename=SingleSineWave)只要將最后一行中的py.iplot替換為下面代碼py.offline.plot便可以運(yùn)行。漏斗圖這
3、個(gè)圖代碼太長(zhǎng)了,就不 po 出來(lái)了。Basic Box Plotimportplotly.plotlyimportplotly.graph_objsasgoimportnumpyasnpy0=np.random.randn(50)-1y1=np.random.randn(50)+1trace0=go.Box(y=y0)trace1=go.Box(y=y1)data=trace0,trace1plotly.offline.plot(data)Wind Rose Chartimportplotly.graph_objsasgotrace1=go.Barpolar(r=77.5,72.5,70.0,
4、45.0,22.5,42.5,40.0,62.5,text=North,N-E,East,S-E,South,S-W,West,N-W,name=11-14m/s,marker=dict(color=rgb(106,81,163)trace2=go.Barpolar(r=57.49999999999999,50.0,45.0,35.0,20.0,22.5,37.5,55.00000000000001,text=North,N-E,East,S-E,South,S-W,West,N-W,#鼠標(biāo)浮動(dòng)標(biāo)簽文字描述name=8-11m/s,marker=dict(color=rgb(158,154,2
5、00)trace3=go.Barpolar(r=40.0,30.0,30.0,35.0,7.5,7.5,32.5,40.0,text=North,N-E,East,S-E,South,S-W,West,N-W,name=5-8m/s,marker=dict(color=rgb(203,201,226)trace4=go.Barpolar(r=20.0,7.5,15.0,22.5,2.5,2.5,12.5,22.5,text=North,N-E,East,S-E,South,S-W,West,N-W,name=5m/s,marker=dict(color=rgb(242,240,247)data
6、=trace1,trace2,trace3,trace4layout=go.Layout(title=WindSpeedDistributioninLaurel,NE,font=dict(size=16),legend=dict(font=dict(size=16),radialaxis=dict(ticksuffix=%),orientation=270)fig=go.Figure(data=data,layout=layout)plotly.offline.plot(fig,filename=polar-area-chart)Basic Ternary Plot with Markers篇
7、幅有點(diǎn)長(zhǎng),這里就不 po 代碼了。04 Bokeh這里展示一下常用的圖表和比較搶眼的圖表,詳細(xì)的文檔可查看。條形圖這配色看著還挺舒服的,比 pyecharts 條形圖的配色好看一點(diǎn)。frombokeh.ioimportshow,output_filefrombokeh.modelsimportColumnDataSourcefrombokeh.palettesimportSpectral6frombokeh.plottingimportfigureoutput_file(colormapped_bars.html)#配置輸出文件名fruits=Apples,魅族,OPPO,VIVO,小米,華為
8、#數(shù)據(jù)counts=5,3,4,2,4,6#數(shù)據(jù)source=ColumnDataSource(data=dict(fruits=fruits,counts=counts,color=Spectral6)p=figure(x_range=fruits,y_range=(0,9),plot_height=250,title=FruitCounts,toolbar_location=None,tools=)#條形圖配置項(xiàng)p.vbar(x=fruits,top=counts,width=0.9,color=color,legend=fruits,source=source)p.xgrid.grid_
9、line_color=None#配置網(wǎng)格線顏色p.legend.orientation=horizontal#圖表方向?yàn)樗椒较騪.legend.location=top_centershow(p)#展示圖表年度條形圖可以對(duì)比不同時(shí)間點(diǎn)的量。frombokeh.ioimportshow,output_filefrombokeh.modelsimportColumnDataSource,FactorRangefrombokeh.plottingimportfigureoutput_file(bars.html)#輸出文件名fruits=Apple,魅族,OPPO,VIVO,小米,華為#參數(shù)yea
10、rs=2015,2016,2017#參數(shù)data=fruits:fruits,2015:2,1,4,3,2,4,2016:5,3,3,2,4,6,2017:3,2,4,4,5,3x=(fruit,year)forfruitinfruitsforyearinyearscounts=sum(zip(data2015,data2016,data2017),()source=ColumnDataSource(data=dict(x=x,counts=counts)p=figure(x_range=FactorRange(*x),plot_height=250,title=FruitCountsbyYe
11、ar,toolbar_location=None,tools=)p.vbar(x=x,top=counts,width=0.9,source=source)p.y_range.start=0p.x_range.range_padding=0.1p.xaxis.major_label_orientation=1p.xgrid.grid_line_color=Noneshow(p)餅圖fromcollectionsimportCounterfrommathimportpiimportpandasaspdfrombokeh.ioimportoutput_file,showfrombokeh.pale
12、ttesimportCategory20cfrombokeh.plottingimportfigurefrombokeh.transformimportcumsumoutput_file(pie.html)x=Counter(中國(guó):157,美國(guó):93,日本:89,巴西:63,德國(guó):44,印度:42,意大利:40,澳大利亞:35,法國(guó):31,西班牙:29)data=pd.DataFrame.from_dict(dict(x),orient=index).reset_index().rename(index=str,columns=0:value,index:country)dataangle=d
13、atavalue/sum(x.values()*2*pidatacolor=Category20clen(x)p=figure(plot_height=350,title=PieChart,toolbar_location=None,tools=hover,tooltips=country:value)p.wedge(x=0,y=1,radius=0.4,start_angle=cumsum(angle,include_zero=True),end_angle=cumsum(angle),line_color=white,fill_color=color,legend=country,sour
14、ce=data)p.axis.axis_label=Nonep.axis.visible=Falsep.grid.grid_line_color=Noneshow(p)條形圖年度水果進(jìn)出口frombokeh.ioimportoutput_file,showfrombokeh.modelsimportColumnDataSourcefrombokeh.palettesimportGnBu3,OrRd3frombokeh.plottingimportfigureoutput_file(stacked_split.html)fruits=Apples,Pears,Nectarines,Plums,G
15、rapes,Strawberriesyears=2015,2016,2017exports=fruits:fruits,2015:2,1,4,3,2,4,2016:5,3,4,2,4,6,2017:3,2,4,4,5,3imports=fruits:fruits,2015:-1,0,-1,-3,-2,-1,2016:-2,-1,-3,-1,-2,-2,2017:-1,-2,-1,0,-2,-2p=figure(y_range=fruits,plot_height=250,x_range=(-16,16),title=Fruitimport/export,byyear,toolbar_locat
16、ion=None)p.hbar_stack(years,y=fruits,height=0.9,color=GnBu3,source=ColumnDataSource(exports),legend=%sexports%xforxinyears)p.hbar_stack(years,y=fruits,height=0.9,color=OrRd3,source=ColumnDataSource(imports),legend=%simports%xforxinyears)p.y_range.range_padding=0.1p.ygrid.grid_line_color=Nonep.legend
17、.location=top_leftp.axis.minor_tick_line_color=Nonep.outline_line_color=Noneshow(p)散點(diǎn)圖frombokeh.plottingimportfigure,output_file,showoutput_file(line.html)p=figure(plot_width=400,plot_height=400)p.circle(1,2,3,4,5,6,7,2,4,5,size=20,color=navy,alpha=0.5)show(p)六邊形圖這兩天,馬蜂窩剛被發(fā)現(xiàn)數(shù)據(jù)造假,這不,與馬蜂窩應(yīng)應(yīng)景。importnum
18、pyasnpfrombokeh.ioimportoutput_file,showfrombokeh.plottingimportfigurefrombokeh.util.heximportaxial_to_cartesianoutput_file(hex_coords.html)q=np.array(0,0,0,-1,-1,1,1)r=np.array(0,-1,1,0,1,-1,0)p=figure(plot_width=400,plot_height=400,toolbar_location=None)#p.grid.visible=False#配置網(wǎng)格是否可見(jiàn)p.hex_tile(q,r
19、,size=1,fill_color=firebrick*3+navy*4,line_color=white,alpha=0.5)x,y=axial_to_cartesian(q,r,1,pointytop)p.text(x,y,text=(%d,%d)%(q,r)for(q,r)inzip(q,r),text_baseline=middle,text_align=center)show(p)環(huán)比條形圖這個(gè)實(shí)現(xiàn)挺厲害的,看了一眼就吸引了我。我在代碼中都做了一些注釋?zhuān)M麑?duì)你理解有幫助。注:圓心為正中央,即直角坐標(biāo)系中標(biāo)簽為(0,0)的地方。fromcollectionsimportOrdere
20、dDictfrommathimportlog,sqrtimportnumpyasnpimportpandasaspdfromsix.movesimportcStringIOasStringIOfrombokeh.plottingimportfigure,show,output_fileantibiotics=bacteria,penicillin,streptomycin,neomycin,gram結(jié)核分枝桿菌,800,5,2,negative沙門(mén)氏菌,10,0.8,0.09,negative變形桿菌,3,0.1,0.1,negative肺炎克雷伯氏菌,850,1.2,1,negative布魯
21、氏菌,1,2,0.02,negative銅綠假單胞菌,850,2,0.4,negative大腸桿菌,100,0.4,0.1,negative產(chǎn)氣桿菌,870,1,1.6,negative白色葡萄球菌,0.007,0.1,0.001,positive溶血性鏈球菌,0.001,14,10,positive草綠色鏈球菌,0.005,10,40,positive肺炎雙球菌,0.005,11,10,positivedrug_color=OrderedDict(#配置中間標(biāo)簽名稱(chēng)與顏色(盤(pán)尼西林,#0d3362),(鏈霉素,#c64737),(新霉素,black),)gram_color=positive
22、:#aeaeb8,negative:#e69584,#讀取數(shù)據(jù)df=pd.read_csv(StringIO(antibiotics),skiprows=1,skipinitialspace=True,engine=python)width=800height=800inner_radius=90outer_radius=300-10minr=sqrt(log(.001*1E4)maxr=sqrt(log(1000*1E4)a=(outer_radius-inner_radius)/(minr-maxr)b=inner_radius-a*maxrdefrad(mic):returna*np.s
23、qrt(np.log(mic*1E4)+bbig_angle=2.0*np.pi/(len(df)+1)small_angle=big_angle/7#整體配置p=figure(plot_width=width,plot_height=height,title=,x_axis_type=None,y_axis_type=None,x_range=(-420,420),y_range=(-420,420),min_border=0,outline_line_color=black,background_fill_color=#f0e1d2)p.xgrid.grid_line_color=None
24、p.ygrid.grid_line_color=None#annularwedgesangles=np.pi/2-big_angle/2-df.index.to_series()*big_angle#計(jì)算角度colors=gram_colorgramforgramindf.gram#配置顏色p.annular_wedge(0,0,inner_radius,outer_radius,-big_angle+angles,angles,color=colors,)#smallwedgesp.annular_wedge(0,0,inner_radius,rad(df.penicillin),-big_
25、angle+angles+5*small_angle,-big_angle+angles+6*small_angle,color=drug_color盤(pán)尼西林)p.annular_wedge(0,0,inner_radius,rad(df.streptomycin),-big_angle+angles+3*small_angle,-big_angle+angles+4*small_angle,color=drug_color鏈霉素)p.annular_wedge(0,0,inner_radius,rad(df.neomycin),-big_angle+angles+1*small_angle,
26、-big_angle+angles+2*small_angle,color=drug_color新霉素)#繪制大圓和標(biāo)簽labels=np.power(10.0,np.arange(-3,4)radii=a*np.sqrt(np.log(labels*1E4)+bp.circle(0,0,radius=radii,fill_color=None,line_color=white)p.text(0,radii:-1,str(r)forrinlabels:-1,text_font_size=8pt,text_align=center,text_baseline=middle)#半徑p.annula
27、r_wedge(0,0,inner_radius-10,outer_radius+10,-big_angle+angles,-big_angle+angles,color=black)#細(xì)菌標(biāo)簽xr=radii0*np.cos(np.array(-big_angle/2+angles)yr=radii0*np.sin(np.array(-big_angle/2+angles)label_angle=np.array(-big_angle/2+angles)label_anglelabel_angle-np.pi/2+=np.pi#easiertoreadlabelsontheleftside#
28、繪制各個(gè)細(xì)菌的名字p.text(xr,yr,df.bacteria,angle=label_angle,text_font_size=9pt,text_align=center,text_baseline=middle)#繪制圓形,其中數(shù)字分別為x軸與y軸標(biāo)簽p.circle(-40,-40,-370,-390,color=list(gram_color.values(),radius=5)#繪制文字p.text(-30,-30,-370,-390,text=Gram-+grforgringram_color.keys(),text_font_size=7pt,text_align=left,
29、text_baseline=middle)#繪制矩形,中間標(biāo)簽部分。其中-40,-40,-40為三個(gè)矩形的x軸坐標(biāo)。18,0,-18為三個(gè)矩形的y軸坐標(biāo)p.rect(-40,-40,-40,18,0,-18,width=30,height=13,color=list(drug_color.values()#配置中間標(biāo)簽文字、文字大小、文字對(duì)齊方式p.text(-15,-15,-15,18,0,-18,text=list(drug_color),text_font_size=9pt,text_align=left,text_baseline=middle)output_file(burtin.h
30、tml,title=burtin.pyexample)show(p)元素周期表元素周期表,這個(gè)實(shí)現(xiàn)好牛逼啊,距離初三剛開(kāi)始學(xué)化學(xué)已經(jīng)很遙遠(yuǎn)了,想當(dāng)年我還是化學(xué)課代表呢!由于基本用不到化學(xué)了,這里就不實(shí)現(xiàn)了。真實(shí)狀態(tài)05 Pyechartspyecharts 也是一個(gè)比較常用的數(shù)據(jù)可視化庫(kù),用得也是比較多的了,是百度 Echarts 庫(kù)的 Python 支持。這里也展示一下常用的圖表。文檔地址為:/#/zh-cn/prepare?id=%E5%AE%89%E8%A3%85-pyecharts條形圖frompyechartsimportBarbar=Bar(我的第一個(gè)圖表,這里是副標(biāo)題)bar.a
31、dd(服裝,襯衫,羊毛衫,雪紡衫,褲子,高跟鞋,襪子,5,20,36,10,75,90)#bar.print_echarts_options()#該行只為了打印配置項(xiàng),方便調(diào)試時(shí)使用bar.render()#生成本地HTML文件散點(diǎn)圖frompyechartsimportPolarimportrandomdata_1=(10,random.randint(1,100)foriinrange(300)data_2=(11,random.randint(1,100)foriinrange(300)polar=Polar(極坐標(biāo)系-散點(diǎn)圖示例,width=1200,height=600)polar.
32、add(,data_1,type=scatter)polar.add(,data_2,type=scatter)polar.render()餅圖importrandomfrompyechartsimportPieattr=A,B,C,D,E,Fpie=Pie(餅圖示例,width=1000,height=600)pie.add(,attr,random.randint(0,100)for_inrange(6),radius=50,55,center=25,50,is_random=True,)pie.add(,attr,random.randint(20,100)for_inrange(6),
33、radius=0,45,center=25,50,rosetype=area,)pie.add(,attr,random.randint(0,100)for_inrange(6),radius=50,55,center=65,50,is_random=True,)pie.add(,attr,random.randint(20,100)for_inrange(6),radius=0,45,center=65,50,rosetype=radius,)pie.render()詞云這個(gè)是我在前面的文章中用到的圖片實(shí)例,這里就不 po 具體數(shù)據(jù)了。frompyechartsimportWordCloudname=SamSClub#詞條value=10000#權(quán)重wordcloud=WordCloud(width=1300,height=620)wordcloud.add(,name,value,word_size_range=20,100)wordcloud.r
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 用智慧譜寫(xiě)幼兒園發(fā)展新篇章計(jì)劃
- 重大建設(shè)項(xiàng)目的安全檢查計(jì)劃
- 2025年貓爬架項(xiàng)目發(fā)展計(jì)劃
- 2025年板臥式電除塵器項(xiàng)目合作計(jì)劃書(shū)
- 2025年密封用填料及類(lèi)似品項(xiàng)目建議書(shū)
- 實(shí)施均衡發(fā)展的人口政策
- 醫(yī)療健康管理服務(wù)協(xié)議
- 藝術(shù)品交易與展示項(xiàng)目投資合同
- 擔(dān)保期權(quán)合同
- 西游記中的人物形象賞析與解讀
- 人教版(2024新版)七年級(jí)上冊(cè)英語(yǔ)各單元重點(diǎn)語(yǔ)法知識(shí)點(diǎn)講義
- 安全閥校驗(yàn)標(biāo)準(zhǔn)
- 耳穴壓豆課件
- 建筑制圖與識(shí)圖教學(xué)課件:第八章 結(jié)構(gòu)施工圖
- 湘教版三年級(jí)美術(shù)下冊(cè)教案全冊(cè)
- (高清版)DB15∕T 3585-2024 高標(biāo)準(zhǔn)農(nóng)田施工質(zhì)量評(píng)定規(guī)程
- 試油(氣)HSE作業(yè)指導(dǎo)書(shū)
- 重癥監(jiān)護(hù)-ICU的設(shè)置、管理與常用監(jiān)測(cè)技術(shù)
- 法律顧問(wèn)服務(wù)投標(biāo)方案(完整技術(shù)標(biāo))
- 中醫(yī)藥三方合作協(xié)議書(shū)范本
- 2024年《動(dòng)漫藝術(shù)概論》自考復(fù)習(xí)題庫(kù)(附答案)
評(píng)論
0/150
提交評(píng)論