学习https://seaborn.pydata.org/index.html记录,描述不一定准确,具体请参考官网
%matplotlib inline
import numpy as np
import pandas as pd
from scipy import stats, integrate
import seaborn as sns
import matplotlib.pyplot as plt
# plt.rcParams['font.sans-serif']=['SimHei'] # 用来正常显示中文标签
# plt.rcParams['axes.unicode_minus']=False # 用来正常显示负号
# seaborn中文乱码解决方案
from matplotlib.font_manager import FontProperties
myfont=FontProperties(fname=r'C:\Windows\Fonts\simhei.ttf',size=14)
sns.set(font=myfont.get_name())
np.random.seed(20180514)
x = np.random.normal(size=100)
sns.distplot(x) # 默认
# 隐藏数据趋势kde,显示数据紧密度fit
sns.distplot(x, kde=False, fit=stats.gamma)
# 隐藏直方图 hist=False
sns.distplot(x, hist=False, rug=True) # rug=True表示显示 rugplot(x)
# 单变量 kdeplot()
sns.kdeplot(x, shade=True)
sns.kdeplot(x, bw=.2, label="bw: 0.2")
sns.kdeplot(x, bw=2, label="bw: 2")
plt.legend()
# 双变量 kdeplot()
f, ax = plt.subplots(figsize=(6, 6))
cmap = sns.cubehelix_palette(as_cmap=True, dark=0, light=1, reverse=True)
sns.kdeplot(df.x, df.y, cmap=cmap, n_levels=20, shade=True)
sns.kdeplot(x, shade=True, cut=-2) # cut默认为3
sns.rugplot(x)
f, ax = plt.subplots(figsize=(6, 6))
sns.kdeplot(df.x, df.y, ax=ax) # kdeplot() 线条趋势
sns.rugplot(df.x, color="g", ax=ax) # rugplot() X轴条码
sns.rugplot(df.y, vertical=True, ax=ax) # rugplot() Y轴条码
mean, cov = [0, 1], [(1, .5), (.5, 1)]
data = np.random.multivariate_normal(mean, cov, 200)
df = pd.DataFrame(data, columns=["x", "y"])
sns.jointplot(x="x", y="y", data=df)
# kind="kde" 线条趋势
sns.jointplot(x="x", y="y", data=df, kind="kde")
# kind="kde" 六角形
x, y = np.random.multivariate_normal(mean, cov, 1000).T
with sns.axes_style("white"):
sns.jointplot(x=x, y=y, kind="hex", color="k")
g = sns.jointplot(x="x", y="y", data=df, kind="kde", color="m")
g.plot_joint(plt.scatter, c="w", s=30, linewidth=1, marker="+")
g.ax_joint.collections[0].set_alpha(0)
g.set_axis_labels("X轴", "Y轴")
# plt.xlabel('X轴')
# https://github.com/mwaskom/seaborn-data 数据地址
iris = sns.load_dataset("iris")
sns.pairplot(iris)
g = sns.pairplot(iris)
g.map_diag(sns.kdeplot)
g.map_offdiag(sns.kdeplot, cmap="Blues_d", n_levels=6)