首页 文章

按分类变量分组的箱图

提问于
浏览
1

将pandas用于大型数据集,我已经将其缩减为我需要的信息 . 基本上我想根据他们的帖子中使用的主题标签的数量来绘制来自两个不同国家的用户的朋友数量的分布,作为并排的箱图(我称之为分组的箱图) . 6,我将此视为一个分类变量) . 这导致在同一帧中总共2 * 6 = 12个箱图,以便于比较 .

我做了一些研究,我知道df.boxplot(by ='x'),但这并不能说明比较这两个国家的额外水平 .

数据集包含多个主题标签(int),国家(字符串),朋友数(int)的列 .

值得注意的是,我对Python中的绘图相当新,包括轴和子图等,所以请尽可能在答案中包含一些额外的信息 .

编辑:小样本数据集

#followers  #friends  #mentions  #hashtags  country  lang_user place  
450            53        71          1          0       ja         es   NaN  
489            54        34          1          1       ja         es   NaN  
867          1569      1999          0          0       en         es   NaN  
1021          224       242          0          3       ja         ja   NaN  
1022          377       506          1          5       ja         ja   NaN  
1023          315       305          0          2       ja         ja   NaN

1 回答

  • 3

    我喜欢用seaborn进行这种可视化 . 我猜"extra level"你的意思是 "hue".

    import seaborn as sns
    sns.set_style("whitegrid")
    tips = sns.load_dataset("tips")
    ax = sns.boxplot(x="day", y="total_bill", hue="smoker",              
    data=tips, palette="Set3")
    

    结果将是:
    enter image description here

    看看这个文件:https://seaborn.pydata.org/generated/seaborn.boxplot.html

相关问题