【问题标题】:how to avoid automatic sorting with seaborn in a grid plot? (python)如何避免在网格图中使用 seaborn 进行自动排序? (Python)
【发布时间】:2022-01-19 11:04:25
【问题描述】:

我正在尝试绘制一些我拥有的数据,并且我正在努力通过 seaborn 进行自动排序。正如您在下图中看到的那样,每个图表的 x 轴排序都不同,我希望它们相同,[Melhorou, Piorou, Indiferente]。有谁知道如何做到这一点?

这是我到目前为止所做的一段代码。

f = plt.figure()

ax = f.add_subplot(2, 2, 1)
sns.despine(f)
sns.histplot(data = data , x = "A pandemia afetou a sua performance acadêmica?  [1o sem de 2020]", multiple="dodge", palette = "Blues", ax = ax);
plt.xlabel("1o sem 2020")

ax = f.add_subplot(2, 2, 2)
sns.despine(f)
sns.histplot(data = data , x = "A pandemia afetou a sua performance acadêmica?  [2o sem de 2020]", multiple="dodge", palette = "Blues", ax = ax);
plt.xlabel("2o sem 2020")

ax = f.add_subplot(2, 2, 3)
sns.despine(f)
sns.histplot(data = data , x = "A pandemia afetou a sua performance acadêmica?  [1o sem de 2021]", multiple="dodge", palette = "Blues", ax = ax);
plt.xlabel("1o sem 2021")

ax = f.add_subplot(2, 2, 4)
sns.despine(f)
sns.histplot(data = data , x = "A pandemia afetou a sua performance acadêmica?  [2o sem de 2021]", multiple="dodge", palette = "Blues", ax = ax);
plt.xlabel("2o sem 2021")

【问题讨论】:

    标签: python seaborn


    【解决方案1】:

    默认情况下,值在列中出现的顺序决定了histplot的x轴顺序。

    您可以将数据框的列设为分类,并以这种方式强制排序。

    from matplotlib import pyplot as plt
    import seaborn as sns
    import pandas as pd
    import numpy as np
    
    column_names_long = ["A pandemia afetou a sua performance acadêmica?  [1o sem de 2020]",
                         "A pandemia afetou a sua performance acadêmica?  [2o sem de 2020]",
                         "A pandemia afetou a sua performance acadêmica?  [1o sem de 2021]",
                         "A pandemia afetou a sua performance acadêmica?  [2o sem de 2021]"]
    column_names_short = ["1o sem de 2020", "2o sem de 2020", "1o sem de 2021", "2o sem de 2021"]
    
    affectings = ['Melhorou', 'Piorou', 'Indiferente']
    
    data = pd.DataFrame({c: np.random.choice(affectings, 100) for c in column_names_long})
    # rename the columns, so seaborn can show them directly
    data = data.rename(columns={long: short for long, short in zip(column_names_long, column_names_short)})
    for column_name in column_names_short:
        data[column_name] = pd.Categorical(data[column_name], affectings)  # fix an order on each of the columns
    
    fig, axs = plt.subplots(ncols=2, nrows=2, figsize=(12, 7))
    
    for ax, column_name in zip(axs.flatten(), column_names_short):
        sns.histplot(data=data, x=column_name, color="dodgerblue", ax=ax)
    
    sns.despine(fig)
    
    plt.tight_layout()  # fit the subplots and their labels nicely
    plt.show()
    

    或者,您可以将histplot 替换为countplot,它接受order= 参数并允许您为条形分配不同的颜色:

    for ax, column_name in zip(axs.flatten(), column_names_short):
        sns.countplot(data=data, x=column_name, order=affectings,
                      palette=["dodgerblue", "crimson", "orange"], ax=ax)
    

    【讨论】:

    • 是的!可以的,谢谢详细回复
    猜你喜欢
    • 2019-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多