牛批了,1行python代碼就可實(shí)現(xiàn)炫酷可視化
之前畫圖一直在用matlibplot、pyecharts,最近學(xué)習(xí)了一個(gè)新的可視化庫--cufflinks,用了兩天我已經(jīng)深深愛上它了
主要是因?yàn)樗梅ê唵巍D形漂亮、代碼量少,用一兩行代碼,就能畫出非常漂亮的圖形
下面我們一起來看看吧!
1.用法簡單
cufflinks庫主要和dataFrame數(shù)據(jù)結(jié)合使用,繪圖函數(shù)就是 dataFrame.iplot,記住這個(gè)就行了,但是 iplot 函數(shù)里的參數(shù)很多,一些參數(shù)說明如下:
kind:圖的種類,如 scatter、pie、histogram 等mode:lines、markers、lines+markers,分別表示折線、點(diǎn)、折線和點(diǎn)colors:軌跡對(duì)應(yīng)的顏色dash:軌跡對(duì)應(yīng)的虛實(shí)線,solid、dash、dashdot 三種width:軌跡的粗細(xì)xTitle:橫坐標(biāo)名稱yTitle:縱坐標(biāo)的名稱title:圖表的標(biāo)題
如下圖,df為隨機(jī)生成的dataFrame數(shù)據(jù),kind='bar'表示柱狀圖,title代表標(biāo)題,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生成隨機(jī)數(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 |
散點(diǎn)圖
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庫還有更豐富的繪圖功能等著你去挖掘。
評(píng)論
圖片
表情
