土壤有机质含量
for feature in features_to_plot:
fig, axs = plt.subplots(1, 2, figsize=(14,5))
# 判断类别型还是连续型特征
if X[feature].nunique() < 15: # 如果唯一值很少,就用countplot画类别分布
sns.countplot(x=feature, data=X, ax=axs[0], palette='Set2', legend=False)
axs[0].set_title(f'{feature} 类别分布')
else: # 连续型用直方图
sns.histplot(X[feature], kde=True, ax=axs[0], color='skyblue')
axs[0].set_title(f'{feature} 分布直方图')
# 特征 vs 目标 散点图
axs[1].scatter(X[feature], y, alpha=0.7, color='orange')
axs[1].set_xlabel(feature)
axs[1].set_ylabel('土壤有机质含量(%)')
axs[1].set_title(f'{feature} 与土壤有机质含量(%)关系')
plt.tight_layout()
plt.show()