自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

再見,可視化!你好,Pandas!

開發(fā) 后端
大家在用Python做數(shù)據(jù)分析時,正常的做法是用先pandas先進行數(shù)據(jù)處理,然后再用Matplotlib、Seaborn、Plotly、Bokeh等對dataframe或者series進行可視化操作。

用Python做數(shù)據(jù)分析離不開pandas,pnadas更多的承載著處理和變換數(shù)據(jù)的角色,pands中也內(nèi)置了可視化的操作,但效果很糙。

因此,大家在用Python做數(shù)據(jù)分析時,正常的做法是用先pandas先進行數(shù)據(jù)處理,然后再用Matplotlib、Seaborn、Plotly、Bokeh等對dataframe或者series進行可視化操作。

但是說實話,每個可視化包都有自己獨特的方法和函數(shù),經(jīng)常忘,這是讓我一直很頭疼的地方。

好消息來了!從最新的pandas版本0.25.3開始,不再需要上面的操作了,數(shù)據(jù)處理和可視化完全可以用pandas一個就全部搞定。

pandas現(xiàn)在可以使用Plotly、Bokeh作為可視化的backend,直接實現(xiàn)交互性操作,無需再單獨使用可視化包了。

下面我們一起看看如何使用。

1. 激活backend

在import了pandas之后,直接使用下面這段代碼激活backend,比如下面要激活plotly。 

  1. pd.options.plotting.backend = 'plotly' 

目前,pandas的backend支持以下幾個可視化包。

  •  Plotly
  •  Holoviews
  •  Matplotlib
  •  Pandas_bokeh
  •  Hyplot

2. Plotly backend

Plotly的好處是,它基于Javascript版本的庫寫出來的,因此生成的Web可視化圖表,可以顯示為HTML文件或嵌入基于Python的Web應(yīng)用程序中。

下面看下如何用plotly作為pandas的backend進行可視化。

如果還沒安裝Plotly,則需要安裝它pip intsall plotly。如果是在Jupyterlab中使用Plotly,那還需要執(zhí)行幾個額外的安裝步驟來顯示可視化效果。

首先,安裝IPywidgets。 

  1. pip install jupyterlab "ipywidgets>=7.5" 

然后運行此命令以安裝Plotly擴展。 

  1. jupyter labextension install jupyterlab-plotly@4.8.1 

示例選自openml.org的的數(shù)據(jù)集,鏈接如下:

數(shù)據(jù)鏈接:https://www.openml.org/d/187

這個數(shù)據(jù)也是Scikit-learn中的樣本數(shù)據(jù),所以也可以使用以下代碼將其直接導(dǎo)入。 

  1. import pandas as pd  
  2. import numpy as np  
  3. from sklearn.datasets import fetch_openml  
  4. pd.options.plotting.backend = 'plotly'  
  5. X,y = fetch_openml("wine", version=1as_frame=Truereturn_X_y=True 
  6. data = pd.concat([X,y], axis=1 
  7. data.head() 

該數(shù)據(jù)集是葡萄酒相關(guān)的,包含葡萄酒類型的許多功能和相應(yīng)的標(biāo)簽。數(shù)據(jù)集的前幾行如下所示。

下面使用Plotly backend探索一下數(shù)據(jù)集。

繪圖方式與正常使用Pandas內(nèi)置的繪圖操作幾乎相同,只是現(xiàn)在以豐富的Plotly顯示可視化效果。

下面的代碼繪制了數(shù)據(jù)集中兩個要素之間的關(guān)系。 

  1. fig = data[['Alcohol', 'Proline']].plot.scatter(y='Alcohol'x='Proline' 
  2. fig.show() 

如果將鼠標(biāo)懸停在圖表上,可以選擇將圖表下載為高質(zhì)量的圖像文件。

我們可以結(jié)合Pandas的groupby函數(shù)創(chuàng)建一個條形圖,總結(jié)各類之間Hue的均值差異。 

  1. data[['Hue','class']].groupby(['class']).mean().plot.bar() 

將class添加到我們剛才創(chuàng)建的散點圖中。通過Plotly可以輕松地為每個類應(yīng)用不同的顏色,以便直觀地看到分類。 

  1. fig = data[['Hue', 'Proline', 'class']].plot.scatter(x='Hue'y='Proline'color='class'title='Proline and Hue by wine class' 
  2. fig.show() 

3. Bokeh backend

Bokeh是另一個Python可視化包,也可提供豐富的交互式可視化效果。Bokeh還具有streaming API,可以為比如金融市場等流數(shù)據(jù)創(chuàng)建實時可視化。

pandas-Bokeh的GitHub鏈接如下:

https://github.com/PatrikHlobil/Pandas-Bokeh

老樣子,用pip安裝即可,pip install pandas-bokeh。

為了在Jupyterlab中顯示Bokeh可視化效果,還需要安裝兩個新的擴展。 

  1. jupyter labextension install @jupyter-widgets/jupyterlab-manager  
  2. jupyter labextension install @bokeh/jupyter_bokeh 

下面我們使用Bokeh backend重新創(chuàng)建剛剛plotly實現(xiàn)的的散點圖。 

  1. pd.options.plotting.backend = 'pandas_bokeh'  
  2. import pandas_bokeh  
  3. from bokeh.io import output_notebook  
  4. from bokeh.plotting import figure, show   
  5. output_notebook()  
  6. p1 = data.plot_bokeh.scatter(x='Hue',   
  7.                               y='Proline',   
  8.                               category='class' 
  9.                               title='Proline and Hue by wine class' 
  10.                               show_figure=False 
  11. show(p1) 

關(guān)鍵語句就一行代碼,非常快捷,交互式效果如下。

Bokeh還具有plot_grid函數(shù),可以為多個圖表創(chuàng)建類似于儀表板的布局,下面在網(wǎng)格布局中創(chuàng)建了四個圖表。 

  1. output_notebook()  
  2. p1 = data.plot_bokeh.scatter(x='Hue',   
  3.                               y='Proline',   
  4.                               category='class' 
  5.                               title='Proline and Hue by wine class' 
  6.                               show_figure=False 
  7. p2 = data[['Hue','class']].groupby(['class']).mean().plot.bar(title='Mean Hue per Class' 
  8. df_hue = pd.DataFrame({  
  9.     'class_1': data[data['class'] == '1']['Hue'],  
  10.     'class_2': data[data['class'] == '2']['Hue'],  
  11.     'class_3': data[data['class'] == '3']['Hue']},  
  12.     columns=['class_1', 'class_2', 'class_3'])   
  13. p3 = df_hue.plot_bokeh.hist(title='Distribution per Class: Hue' 
  14. df_proline = pd.DataFrame({  
  15.     'class_1': data[data['class'] == '1']['Proline'],  
  16.     'class_2': data[data['class'] == '2']['Proline'],  
  17.     'class_3': data[data['class'] == '3']['Proline']},  
  18.     columns=['class_1', 'class_2', 'class_3'])   
  19. p4 = df_proline.plot_bokeh.hist(title='Distribution per Class: Proline' 
  20. pandas_bokeh.plot_grid([[p1, p2],   
  21.                         [p3, p4]], plot_width=450

可以看到,可視化的部分都是在pandas的dataframe基礎(chǔ)上一行代碼搞定,最后plot_grid完成布局。

4. 總結(jié)

在內(nèi)置的Pandas繪圖功能增加多個第三方可視化backend,大大增強了pandas用于數(shù)據(jù)可視化的功能,今后可能真的不需再去學(xué)習(xí)眾多可視化操作了,使用pandas也可以一擊入魂! 

 

責(zé)任編輯:龐桂玉 來源: Python編程
相關(guān)推薦

2021-01-13 11:13:46

ExcelPandas代碼

2022-08-24 13:39:46

PandasGUIExcel

2012-08-01 09:50:09

HotmailOutlook微軟

2015-03-16 11:09:28

MongoDBPostgreSQL數(shù)據(jù)遷移

2021-04-23 09:09:19

GraphQLREST查詢

2020-03-11 14:39:26

數(shù)據(jù)可視化地圖可視化地理信息

2023-02-15 08:24:12

數(shù)據(jù)分析數(shù)據(jù)可視化

2019-02-01 10:35:33

PythonGo語言編程語言

2021-07-27 05:56:53

CrocFTPSFTP

2018-01-02 08:40:19

云安全云遷移數(shù)據(jù)泄露

2023-11-28 17:24:45

2009-03-30 08:44:22

微軟Windows 7操作系統(tǒng)

2017-10-14 13:54:26

數(shù)據(jù)可視化數(shù)據(jù)信息可視化

2022-08-26 09:15:58

Python可視化plotly

2009-04-21 14:26:41

可視化監(jiān)控IT管理摩卡

2011-01-07 18:05:37

QQ騰訊移動互聯(lián)網(wǎng)

2024-01-03 18:45:35

Pandas繪圖函數(shù)

2015-08-20 10:06:36

可視化

2020-06-08 15:06:33

Pandas可視化數(shù)據(jù)

2021-06-02 22:25:26

2G5G運營商
點贊
收藏

51CTO技術(shù)棧公眾號