【问题标题】:How to plot multiple columns side by side with Seaborn countplot如何使用 Seaborn countplot 并排绘制多列
【发布时间】:2020-06-23 12:13:39
【问题描述】:

我有一个Pandas DataFrame,例如:

pd.DataFrame({
    "A": [165,166,920,920,920,165,162,162,166,165,755,469],
    "B": [1920,868,470,470,698,945,1670,575,701,1920,1920,470],
    "C": [1670,461,461,212,212,212,212,414,453,196,254,461]
})

我希望每个option A, B and C 都有一个seaborn countplot,每列并排绘制一个top 2

下面是类似的图像。我的问题是让列与图例并排显示。

谢谢

【问题讨论】:

    标签: python pandas matplotlib data-visualization seaborn


    【解决方案1】:

    你可以试试这个吗?

    import pandas as pd
    import seaborn as sns
    
    df=pd.DataFrame({
    "A": [165,166,920,920,920,165,162,162,166,165,755,469],
    "B": [1920,868,470,470,698,945,1670,575,701,1920,1920,470],
    "C": [1670,461,461,212,212,212,212,414,453,196,254,461]})
    
    
    sns.set(style="darkgrid")
    fig, ax =plt.subplots(3,1)
    max_count = max([max(df[i].value_counts()) for i in df.columns])
    A=sns.countplot(y=df['A'],ax=ax[0],order=df.A.value_counts().iloc[:2].index)
    B=sns.countplot(y=df['B'],ax=ax[1],order=df.B.value_counts().iloc[:2].index)
    C=sns.countplot(y=df['C'],ax=ax[2],order=df.C.value_counts().iloc[:2].index)
    ax[0].set_xlim(0,max_count)
    ax[1].set_xlim(0,max_count)
    ax[2].set_xlim(0,max_count)
    A.set(xticklabels=[])
    B.set(xticklabels=[])
    

    【讨论】:

    • 如何设置整体标题。设置和 set_title 不起作用
    • @A.JT,使用这个:ax[0].set_title(<title>) 让我知道这是否适合你。 :)
    • 很高兴为您提供帮助,@A.JT! :)
    猜你喜欢
    • 2018-11-21
    • 2016-01-08
    • 2017-12-09
    • 1970-01-01
    • 2018-03-03
    • 2021-04-26
    • 2019-07-29
    • 2018-06-11
    • 2018-11-29
    相关资源
    最近更新 更多