Seaborn distplot在尝试为循环绘制每个Pandas列时只返回一列



我在尝试为每个循环绘制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值和相关性。感谢您的帮助。

您可能只得到与最后一个循环相对应的图形
因此,您必须明确要求在每个循环中显示图片。

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])

最新更新