1. 程式人生 > 實用技巧 >sns.FacetGrid(),map用法

sns.FacetGrid(),map用法

先看看一個例子

#類別變數的每個類別頻數視覺化
def count_plot(x,  **kwargs):
    sns.countplot(x=x)
    x=plt.xticks(rotation=90)

f = pd.melt(train,  value_vars=['Sex','Embarked'])
g = sns.FacetGrid(f, col="variable",  col_wrap=2, sharex=False, sharey=False, size=5)
g = g.map(count_plot, "value")

我的理解是繪製多個變數(但是資料量太多就不建議使用該方法,因為該類接受的物件是長資料,長資料需要使用到melt,資料量太大會很耗時

),但是又不寫迴圈時,可以使用sns.FacetGrid(),map裡面可以使用你自已定義的函式,或者是sns、plt等

首先FacetGrid是seaborn庫中的一個類,我們在初始化這個類時只需要給它傳一個DataFrame的資料集即可。例項化這個類以後,我麼就可以直接使用這個物件的方法繪製需要的圖形

sns.FacetGrid(data, row=None, col=None, hue=None, col_wrap=None, sharex=True, sharey=True, height=3, aspect=1, palette=None, row_order=None, 
col_order=None, hue_order=None, hue_kws=None, dropna=True, legend_out=True, despine=True, margin_titles=False, xlim=None, ylim=None, subplot_kws=None, gridspec_kws=None, size=None)

主要的引數

data:處理後的(“長格式”)dataframe資料

row, col, hue: strings 也就是行列,分組

col_wrap: int,畫布可以分為多少列

看一些有哪些屬性方法

pc.obj_info(sns.FacetGrid)
ObjInfo object of :
    函式/方法:['add_legend', 'despine', 'facet_axis', 'facet_data', 'map', 'map_dataframe', 'savefig', 'set', 'set_axis_labels', 'set_titles', 'set_xlabels', 'set_xticklabels', 'set_ylabels', 'set_yticklabels']

    屬性:['ax']

我們主要看map,map接受畫圖方法(自定義或者sns自帶等),接受需要畫圖的變數,可以有1個變數或者2個,2個就是x軸和y軸

想要看更詳細的資訊,請移步:https://blog.csdn.net/weixin_42398658/article/details/82960379