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

image

來源: Python數據科學
做者:東哥起飛

Python作數據分析離不開pandaspnadas更多的承載着處理和變換數據的角色,pands中也內置了可視化的操做,但效果很糙。python

所以,你們在用Python作數據分析時,正常的作法是用先pandas先進行數據處理,而後再用MatplotlibSeabornPlotlyBokeh等對dataframe或者series進行可視化操做。git

可是說實話,每一個可視化包都有本身獨特的方法和函數,常常忘,這是讓我一直很頭疼的地方。github

好消息來了!從最新的pandas版本0.25.3開始,再也不須要上面的操做了,數據處理和可視化徹底能夠用pandas一個就所有搞定。函數

pandas如今可使用PlotlyBokeh做爲可視化的backend,直接實現交互性操做,無需再單獨使用可視化包了。佈局

下面咱們一塊兒看看如何使用。學習

1. 激活backend

importpandas以後,直接使用下面這段代碼激活backend,好比下面要激活plotlyfetch

pd.options.plotting.backend = 'plotly'

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

  • Plotly
  • Holoviews
  • Matplotlib
  • Pandas_bokeh
  • Hyplot

2. Plotly backend

Plotly的好處是,它基於Javascript版本的庫寫出來的,所以生成的Web可視化圖表,能夠顯示爲HTML文件或嵌入基於Python的Web應用程序中。spa

下面看下如何用plotly做爲pandas的backend進行可視化。3d

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

首先,安裝IPywidgets

pip install jupyterlab "ipywidgets>=7.5"

而後運行此命令以安裝Plotly擴展。

jupyter labextension install jupyterlab-plotly@4.8.1

示例選自openml.org的的數據集,連接以下:

數據連接: https://www.openml.org/d/187

這個數據也是Scikit-learn中的樣本數據,因此也可使用如下代碼將其直接導入。

import pandas as pd
import numpy as np

from sklearn.datasets import fetch_openml

pd.options.plotting.backend = 'plotly'

X,y = fetch_openml("wine", version=1, as_frame=True, return_X_y=True)
data = pd.concat([X,y], axis=1)
data.head()

該數據集是葡萄酒相關的,包含葡萄酒類型的許多功能和相應的標籤。數據集的前幾行以下所示。

image

下面使用Plotly backend探索一下數據集。

繪圖方式與正常使用Pandas內置的繪圖操做幾乎相同,只是如今以豐富的Plotly顯示可視化效果。

下面的代碼繪製了數據集中兩個要素之間的關係。

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

image

若是將鼠標懸停在圖表上,能夠選擇將圖表下載爲高質量的圖像文件。
image

咱們能夠結合Pandasgroupby函數建立一個條形圖,總結各種之間Hue的均值差別。

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

image

class添加到咱們剛纔建立的散點圖中。經過Plotly能夠輕鬆地爲每一個類應用不一樣的顏色,以便直觀地看到分類。

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

image

3. Bokeh backend

Bokeh是另外一個Python可視化包,也可提供豐富的交互式可視化效果。Bokeh還具備streaming API,能夠爲好比金融市場等流數據建立實時可視化。

pandas-Bokeh的GitHub連接以下:

https://github.com/PatrikHlob...

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

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

jupyter labextension install @jupyter-widgets/jupyterlab-manager
jupyter labextension install @bokeh/jupyter_bokeh

下面咱們使用Bokeh backend從新建立剛剛plotly實現的的散點圖。

pd.options.plotting.backend = 'pandas_bokeh'

import pandas_bokeh
from bokeh.io import output_notebook
from bokeh.plotting import figure, show

output_notebook()
p1 = data.plot_bokeh.scatter(x='Hue', 
                              y='Proline', 
                              category='class', 
                              title='Proline and Hue by wine class',
                              show_figure=False)
show(p1)

關鍵語句就一行代碼,很是快捷,交互式效果以下。

image

Bokeh還具備plot_grid函數,能夠爲多個圖表建立相似於儀表板的佈局,下面在網格佈局中建立了四個圖表。

output_notebook()

p1 = data.plot_bokeh.scatter(x='Hue', 
                              y='Proline', 
                              category='class', 
                              title='Proline and Hue by wine class',
                              show_figure=False)

p2 = data[['Hue','class']].groupby(['class']).mean().plot.bar(title='Mean Hue per Class')

df_hue = pd.DataFrame({
    'class_1': data[data['class'] == '1']['Hue'],
    'class_2': data[data['class'] == '2']['Hue'],
    'class_3': data[data['class'] == '3']['Hue']},
    columns=['class_1', 'class_2', 'class_3'])

p3 = df_hue.plot_bokeh.hist(title='Distribution per Class: Hue')

df_proline = pd.DataFrame({
    'class_1': data[data['class'] == '1']['Proline'],
    'class_2': data[data['class'] == '2']['Proline'],
    'class_3': data[data['class'] == '3']['Proline']},
    columns=['class_1', 'class_2', 'class_3'])

p4 = df_proline.plot_bokeh.hist(title='Distribution per Class: Proline')

pandas_bokeh.plot_grid([[p1, p2], 
                        [p3, p4]], plot_width=450)

能夠看到,可視化的部分都是在pandasdataframe基礎上一行代碼搞定,最後plot_grid完成佈局。
image

4. 總結

在內置的Pandas繪圖功能增長多個第三方可視化backend,大大加強了pandas用於數據可視化的功能,從此可能真的不需再去學習衆多可視化操做了,使用pandas也能夠一擊入魂!


原創不易,來波點贊支持。

本篇首發於個人原創公衆號:Python數據科學,歡迎關注。
我的網站:http://www.datadeepin.com/

相關文章
相關標籤/搜索