Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
512 views
in Technique[技术] by (71.8m points)

pandas - stacking Seaborn Catplot by looping through dataframes

I have multiple data frames consist of three main columns: 1)the categories (c1, c2, c3), one includes the data values, and one includes different time-periods (AA, BB, CC, DD).

what I am trying to generate is to generate boxplots of the data for all dataframe, at once, and in one figure ! I did try with different enumerate options and "ax" argument, but still it generates the boxplot separately, I couldn't figure it out.

allCN=[df1, df2, df3]
fig, axs = plt.subplots(nrows = 3, ncols=4, figsize = (30,54))
axes = axes.flatten()

for i, x in enumerate(allCN):

    sns.set(style="ticks", palette='Set2')
    sns.set_context("paper", font_scale=1.1, rc={"lines.linewidth": 1.1})

    g=sns.catplot(x="Cat", y="Data", ax=axs[i,0],
                   col="Period", data=x, kind="box", height=4, aspect=10/18,
                     width=0.6,fliersize=2.5,showfliers=False, linewidth=1.1,
                     notch=False,orient="v"))
    g.set_ylabels("test", size=12)
    g.set_xlabels("")

example output

question from:https://stackoverflow.com/questions/65949183/stacking-seaborn-catplot-by-looping-through-dataframes

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One way is to stack your data frames and use the row= argument inside catplot. First to create something like your data:

import pandas as pd
import numpy as np
import seaborn as sns

df1 = pd.DataFrame({'Cat':np.random.choice(['C1','C2','C3'],50),
                    'Data':np.random.uniform(0,1,50),"Period":np.random.choice(['AA','CC','DD'],50)})

df2 = pd.DataFrame({'Cat':np.random.choice(['C1','C2','C3'],50),
                    'Data':np.random.uniform(0,1,50),"Period":np.random.choice(['AA','CC','DD'],50)})

df3 = pd.DataFrame({'Cat':np.random.choice(['C1','C2','C3'],50),
                    'Data':np.random.uniform(0,1,50),"Period":np.random.choice(['AA','CC','DD'],50)})

Then concat the dataframes and add another column (i used source below) to annotate the dataframe:

allCN=pd.concat([df1,df2,df3])
allCN['source'] = np.repeat(['df1','df2','df3'],[len(df1),len(df2),len(df3)])

sns.catplot(x="Cat", y="Data",
            col="Period", row = "source", 
            data=allCN, kind="box", height=2,aspect=1.6)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...