牛批了,1行python代碼就可實現(xiàn)炫酷可視化
擊上方“Python爬蟲與數(shù)據(jù)挖掘”,進行關(guān)注
回復(fù)“書籍”即可獲贈Python從入門到進階共10本電子書
之前畫圖一直在用matlibplot、pyecharts,最近學習了一個新的可視化庫--cufflinks,用了兩天我已經(jīng)深深愛上它了
主要是因為它用法簡單、圖形漂亮、代碼量少,用一兩行代碼,就能畫出非常漂亮的圖形
下面我們一起來看看吧!
1.用法簡單
cufflinks庫主要和dataFrame數(shù)據(jù)結(jié)合使用,繪圖函數(shù)就是 dataFrame.iplot,記住這個就行了,但是 iplot 函數(shù)里的參數(shù)很多,一些參數(shù)說明如下:
kind:圖的種類,如 scatter、pie、histogram 等mode:lines、markers、lines+markers,分別表示折線、點、折線和點colors:軌跡對應(yīng)的顏色dash:軌跡對應(yīng)的虛實線,solid、dash、dashdot 三種width:軌跡的粗細xTitle:橫坐標名稱yTitle:縱坐標的名稱title:圖表的標題
如下圖,df為隨機生成的dataFrame數(shù)據(jù),kind='bar'表示柱狀圖,title代表標題,xTitle命名X軸,yTitle命名Y軸:
import pandas as pd
import numpy as np
import cufflinks as cf
df=pd.DataFrame(np.random.rand(12, 4), columns=['a', 'b', 'c', 'd'])
df.iplot(kind ='bar',title='示例', xTitle = 'X軸', yTitle ='Y軸')

2.少量代碼就能畫出非常漂亮的圖形
cufflinks為我們提供了豐富的主題樣式,支持包括polar、pearl、henanigans、solar、ggplot、space和white等7種主題。
折線圖
cf.datagen.lines(4,10).iplot(mode='lines+markers',theme='solar')

cufflinks使用datagen生成隨機數(shù),figure定義為lines形式,cf.datagen.lines(2,10)的具體形式如下:
cf.datagen.lines(2,10) #2代表2組,10代表10天
| WCB.EH | OAA.CQ | |
|---|---|---|
| 2015-01-01 | -0.052580 | -0.351618 |
| 2015-01-02 | 1.056254 | -1.476417 |
| 2015-01-03 | 0.078017 | 1.129168 |
| 2015-01-04 | 0.282141 | 0.908655 |
| 2015-01-05 | 0.960537 | -0.223996 |
| 2015-01-06 | 1.420355 | 0.212851 |
| 2015-01-07 | 2.266144 | 0.358502 |
| 2015-01-08 | 0.008034 | 1.086130 |
| 2015-01-09 | 1.876946 | 2.226895 |
| 2015-01-10 | 1.855625 | 2.852383 |
散點圖
df = pd.DataFrame(np.random.rand(50, 4), columns=['a', 'b', 'c', 'd'])
df.iplot(kind='scatter',mode='markers',colors=['orange','teal','blue','yellow'],size=20,theme='solar')

氣泡圖
df.iplot(kind='bubble',x='a',y='b',size='c',theme='solar')

subplots 子圖
df=cf.datagen.lines(4)
df.iplot(subplots=True,shape=(4,1),shared_xaxes=True,vertical_spacing=.02,fill=True,theme='ggplot')

箱形圖
cf.datagen.box(20).iplot(kind='box',legend=False,theme='ggplot')

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

3D圖
cf.datagen.scatter3d(5,4).iplot(kind='scatter3d',x='x',y='y',z='z',text='text',categories='categories')

怎么樣?是不是很方便,希望我的介紹能夠起到拋磚引玉的作用,cufflinks庫還有更豐富的繪圖功能等著你去挖掘。
------------------- End -------------------
往期精彩文章推薦:

歡迎大家點贊,留言,轉(zhuǎn)發(fā),轉(zhuǎn)載,感謝大家的相伴與支持
想加入Python學習群請在后臺回復(fù)【入群】
萬水千山總是情,點個【在看】行不行
/今日留言主題/
隨便說一兩句吧~
