酷炫的可視化工具包 - cufflinks

在學習可視化的時候發現了一個非常棒的工具cufflinks,可以交互式的繪製圖像,並且非常酷炫, 下面轉載一篇博客,記錄一下使用方法:
原博客地址:Python動態可視化Cufflinks

一、cufflinks介紹

1.1 簡介

  學過Python數據分析的朋友都知道,在可視化的工具中,有很多優秀的三方庫,比如matplotlibseabornplotlypyecharts等等。這些可視化庫都有自己的特點,在實際應用中也廣爲大家使用。

  就像seaborn封裝了matplotlib一樣,cufflinks在plotly的基礎上做了一進一步的包裝,方法統一,參數配置簡單。其次它還可以結合pandas的dataframe隨意靈活地畫圖。可以把它形容爲"pandas like visualization"。

1.2 安裝

pip install cufflinks
'''
Collecting cufflinks
  Using cached https://files.pythonhosted.org/packages/5e/5a/db3d6523ee870ecc229008b209b6b21231397302de34f9c446929a41f027/cufflinks-0.16.tar.gz
...................................................................................
Successfully built cufflinks retrying
Installing collected packages: retrying, plotly, colorlover, cufflinks
Successfully installed colorlover-0.3.0 cufflinks-0.16 plotly-3.10.0 retrying-1.3.3
'''

1.3 cufflinks包介紹

import cufflinks as cf
print(cf.help())
Use 'cufflinks.help(figure)' to see the list of available parameters for the given figure.
Use 'DataFrame.iplot(kind=figure)' to plot the respective figure
Figures:
	bar
	box
	bubble
	bubble3d
	candle
	choroplet
	distplot
	heatmap
	histogram
	ohlc
	pie
	ratio
	scatter
	scatter3d
	scattergeo
	spread
	surface
	violin
None

1.4 使用方法

使用方法其實很簡單,我總結一下,它的格式大致是這樣的:
DataFrame.Figure.iplot

DataFrame:代表pandas的數據框;
Figure:代表我們上面看到的可繪製圖形,比如bar、box、histogram等等;
iplot:代表繪製方法,其中有很多參數可以進行配置,調節符合你自己風格的可視化圖形;

二、cufflinks使用

import pandas as pd
import numpy as np
import cufflinks as cf
# 如果使用online模式,那麼生成的圖形是有限制的。所以,我們這裏先設置爲offline模式,這樣就避免了出現次數限制問題。
cf.set_config_file(offline=True)
df = pd.read_csv('./PimaIndiansdiabetes.csv')
print(df.shape)  #(768, 9)

2.0 條形圖 bar

# 隨機生成bar 條形圖
df1=pd.DataFrame(np.random.rand(12, 4), columns=['a', 'b', 'c', 'd'])	
df1.iplot(kind='bar',barmode='stack')
# Pima生成bar 條形圖
df.iloc[0:66,0:3].iplot(kind='bar',barmode='stack')

在這裏插入圖片描述

2.1 histogram直方圖

# 隨機生成histogram直方圖
cf.datagen.histogram(3).iplot(kind='histogram')

在這裏插入圖片描述

# Pima生成histogram直方圖
df.iloc[:,0:3].iplot(kind='histogram')

2.2 box箱型圖

# box箱型圖,可以看到,x軸每個box都有對應的名稱,這是因爲cufflinks通過kind參數識別了box圖形,
# 自動爲它生成的名字。如果我們只生成隨機數,它是這樣子的,默認生成100行的隨機分佈的數據,列數由自己選定。
cf.datagen.box(20).iplot(kind='box',legend=False)

在這裏插入圖片描述

print(df.columns)
'''
Index(['Pregnancies', 'Glucose', 'BloodPressure', 'SkinThickness', 'Insulin',
       'BMI', 'DiabetesPedigreeFunction', 'Age', 'Outcome'],
      dtype='object')
'''
df.iplot(kind='box',legend=False)

在這裏插入圖片描述

2.3 scatter散點圖

# scatter散點圖df.iplot(kind='scatter',
         mode='markers',
         colors=[
             'orange', 'teal', 'blue', 'yellow', 'black', 'red', 'green',
             'magenta', 'cyan'
         ],
         size=5)

在這裏插入圖片描述

2.4 lines 線圖

# 隨機數繪圖,'DataFrame' object has no attribute 'lines'
cf.datagen.lines(1,500).ta_plot(study='sma',periods=[13,21,55])
'''
1)cufflinks使用datagen生成隨機數;

2)figure定義爲lines形式,數據爲(1,500)

3)然後再用ta_plot繪製這一組時間序列,參數設置SMA展現三個不同週期的時序分析。

在這裏插入圖片描述

2.5 bubble氣泡圖

# bubble氣泡圖
d = pd.DataFrame(np.random.rand(50, 4), columns=['a', 'b', 'c', 'd'])
d.iplot(kind='bubble',x='a',y='b',size='a')

在這裏插入圖片描述

# bubble氣泡圖
# pPma bubble氣泡圖
d = df.iloc[0:100,:]
#size決定了圈的大小
d.iplot(kind='bubble',x='Glucose',y='DiabetesPedigreeFunction',size='DiabetesPedigreeFunction')

在這裏插入圖片描述

2.6 3d 圖

#隨機數生成3d 圖
cf.datagen.scatter3d(5,4).iplot(kind='scatter3d',x='x',y='y',z='z',text='text',categories='categories')

在這裏插入圖片描述

2.7 scatter matrix 散點矩陣圖

#隨機scatter matrix 散點矩陣圖
df2 = pd.DataFrame(np.random.randn(1000, 4), columns=['a', 'b', 'c', 'd'])	
df2.scatter_matrix()

在這裏插入圖片描述

#Pima scatter matrix 散點矩陣圖
df.iloc[0:200,0:5].scatter_matrix()

在這裏插入圖片描述

2.8 subplots 子圖

#隨機subplots 子圖
df3=cf.datagen.lines(4)	
df3.iplot(subplots=True,shape=(4,1),shared_xaxes=True,vertical_spacing=.02,fill=True)

在這裏插入圖片描述

#Pima subplots 子圖
df.iloc[0:200,0:5].iplot(subplots=True,shape=(5,1),shared_xaxes=True,vertical_spacing=.02,fill=True)

在這裏插入圖片描述

df.iloc[0:200,0:6].iplot(subplots=True,subplot_titles=True,legend=False)

在這裏插入圖片描述

2.9 shapes 形狀圖

# 隨機shapes 形狀圖
df5=cf.datagen.lines(3,columns=['a','b','c'])	
df5.iplot(hline=[dict(y=-1,color='blue',width=3),dict(y=1,color='pink',dash='dash')])
# 將某個區域標記出來,可以使用hspan類型。豎條的區域,可以用vspan類型。
df5.iplot(hspan=[(-1,1),(2,5)])

在這裏插入圖片描述

# Pima shapes 形狀圖
df.iloc[0:200,0:6].iplot(hline=[dict(y=-1,color='blue',width=3),dict(y=1,color='pink',dash='dash')])
# 將某個區域標記出來,可以使用hspan類型。豎條的區域,可以用vspan類型。
df.iloc[0:200,0:6].iplot(hspan=[(100,120),(220,250)])

在這裏插入圖片描述
在這裏插入圖片描述

三、總結

help(df.iplot)

數據集和jupyter文件鏈接:https://pan.baidu.com/s/1O5ukYe41iAO9v_czHbs5CA

提取碼:by2a

更多參考:[https://www.cnblogs.com/Yang-Sen/p/11226005.html]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章