Skip to content

Commit ae2830e

Browse files
committed
提交 seaborn-3 图片和代码示例
1 parent 5326cd8 commit ae2830e

21 files changed

+103
-0
lines changed

day-126/2.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import seaborn as sns
2+
import matplotlib.pyplot as plt
3+
from scipy import stats
4+
sns.set(color_codes=True)
5+
6+
# 2.1 示例
7+
x = np.random.normal(size=100)
8+
sns.distplot(x)
9+
10+
# 2.2 示例
11+
x = np.random.normal(size=100)
12+
sns.distplot(x, kde=False, rug=True)
13+
14+
# 2.3 示例
15+
x = np.random.normal(size=100)
16+
sns.distplot(x, hist=False, rug=True)
17+
18+
x = np.random.normal(0, 1, size=30) # 初始化一组服从正态分布的随机数
19+
bandwidth = 1.06 * x.std() * x.size ** (-1 / 5.) # 根据经验公式计算 KDE 的带宽
20+
support = np.linspace(-4, 4, 200)
21+
22+
kernels = []
23+
for x_i in x:
24+
25+
kernel = stats.norm(x_i, bandwidth).pdf(support) # 获取每一个观测值的核密度估计
26+
kernels.append(kernel)
27+
plt.plot(support, kernel, color="r") # 为每一个观测值绘制核密度估计曲线
28+
29+
sns.rugplot(x, color=".2", linewidth=3)
30+
31+
from scipy.integrate import trapz
32+
density = np.sum(kernels, axis=0)
33+
density /= trapz(density, support)
34+
plt.plot(support, density)
35+
36+
sns.kdeplot(x, shade=True)
37+
38+
sns.kdeplot(x)
39+
sns.kdeplot(x, bw=.2, label="bw: 0.2")
40+
sns.kdeplot(x, bw=2, label="bw: 2")
41+
plt.legend()
42+
43+
sns.kdeplot(x, shade=True, cut=0)
44+
sns.rugplot(x)
45+
46+
# 2.4 示例
47+
x = np.random.gamma(6, size=200)
48+
sns.distplot(x, kde=False, fit=stats.gamma)
49+
50+
51+
52+
53+
54+

day-126/3.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import seaborn as sns
2+
import matplotlib.pyplot as plt
3+
from scipy import stats
4+
sns.set(color_codes=True)
5+
6+
mean, cov = [0, 1], [(1, .5), (.5, 1)]
7+
data = np.random.multivariate_normal(mean, cov, 200)
8+
df = pd.DataFrame(data, columns=["x", "y"])
9+
10+
# 3.1 示例
11+
sns.jointplot(x="x", y="y", data=df)
12+
13+
# 3.2 示例
14+
x, y = np.random.multivariate_normal(mean, cov, 1000).T
15+
with sns.axes_style("white"):
16+
sns.jointplot(x=x, y=y, kind="hex", color="k")
17+
18+
# 3.3 示例
19+
sns.jointplot(x="x", y="y", data=df, kind="kde")
20+
21+
f, ax = plt.subplots(figsize=(6, 6))
22+
sns.kdeplot(df.x, df.y, ax=ax)
23+
sns.rugplot(df.x, color="g", ax=ax)
24+
sns.rugplot(df.y, vertical=True, ax=ax)
25+
26+
f, ax = plt.subplots(figsize=(6, 6))
27+
cmap = sns.cubehelix_palette(as_cmap=True, dark=0, light=1, reverse=True)
28+
sns.kdeplot(df.x, df.y, cmap=cmap, n_levels=60, shade=True)
29+
30+
g = sns.jointplot(x="x", y="y", data=df, kind="kde", color="m")
31+
g.plot_joint(plt.scatter, c="w", s=30, linewidth=1, marker="+")
32+
g.ax_joint.collections[0].set_alpha(0)
33+
g.set_axis_labels("$X$", "$Y$")
34+
35+
36+
37+
38+

day-126/4.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import seaborn as sns
2+
import matplotlib.pyplot as plt
3+
from scipy import stats
4+
sns.set(color_codes=True)
5+
6+
iris = sns.load_dataset("iris")
7+
sns.pairplot(iris)
8+
9+
g = sns.PairGrid(iris)
10+
g.map_diag(sns.kdeplot)
11+
g.map_offdiag(sns.kdeplot, n_levels=6)

day-126/picture/displot-1.png

14.3 KB
Loading

day-126/picture/displot-10.png

15.6 KB
Loading

day-126/picture/displot-11.png

12.4 KB
Loading

day-126/picture/displot-12.png

25.5 KB
Loading

day-126/picture/displot-13.png

15.1 KB
Loading

day-126/picture/displot-14.png

49.3 KB
Loading

day-126/picture/displot-15.png

16.9 KB
Loading

0 commit comments

Comments
 (0)