使用 Seaborn 的箱线图弄乱了绘图
Messed up plots using boxplot with Seaborn
我正在使用此代码绘制我的 df 中的所有数据:
m_cols = ['is_canceled','lead_time', 'arrival_date_year','arrival_date_week_number','arrival_date_day_of_month','stays_in_weekend_nights','adults','children','babies','is_repeated_guest','previous_cancellations','previous_bookings_not_canceled','booking_changes','agent','total_of_special_requests']
for col in num_cols:
sns.boxplot(y=df['is_canceled'].astype('category'),x=col,data=df)
plt.show()
但是我得到了一些看起来像这样的情节,我该如何解决?
箱线图似乎表明绝大多数值是零,其余显示为离群值。所以例如previous_annulations 通常为零,少数具有特定值。所有具有相同值的异常值都被绘制在彼此之上。请注意,boxplot
的“框”位于第 25th 和第 75th 个百分位数之间,中间有一个除法.
一个想法可能是使用不同类型的图,例如violinplot
使用 titanic 数据集:
import matplotlib.pyplot as plt
import seaborn as sns
df = sns.load_dataset('titanic')
m_cols = df.select_dtypes('number').columns.to_list()[1:]
fig, axs = plt.subplots(nrows=len(m_cols), ncols=2, figsize=(15, 7))
for col, ax_row in zip(m_cols, axs):
sns.boxplot(y=df['survived'].astype('category'), x=col, data=df, ax=ax_row[0], palette='rocket')
sns.violinplot(y=df['survived'].astype('category'), x=col, data=df, ax=ax_row[1], palette='rocket')
sns.despine()
plt.tight_layout()
plt.show()
我正在使用此代码绘制我的 df 中的所有数据:
m_cols = ['is_canceled','lead_time', 'arrival_date_year','arrival_date_week_number','arrival_date_day_of_month','stays_in_weekend_nights','adults','children','babies','is_repeated_guest','previous_cancellations','previous_bookings_not_canceled','booking_changes','agent','total_of_special_requests']
for col in num_cols:
sns.boxplot(y=df['is_canceled'].astype('category'),x=col,data=df)
plt.show()
但是我得到了一些看起来像这样的情节,我该如何解决?
箱线图似乎表明绝大多数值是零,其余显示为离群值。所以例如previous_annulations 通常为零,少数具有特定值。所有具有相同值的异常值都被绘制在彼此之上。请注意,boxplot
的“框”位于第 25th 和第 75th 个百分位数之间,中间有一个除法.
一个想法可能是使用不同类型的图,例如violinplot
使用 titanic 数据集:
import matplotlib.pyplot as plt
import seaborn as sns
df = sns.load_dataset('titanic')
m_cols = df.select_dtypes('number').columns.to_list()[1:]
fig, axs = plt.subplots(nrows=len(m_cols), ncols=2, figsize=(15, 7))
for col, ax_row in zip(m_cols, axs):
sns.boxplot(y=df['survived'].astype('category'), x=col, data=df, ax=ax_row[0], palette='rocket')
sns.violinplot(y=df['survived'].astype('category'), x=col, data=df, ax=ax_row[1], palette='rocket')
sns.despine()
plt.tight_layout()
plt.show()