seaborn 是一个基于 matplotlib的绘图工具库, 提供比较高层的接口来绘制精美的统计图表
看看官方文档上给的一个例子, 泰坦尼克上的乘客数据分析
import seaborn as sns
sns.set(style="darkgrid")
titanic = sns.load_dataset("titanic")
print(titanic.info())
ax = sns.countplot(x="class", hue="who", data=titanic)
g = sns.factorplot(x="class", hue="who", col="survived",
data=titanic, kind="count",
size=4, aspect=.7);
数据集装载为一个数据框
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 15 columns):
survived 891 non-null int64
pclass 891 non-null int64
sex 891 non-null object
age 714 non-null float64
sibsp 891 non-null int64
parch 891 non-null int64
fare 891 non-null float64
embarked 889 non-null object
class 891 non-null category
who 891 non-null object
adult_male 891 non-null bool
deck 203 non-null category
embark_town 889 non-null object
alive 891 non-null object
alone 891 non-null bool
dtypes: bool(2), category(2), float64(2), int64(4), object(5)
memory usage: 63.0+ KB
None
图一为一二三等舱的乘客分布, 在传统直方图中可以在增加类型
图一为生还的乘客的分布, 在其中可以看出点什么
再用鸢尾花数据集为例, 我们来绘制一个箱体图
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from pandas.plotting import scatter_matrix
iris = datasets.load_iris()
# Create box plot with Seaborn's default settings
_ = sns.boxplot(x='species', y='petal length (cm)', data=iris.data)
# Label the axes
_ = plt.xlabel('species')
_ = plt.ylabel('petal length (cm)')
# Show the plot
plt.show()
网友评论