【深入探索Seaborn】Python高级统计数据可视化实例详解
warning:
这篇文章距离上次修改已过201天,其中的内容可能已经有所变动。
import seaborn as sns
import matplotlib.pyplot as plt
# 加载Seaborn自带的Titanic数据集
df = sns.load_dataset('titanic')
# 分析Embarked离散特征对生存率的影响
sns.catplot(x='embarked', y='survived', data=df, kind='bar', color='lightblue')
plt.title('Port of Embarkation and Survival Rate')
plt.xlabel('Port')
plt.ylabel('Survival Rate (%)')
plt.show()
# 分析不同Pclass和Sex对生存率的影响
sns.catplot(x='class', y='survived', hue='sex', data=df, kind='violin', split=True, bw=.2, scale='width')
plt.title('Passenger Class, Sex, and Survival Rate')
plt.xlabel('Class and Sex')
plt.ylabel('Survival Rate (%)')
plt.show()
# 分析不同年龄(Age)和Pclass的生存率关系
sns.boxplot(x='class', y='age', data=df, notch=True)
plt.title('Survival Rate by Passenger Class and Age')
plt.xlabel('Class')
plt.ylabel('Age')
plt.show()
# 分析不同年龄(Age)和Sex的生存率关系
sns.factorplot(x='sex', y='age', hue='survived', data=df, kind='box', palette='Set1')
plt.title('Survival Rate by Sex, Age, and Class')
plt.xlabel('Sex')
plt.ylabel('Age')
plt.show()
这段代码使用Seaborn的catplot
和factorplot
函数来分析Titanic数据集中不同特征对于生存率的影响。通过制图,可以了解到各个特征与生存率之间的复杂关系。
评论已关闭