学习https://seaborn.pydata.org 记录,描述不一定准确,具体请参考官网
%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)
fig, axes = plt.subplots(2,2,figsize=(12, 10))
sns.distplot(x, ax = axes[0,0]) # 默认
sns.distplot(x, ax = axes[0,1], kde=False) # 隐藏数据趋势线kde
sns.distplot(x, ax = axes[1,0], hist=False, rug=True) # 隐藏直方图hist
sns.distplot(x, ax = axes[1,1], kde=False, fit=stats.gamma) # 显示数据紧密度fit
axes[0,0].set_title('默认')
axes[0,1].set_title('隐藏趋势线')
axes[1,0].set_title('隐藏直方图')
axes[1,1].set_title('显示数据紧密度线')
fig, axes = plt.subplots(1,2,figsize=(12, 5))
sns.kdeplot(x, shade=True, ax = axes[0]) # shade=True 阴影
sns.kdeplot(x, bw=.2, label="bw: 0.2", ax = axes[0])
sns.kdeplot(x, bw=2, label="bw: 2", ax = axes[0])
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, ax = axes[1])
fig, axes = plt.subplots(1,2,figsize=(12, 5))
sns.kdeplot(x, ax = axes[0], shade=True, cut=-2) # cut默认为3
sns.rugplot(x, ax = axes[0])
sns.kdeplot(df.x, df.y, ax = axes[1]) # kdeplot() 线条趋势
sns.rugplot(df.x, color="g", ax = axes[1]) # rugplot() X轴条码
sns.rugplot(df.y, vertical=True, ax = axes[1]) # 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='reg')
# 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轴")
# 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)