精選30張炫酷的動態(tài)交互式圖表,Pandas一鍵生成,通俗易懂
DataFrame數(shù)據(jù)集當中生成炫酷的動態(tài)交互式的圖表,我們先來介紹一下這次需要用到的模塊cufflinksseaborn封裝了matplotlib一樣,cufflinks也在plotly上面做了進一步的包裝及優(yōu)化,方法統(tǒng)一、參數(shù)配置簡單,對于DataFrame數(shù)據(jù)集而言也可以方便靈活的繪圖,而這次我們要繪制的圖表包括折線圖 面積圖 散點圖 柱狀圖 直方圖 箱型圖 熱力圖 3D 散點圖/3D 氣泡圖 趨勢圖 餅圖 K線圖 多個子圖相拼合
模塊的安裝
涉及到安裝,直接pip install即可
pip?install?cufflinks
導入模塊,并查看相關的配置
我們導入該模塊,看一下目前的版本是在多少
cf.__version__
output
'0.17.3'
0.17.3,也是最新的版本,然后我們最新版本支持可以繪制的圖表有哪些cf.help()
output
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
?.......
df.iplot(kind=圖表名稱)而如何我們想要查看某個特定圖表繪制時候的參數(shù),例如柱狀圖bar參數(shù)有哪些,可以這么做cf.help('bar')
柱狀圖
我們先來看一下直方圖圖表的繪制,首先來創(chuàng)建一個數(shù)據(jù)集用于圖表的繪制
df2?=?pd.DataFrame({'Category':['A','B','C','D'],
????????????????????'Values':[95,56,70,85]})
df2
output
??Category??Values
0????????A??????95
1????????B??????56
2????????C??????70
3????????D??????85
然后我們來繪制直方圖
df2.iplot(kind='bar',x='Category',y='Values',
??????????xTitle?=?"Category",yTitle?=?"Values",
??????????title?=?"直方圖")
output

x參數(shù)上面填的是x軸上面對應的變量名,而y參數(shù)填的是y軸上面對應的變量名,我們可以將繪制的圖表以png的格式下載下來,

df?=?pd.DataFrame(np.random.randn(100,4),columns='A?B?C?D'.split())
df.head()
output
??????????A?????????B?????????C?????????D
0??0.612403?-0.029236?-0.595502??0.027722
1??1.167609??1.528045?-0.498168?-0.221060
2?-1.338883?-0.732692??0.935410??0.338740
3??1.662209??0.269750?-1.026117?-0.858472
4??1.387077?-0.839192?-0.562382?-0.989672
我們來繪制直方圖的圖表
df.head(10).iplot('bar')
output

我們也可以來繪制“堆疊式”的直方圖
df.head(10).iplot(kind='bar',barmode='stack')
output

那么同樣地,我們也可以將直方圖橫過來來繪制
df.head(10).iplot(kind='barh',barmode='stack')
output

折線圖
df數(shù)據(jù)集各列做一個累加df3?=?df.cumsum()
然后我們來繪制折線圖
df3.iplot()
output

當然你也可以篩選出當中的幾列然后來進行繪制,效果如下
df3[["A",?"B"]].iplot()
output

我們也可以給折線圖畫一條擬合其走勢的直線,
df3['A'].iplot(bestfit?=?True,bestfit_colors=['pink'])
output

iplot()方法里面常用的參數(shù)kind:圖表類型,默認的是scatter,散點類型,可供選擇的類型還有bar(直方圖)、box(箱型圖)、heatmap(熱力圖)等等theme: 布局主題,可以通過cf.getThemes()來查看主要有哪些title: 圖表的標題xTitle/yTitle: x或者y軸上面的軸名colors: 繪制圖表時候的顏色subplots: 布爾值,繪制子圖時候需要用到,默認為Falsemode:字符串,繪圖的模式,可以有lines、markers,也還有lines+markers和lines+text等模式size: 針對于散點圖而言,主要用來調(diào)整散點的大小shape: 在繪制子圖時候各個圖的布局bargap: 直方圖當中柱子之間的距離barmode: 直方圖的形態(tài),stack(堆疊式)、group(簇狀)、overlay(覆蓋)
面積圖
從折線圖到面積圖的轉(zhuǎn)變非常的簡單,只需要將參數(shù)fill設置為True即可,代碼如下
df3.iplot(fill?=?True)
output

散點圖
對于散點圖的繪制,我們需要將mode設置成marker,代碼如下
df3.iplot(kind='scatter',x='A',y='B',
??????????mode='markers',size=10)
output

我們可以通過調(diào)整size參數(shù)來調(diào)整散點的大小,例如我們將size調(diào)整成20
df3.iplot(kind='scatter',x='A',y='B',
??????????mode='markers',size=20)
output

或者將mode設置成lines+markers,代碼如下
df3.iplot(kind='scatter',x='A',y='B',
??????????mode='lines?+?markers',size=10)
我們還可以對散點的形狀加以設定,例如下面的代碼
df3.iplot(kind='scatter',x='A',y='B',
??????????mode='markers',size=20,symbol="x",
??????????colorscale='paired',)
output

當然我們也可以對散點的顏色加以設定
df.iplot(kind='scatter'?,mode='markers',
?????????symbol='square',colors=['orange','purple','blue','red'],
?????????size=20)
output

氣泡圖
kind參數(shù)改成bubble,假設我們有這樣一組數(shù)據(jù)cf.datagen.bubble(prefix='industry').head()
output
??????????x?????????y??size????text?categories
0??0.332274??1.053811?????2??LCN.CG??industry1
1?-0.856835??0.422373????87??ZKY.XC??industry1
2?-0.818344?-0.167020????72??ZSJ.DJ??industry1
3?-0.720254??0.458264????11??ONG.SM??industry1
4?-0.004744??0.644006????40??HUW.DN??industry1
我們來繪制一下氣泡圖
cf.datagen.bubble(prefix='industry').iplot(kind='bubble',x='x',y='y',size='size',
???????categories='categories',text='text',?xTitle='Returns',
???????yTitle='Analyst?Score',title='Cufflinks?-?氣泡圖')
output

3D散點圖
那既然我們已經(jīng)提到了氣泡圖,那么3D散點圖也就順便提一下吧,假設我們的數(shù)據(jù)如下所示
cf.datagen.scatter3d(2,150).head()
output
??????????x?????????y?????????z????text?categories
0??0.375359?-0.683845?-0.960599??RER.JD??category1
1??0.635806??1.210649??0.319687??INM.LE??category1
2??0.578831??0.103654??1.333646??BSZ.HS??category1
3?-1.128907?-1.189098??1.531494??GJZ.UX??category1
4??0.067668?-1.990996??0.088281??IQZ.KS??category1
cf.datagen.scatter3d(2,150).iplot(kind='scatter3d',x='x',y='y',z='z',size=15,
??????????????????????????????????categories='categories',text='text',
??????????????????????????????????title='Cufflinks?-?3D氣泡圖',colors=['yellow','purple'],
??????????????????????????????????width=1,margin=(0,0,0,0),
??????????????????????????????????opacity=1)
output

3D氣泡圖
那么提到了3D散點圖,就不得不提3D的氣泡圖了,假設我們的數(shù)據(jù)集長這樣
cf.datagen.bubble3d(5,4).head()
output
??????????x?????????y?????????z??size????text?categories
0?-1.888528??0.801430?-0.493671????77??OKC.HL??category1
1?-0.744953?-0.004398?-1.249949????61??GAG.UH??category1
2??0.980846??1.241730?-0.741482????37??LVB.EM??category1
3?-0.230157??0.427072??0.007010????78??NWZ.MG??category1
4??0.025272?-0.424051?-0.602937????76??JDW.AX??category2
我們來繪制一下3D的氣泡圖
cf.datagen.bubble3d(5,4).iplot(kind='bubble3d',x='x',y='y',z='z',size='size',
???????????????????????????????text='text',categories='categories',
???????????????????????????????title='Cufflinks?-?3D氣泡圖',colorscale='set1',
???????????????????????????????width=.9,opacity=0.9)
output

箱型圖
接下來我們看一下箱型圖的繪制,箱型圖對于我們來觀察數(shù)據(jù)的分布、是否存在極值等情況有著很大的幫助
df.iplot(kind?=?"box")
output

熱力圖
這個是熱力圖的繪制,我們來看一下數(shù)據(jù)集
cf.datagen.heatmap(20,20).head()
output
???????????y_0????????y_1????????y_2??...???????y_17???????y_18???????y_19
x_0??40.000000??58.195525??55.355233??...??77.318287??80.187609??78.959951
x_1??37.111934??25.068114??25.730511??...??27.261941??32.303315??28.550340
x_2??54.881357??54.254479??59.434281??...??75.894161??74.051203??72.896999
x_3??41.337221??39.319033??37.916613??...??15.885289??29.404226??26.278611
x_4??42.862472??36.365226??37.959368??...??24.998608??25.096598??32.413760
我們來繪制一下熱力圖,代碼如下
cf.datagen.heatmap(20,20).iplot(kind='heatmap',colorscale='spectral',title='Cufflinks - 熱力圖')output

趨勢圖
所謂的趨勢圖,說白了就是折線圖和面積圖兩者的結合,代碼如下
df[["A",?"B"]].iplot(kind?=?'spread')
output

餅圖
下面我們來看一下餅圖的繪制,代碼如下
cf.datagen.pie(n_labels=6,?mode?=?"stocks").iplot(
????kind?=?"pie",
????labels?=?"labels",
????values?=?"values")
output

K線圖
cufflinks也可以用來繪制K線圖,我們來看一下這里的數(shù)據(jù)集
cf.datagen.ohlc().head()
output
??????????????????open????????high?????????low???????close
2015-01-01??100.000000??119.144561???97.305961??106.125985
2015-01-02??106.131897??118.814224???96.740816??115.124342
2015-01-03??116.091647??131.477558??115.801048??126.913591
2015-01-04??128.589287??144.116844??117.837221??136.332657
2015-01-05??134.809052??138.681252??118.273850??120.252828
從上面的數(shù)據(jù)集當中可以看到,有開盤價、收盤價、最高/最低價,然后我們來繪制K線圖
cf.datagen.ohlc().iplot(kind?=?"ohlc",xTitle?=?"日期",
????????????????????????yTitle="價格",title?=?"K線圖")
output

直方圖
df?=?pd.DataFrame({'a':?np.random.randn(1000)?+?1,?'b':?np.random.randn(1000),
????????????????????'c':?np.random.randn(1000)?-?1},?columns=['a',?'b',?'c'])
df.iplot(kind?=?"histogram")
output

多個子圖的繪制
然后我們看一下多個子圖的繪制,一個是用scatter_matrix()方法來實現(xiàn)
df?=?pd.DataFrame(np.random.randn(1000,?4),
??????????????????columns=['a',?'b',?'c',?'d'])
df.scatter_matrix()
output

另外就是使用subplots參數(shù),將其參數(shù)設置為True,例如我們來繪制多個直方圖子圖
df_h=cf.datagen.histogram(4)
df_h.iplot(kind='histogram',subplots=True,bins=50)
output

或者是繪制多個折線圖子圖
df=cf.datagen.lines(4)
df.iplot(subplots=True,subplot_titles=True,legend=True)
output

最后我們還可以自由來組合多個子圖的繪制,通過里面的specs參數(shù)
df=cf.datagen.bubble(10,50,mode='stocks')
#?定義要繪制圖表的形式
figs=cf.figures(df,[dict(kind='histogram',keys='x',color='blue'),
????????????????????dict(kind='scatter',mode='markers',x='x',y='y',size=5),
????????????????????dict(kind='scatter',mode='markers',x='x',y='y',size=5,color='teal')],asList=True)
figs.append(cf.datagen.lines(1).figure(bestfit=True,colors=['blue'],bestfit_colors=['red']))
base_layout=cf.tools.get_base_layout(figs)
#?多個子圖如何來分布,specs參數(shù)當中,分為兩行兩列來進行分布
specs=cf.subplots(figs,shape=(3,2),base_layout=base_layout,vertical_spacing=.25,horizontal_spacing=.04,
???????????????specs=[[{'rowspan':2},{}],[None,{}],[{'colspan':2},None]],
???????????????subplot_titles=['直方圖','散點圖_1','散點圖_2','折線圖+擬合線'])
specs['layout'].update(showlegend=True)
cf.iplot(specs)
output


各位伙伴們好,詹帥本帥搭建了一個個人博客和小程序,匯集各種干貨和資源,也方便大家閱讀,感興趣的小伙伴請移步小程序體驗一下哦!(歡迎提建議)
推薦閱讀
牛逼!Python常用數(shù)據(jù)類型的基本操作(長文系列第①篇)
牛逼!Python的判斷、循環(huán)和各種表達式(長文系列第②篇)
推薦閱讀
牛逼!Python常用數(shù)據(jù)類型的基本操作(長文系列第①篇)
牛逼!Python的判斷、循環(huán)和各種表達式(長文系列第②篇)
