【问题标题】:Seaborn distplot only return one column when try to plot each Pandas column by loop forSeaborn distplot 在尝试循环绘制每个 Pandas 列时仅返回一列
【发布时间】:2021-02-23 06:16:37
【问题描述】:

我在尝试使用每个循环绘制 Pandas 列时遇到问题 当我使用displot 而不是distplot 时,它表现得很好,除了它只显示全球分布,而不是基于其组。假设我有一个名为columns 的列名列表和Pandas 的数据框n,它的列名是class。目标是根据每个类的列显示分布图:

for w in columns:
    if w!=<discarded column> or w!=<discarded column>:
        sns.displot(n[w],kde=True

但是当我使用 distplot 时,它只返回第一列:

for w in columns:
    if w!=<discarded column> or w!=<discarded column>:
        sns.distplot(n[w],kde=True

我还是个使用 Seaborn 的新手,因为我从不使用任何可视化并且依赖于 p 值和相关性等数值分析。任何帮助表示赞赏。

【问题讨论】:

    标签: python pandas seaborn


    【解决方案1】:

    您可能只得到与最后一个循环相对应的数字。
    所以你必须明确要求在每个循环中显示图片。

    import matplotlib.pyplot as plt
    
    for w in columns:
        if w not in discarded_columns:
            sns.distplot(n[w], kde=True)
            plt.show()
    

    或者你可以subplots:

    # Keep only target-columns
    target_columns = list(filter(lambda x: x not in discarded_columns, columns))
    
    # Plot with subplots
    fig, axes = plt.subplots(len(target_columns)) # see the parameters, like: nrows, ncols ... figsize=(16,12)
    for i,w in enumerate(target_columns):
        sns.distplot(n[w], kde=True, ax=axes[i])
    

    【讨论】:

      猜你喜欢
      • 2016-11-28
      • 2020-05-19
      • 2020-09-26
      • 2016-05-25
      • 2021-04-06
      • 2019-08-22
      • 2019-08-26
      • 2017-05-10
      • 1970-01-01
      相关资源
      最近更新 更多