记得导入相关包~
import seaborn as sns
import matplotlib.pyplot as plt
分类条形图
g = sns.FacetGrid(train_df, col='Survived')
g.map(plt.hist, 'Age', bins=20)
h = sns.FacetGrid(data1, row = 'Sex', col = 'Pclass', hue = 'Survived')
h.map(plt.hist, 'Age', alpha = .75)
h.add_legend()
grid = sns.FacetGrid(train_df, col='Survived', row='Pclass', size=2.2, aspect=1.6)
grid.map(plt.hist, 'Age', alpha=.5, bins=20)
grid.add_legend()
distplot
displot()集合了matplotlib的hist()与核函数估计kdeplot的功能 可以画出拟合曲线
import matplotlib.pyplot as plt
import seaborn as sns
# 不要子图
fig, ax = plt.subplots(figsize=(10, 5))
sns.distplot(train_df['年龄'], kde=True)
# 同时画出多张图
fig, ax = plt.subplots(2, 2, figsize=(10, 8))
sns.distplot(train_df['诊断结果'], kde=True, ax=ax[0,0]) # 数据集 x轴内容 ax是位置
sns.distplot(train_df['年龄'], kde=True, ax=ax[0,1])
条形图
plt.figure(figsize=[16,12])
plt.subplot(235) #两行三列 图在第五个位置
plt.hist(x = [data1[data1['Survived']==1]['Age'], data1[data1['Survived']==0]['Age']],
stacked=True, color = ['g','r'],label = ['Survived','Dead'])
plt.title('Age Histogram by Survival')
plt.xlabel('Age (Years)')
plt.ylabel('# of Passengers')
plt.legend()
# 绘制个体特征图 一行三列
fig, saxis = plt.subplots(1, 3,figsize=(16,12))#条形图 ax里面是显示的位置
sns.barplot(x = 'Embarked', y = 'Survived', data=data1, ax = saxis[0,0])
sns.barplot(x = 'Pclass', y = 'Survived', order=[1,2,3], data=data1, ax = saxis[0,1])
sns.barplot(x = 'IsAlone', y = 'Survived', order=[1,0], data=data1, ax = saxis[0,2])
散点图
df.plot(kind="scatter", x="特征a", y="特征b")
# save_fig(" ") 可以用来保存图片
alpha设置透明度
df.plot(kind="scatter", x="特征a", y="特征b", alpha=0.1)
设置坐标范围
df.plot(kind="scatter", x="特征a", y="特征b",alpha=0.1)
plt.axis([0, 16, 0, 550000]) #用来设置坐标范围
箱线图
一个单独特征的箱线图
fig, ax = plt.subplots(1, 1, figsize=(12, 4))
sns.boxplot(x='Alley', y='SalePrice', data=data_train)
两个结果对比的箱线图
fig, ax = plt.subplots(1, 2, figsize=(12, 4))
sns.boxplot(x='Embarked', y='Fare', data=train, ax=ax[0])
sns.boxplot(x='Embarked', y='Age', data=train, ax=ax[1])
# sns.boxplot(x='Embarked', y='Age', hue='Sex', data=train, ax=ax[2])
# 设置图的名称
ax[0].set_title('Fare Boxplot By Port of Embarkment')
ax[1].set_title('Age Distribution Boxplot By Embarkement')
热图
#seaborn 用于绘图,其中封装了matplotlib
import seaborn as sns
corrmat = train_df.corr()
f, ax = plt.subplots(figsize=(12, 9))
sns.heatmap(corrmat, vmax=.8, square=True,annot=True) # annot设置成true表示每个方框中显示数值
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['SimHei'] # 指定默认字体/Microsoft YaHei
mpl.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问问题
plt.show()
使用散点图矩阵图,可以两两发现特征之间的联系
from pandas.plotting import scatter_matrix
attributes = ["median_house_value", "median_income", "total_rooms",
"housing_median_age"]
scatter_matrix(housing[attributes], figsize=(12, 8))