喜歡用 Python 做項(xiàng)目的小伙伴不免會(huì)遇到這種情況:做圖表時(shí),用哪種好看又實(shí)用的可視化工具包呢? 之前文章里出現(xiàn)過(guò)漂亮的圖表時(shí),也總有讀者在后臺(tái)留言問(wèn)該圖表時(shí)用什么工具做的。 下面,作者介紹了八種在 Python 中實(shí)現(xiàn)的可視化工具包,其中有些包還能用在其它語(yǔ)言中。快來(lái)試試你喜歡哪個(gè)? 用 Python 創(chuàng)建圖形的方法有很多,但是哪種方法是最好的呢?當(dāng)我們做可視化之前,要先明確一些關(guān)于圖像目標(biāo)的問(wèn)題:你是想初步了解數(shù)據(jù)的分布情況?想展示時(shí)給人們留下深刻印象?也許你想給某人展示一個(gè)內(nèi)在的形象,一個(gè)中庸的形象? 本文將介紹一些常用的 Python 可視化包,包括這些包的優(yōu)缺點(diǎn)以及分別適用于什么樣的場(chǎng)景。這篇文章只擴(kuò)展到 2D 圖,為下一次講 3D 圖和商業(yè)報(bào)表(dashboard)留了一些空間,不過(guò)這次要講的包中,許多都可以很好地支持 3D 圖和商業(yè)報(bào)表。 Matplotlib、Seaborn 和 Pandas 把這三個(gè)包放在一起有幾個(gè)原因:首先 Seaborn 和 Pandas 是建立在 Matplotlib 之上的,當(dāng)你在用 Seaborn 或 Pandas 中的 df.plot() 時(shí),用的其實(shí)是別人用 Matplotlib 寫(xiě)的代碼。因此,這些圖在美化方面是相似的,自定義圖時(shí)用的語(yǔ)法也都非常相似。 當(dāng)提到這些可視化工具時(shí),我想到三個(gè)詞:探索(Exploratory)、數(shù)據(jù)(Data)、分析(Analysis)。這些包都很適合第一次探索數(shù)據(jù),但要做演示時(shí)用這些包就不夠了。 Matplotlib 是比較低級(jí)的庫(kù),但它所支持的自定義程度令人難以置信(所以不要簡(jiǎn)單地將其排除在演示所用的包之外!),但還有其它更適合做展示的工具。 Matplotlib 還可以選擇樣式(style selection),它模擬了像 ggplot2 和 xkcd 等很流行的美化工具。下面是我用 Matplotlib 及相關(guān)工具所做的示例圖: 在處理籃球隊(duì)薪資數(shù)據(jù)時(shí),我想找出薪資中位數(shù)最高的團(tuán)隊(duì)。為了展示結(jié)果,我將每個(gè)球隊(duì)的工資用顏色標(biāo)成條形圖,來(lái)說(shuō)明球員加入哪一支球隊(duì)才能獲得更好的待遇。
importseabornassns importmatplotlib.pyplotasplt color_order=['xkcd:cerulean','xkcd:ocean', 'xkcd:black','xkcd:royalpurple', 'xkcd:royalpurple','xkcd:navyblue', 'xkcd:powderblue','xkcd:lightmaroon', 'xkcd:lightishblue','xkcd:navy'] sns.barplot(x=top10.Team, y=top10.Salary, palette=color_order).set_title('TeamswithHighestMedianSalary') plt.ticklabel_format(style='sci',axis='y',scilimits=(0,0))
importmatplotlib.pyplotasplt importscipy.statsasstats #model2isaregressionmodel log_resid=model2.predict(X_test)-y_test stats.probplot(log_resid,dist="norm",plot=plt) plt.title("NormalQ-Qplot") plt.show()
#AllSalaries ggplot(data=df,aes(x=season_start,y=salary,colour=team))+ geom_point()+ theme(legend.position="none")+ labs(title='SalaryOverTime',x='Year',y='Salary($)')
importpandasaspd frombokeh.plottingimportfigure frombokeh.ioimportshow #is_mascisaone-hotencodeddataframeofresponsestothequestion: #"Doyouidentifyasmasculine?" #DataframePrep counts=is_masc.sum() resps=is_masc.columns #Bokeh p2=figure(title='DoYouViewYourselfAsMasculine?', x_axis_label='Response', y_axis_label='Count', x_range=list(resps)) p2.vbar(x=resps,top=counts,width=0.6,fill_color='red',line_color='black') show(p2) #Pandas counts.plot(kind='bar')
用 Bokeh 表示調(diào)查結(jié)果
-
安裝時(shí)要有 API 秘鑰,還要注冊(cè),不是只用 pip 安裝就可以;
-
Plotly 所繪制的數(shù)據(jù)和布局對(duì)象是獨(dú)一無(wú)二的,但并不直觀;
-
圖片布局對(duì)我來(lái)說(shuō)沒(méi)有用(40 行代碼毫無(wú)意義?。?/span>
-
你可以在 Plotly 網(wǎng)站和 Python 環(huán)境中編輯圖片;
-
支持交互式圖片和商業(yè)報(bào)表;
-
Plotly 與 Mapbox 合作,可以自定義地圖;
-
很有潛力繪制優(yōu)秀圖形。
#plot1-barplot #**note**-thelayoutlinesdonothingandtripnoerrors data=[go.Bar(x=team_ave_df.team, y=team_ave_df.turnovers_per_mp)] layout=go.Layout( title=go.layout.Title( text='TurnoversperMinutebyTeam', xref='paper', x=0 ), xaxis=go.layout.XAxis( title=go.layout.xaxis.Title( text='Team', font=dict( family='CourierNew,monospace', size=18, color='#7f7f7f' ) ) ), yaxis=go.layout.YAxis( title=go.layout.yaxis.Title( text='AverageTurnovers/Minute', font=dict( family='CourierNew,monospace', size=18, color='#7f7f7f' ) ) ), autosize=True, hovermode='closest') py.iplot(figure_or_data=data,layout=layout,filename='jupyter-plot',sharing='public',fileopt='overwrite') #plot2-attemptatascatterplot data=[go.Scatter(x=player_year.minutes_played, y=player_year.salary, marker=go.scatter.Marker(color='red', size=3))] layout=go.Layout(title="test", xaxis=dict(title='why'), yaxis=dict(title='plotly')) py.iplot(figure_or_data=data,layout=layout,filename='jupyter-plot2',sharing='public') [Image:image.png]
-
實(shí)例化圖片;
-
用圖片目標(biāo)屬性格式化;
-
用 figure.add() 將數(shù)據(jù)添加到圖片中。
options={ 'node_color':range(len(G)), 'node_size':300, 'width':1, 'with_labels':False, 'cmap':plt.cm.coolwarm } nx.draw(G,**options)
importitertools importnetworkxasnx importmatplotlib.pyplotasplt f=open('data/facebook/1684.circles','r') circles=[line.split()forlineinf] f.close() network=[] forcircincircles: cleaned=[int(val)forvalincirc[1:]] network.append(cleaned) G=nx.Graph() forvinnetwork: G.add_nodes_from(v) edges=[itertools.combinations(net,2)fornetinnetwork] foredge_groupinedges: G.add_edges_from(edge_group) options={ 'node_color':'lime', 'node_size':3, 'width':1, 'with_labels':False, } nx.draw(G,**options)
-
python
+關(guān)注
關(guān)注
56文章
4827瀏覽量
86779 -
工具包
+關(guān)注
關(guān)注
0文章
47瀏覽量
9716 -
數(shù)據(jù)可視化
+關(guān)注
關(guān)注
0文章
476瀏覽量
10804
原文標(biāo)題:8 個(gè)流行的 Python 可視化工具包,你喜歡哪個(gè)?
文章出處:【微信號(hào):DBDevs,微信公眾號(hào):數(shù)據(jù)分析與開(kāi)發(fā)】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
七款經(jīng)久不衰的數(shù)據(jù)可視化工具!
Python人工智能學(xué)習(xí)工具包+入門(mén)與實(shí)踐資料集錦
從使用效果來(lái)看,數(shù)據(jù)可視化工具離不開(kāi)數(shù)據(jù)中臺(tái)嗎?
能做數(shù)據(jù)治理的數(shù)據(jù)可視化工具,又快又靈活
這樣選數(shù)據(jù)可視化工具,更能選到適用的
六種Python的數(shù)據(jù)可視化包和工具比較

15個(gè)最實(shí)用的數(shù)據(jù)可視化工具

mongodb可視化工具如何使用_介紹一款好用 mongodb 可視化工具

關(guān)于8個(gè)流行的Python可視化工具包

建議收藏的20款實(shí)用的數(shù)據(jù)可視化工具
數(shù)據(jù)可視化工具的圖表主要分為哪些
幾款好用的可視化工具推薦

一鍵生成可視化圖表/大屏 這13款數(shù)據(jù)可視化工具很強(qiáng)大

評(píng)論