Skip to content

Commit 8bbbeb3

Browse files
committed
docs(correlation): update correlation docs
1 parent 19690d5 commit 8bbbeb3

11 files changed

Lines changed: 172 additions & 158 deletions

docs/usage/correlation.md

Lines changed: 112 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,112 @@
1-
# 相关图
2-
3-
点线相关图是一类用于展示两个变量之间相关关系的可视化图形,通常在图中绘制每个样本的一对变量值作为点,并通过拟合线、相关系数和显著性标注来体现它们的相关程度。
4-
它是探索变量之间线性或非线性关系的常用方法,尤其在科学研究和数据分析中非常常见。
5-
6-
`plot_correlation_figure` 提供了简洁易用的点线相关图功能,能够自动绘制变量散点、拟合线,可以计算并显示 Spearman 或 Pearson 两种相关系数(默认计算 Spearman 相关性)。
7-
同时,`plot_correlation_figure` 会根据显著性水平自动标注 `*``**``***`,帮助用户快速判断相关性是否显著,适合用于科研图表、演示幻灯片或论文插图中。
8-
9-
## 快速出图
10-
11-
假如我们有两组样本数量一致的数据(每组包含 100 个样本),我们希望通过绘图直观展示它们之间是否存在相关性。
12-
这通常可以通过点线相关图来实现,将每对样本值绘制为散点,并结合拟合线和相关系数,判断变量之间的相关程度及显著性。
13-
14-
15-
```python
16-
import numpy as np
17-
import matplotlib.pyplot as plt
18-
from plotfig import *
19-
20-
np.random.seed(42)
21-
data1 = np.arange(100)
22-
data2 = data1 + np.random.normal(1,50, 100)
23-
# data2是在data1的基础上加上了噪声。
24-
# 正经人都知道data1和data2相关,那么plotfig知不知道呢?
25-
26-
ax = plot_correlation_figure(data1,data2)
27-
```
28-
29-
30-
31-
![png](correlation_files/correlation_4_0.png)
32-
33-
34-
35-
## 参数设置
36-
37-
全部参数见[`plot_correlation_figure`](../api/index.md/#plotfig.correlation)的 API 文档。
38-
39-
40-
```python
41-
import numpy as np
42-
import matplotlib.pyplot as plt
43-
from plotfig import *
44-
45-
np.random.seed(42)
46-
data1 = np.arange(100)
47-
data2 = data1 + np.random.normal(1,50, 100)
48-
# data2是在data1的基础上加上了噪声。
49-
# 正经人都知道data1和data2相关,那么plotfig知不知道呢?
50-
51-
fig, ax = plt.subplots(figsize=(3, 3))
52-
ax = plot_correlation_figure(
53-
data1,
54-
data2,
55-
stats_method="spearman", # 仅有“spearman, pearson”,默认是spearman
56-
ci=True, # 显示95%置信区间
57-
dots_color="green",
58-
line_color="pink",
59-
title_name="Correlation between data1 and data2",
60-
title_fontsize=10,
61-
title_pad=20, # 控制释标题和图的距离,默认是10
62-
x_label_name="Data1",
63-
y_label_name="Data2",
64-
)
65-
```
66-
67-
68-
69-
![png](correlation_files/correlation_7_0.png)
70-
71-
72-
73-
利用 `hexbin=True` 。我们可以展示大量散点分布的密度,而不需要绘制所有的散点。
74-
75-
76-
```python
77-
import numpy as np
78-
import matplotlib.pyplot as plt
79-
from plotfig import *
80-
81-
np.random.seed(42)
82-
n = 100_000
83-
data1 = np.random.standard_normal(n)
84-
data2 = 2.0 + 3.0 * data1 + 4.0 * np.random.standard_normal(n)
85-
86-
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(7, 3), layout="constrained")
87-
ax1 = plot_correlation_figure(
88-
data1,
89-
data2,
90-
ax=ax1
91-
)
92-
93-
hb = plot_correlation_figure(
94-
data1,
95-
data2,
96-
ax=ax2,
97-
hexbin=True,
98-
hexbin_cmap="Reds",
99-
hexbin_gridsize=30
100-
)
101-
cb = fig.colorbar(hb, ax=ax2, label='counts')
102-
```
103-
104-
105-
106-
![png](correlation_files/correlation_9_0.png)
107-
108-
1+
# 相关图
2+
3+
点线相关图是一类用于展示两个变量之间相关关系的可视化图形,通常在图中绘制每个样本的一对变量值作为点,并通过拟合线、相关系数和显著性标注来体现它们的相关程度。
4+
它是探索变量之间线性或非线性关系的常用方法,尤其在科学研究和数据分析中非常常见。
5+
6+
`plot_correlation_figure` 提供了简洁易用的点线相关图功能,能够自动绘制变量散点、拟合线,可以计算并显示 Spearman 或 Pearson 两种相关系数(默认计算 Spearman 相关性)。
7+
同时,`plot_correlation_figure` 会根据显著性水平自动标注 `*``**``***`,帮助用户快速判断相关性是否显著,适合用于科研图表、演示幻灯片或论文插图中。
8+
9+
## 快速出图
10+
11+
假如我们有两组样本数量一致的数据(每组包含 100 个样本),我们希望通过绘图直观展示它们之间是否存在相关性。
12+
这通常可以通过点线相关图来实现,将每对样本值绘制为散点,并结合拟合线和相关系数,判断变量之间的相关程度及显著性。
13+
14+
15+
```python
16+
import numpy as np
17+
import matplotlib.pyplot as plt
18+
from plotfig import *
19+
20+
np.random.seed(42)
21+
data1 = np.arange(100)
22+
data2 = data1 + np.random.normal(1,50, 100)
23+
# data2是在data1的基础上加上了噪声。
24+
# 正经人都知道data1和data2相关,那么plotfig知不知道呢?
25+
26+
ax = plot_correlation_figure(data1,data2)
27+
```
28+
29+
30+
31+
![png](correlation_files/correlation_4_0.png)
32+
33+
34+
35+
## hexbin图
36+
37+
利用 `hexbin=True` 。我们可以展示大量散点分布的密度,而不需要绘制所有的散点。
38+
39+
40+
```python
41+
import numpy as np
42+
import matplotlib.pyplot as plt
43+
from plotfig import *
44+
45+
np.random.seed(42)
46+
n = 100_000
47+
data1 = np.random.standard_normal(n)
48+
data2 = 2.0 + 3.0 * data1 + 4.0 * np.random.standard_normal(n)
49+
50+
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(7, 3), layout="constrained")
51+
ax1 = plot_correlation_figure(
52+
data1,
53+
data2,
54+
ax=ax1
55+
)
56+
57+
hb = plot_correlation_figure(
58+
data1,
59+
data2,
60+
ax=ax2,
61+
hexbin=True,
62+
hexbin_cmap="Reds",
63+
hexbin_gridsize=30
64+
)
65+
cb = fig.colorbar(hb, ax=ax2, label='counts')
66+
```
67+
68+
69+
70+
![png](correlation_files/correlation_7_0.png)
71+
72+
73+
74+
## 参数设置
75+
76+
全部参数见[`plot_correlation_figure`](../api/index.md/#plotfig.correlation)的 API 文档。
77+
78+
79+
```python
80+
import numpy as np
81+
import matplotlib.pyplot as plt
82+
from plotfig import *
83+
84+
np.random.seed(42)
85+
data1 = np.arange(100)
86+
data2 = data1 + np.random.normal(1,50, 100)
87+
# data2是在data1的基础上加上了噪声。
88+
# 正经人都知道data1和data2相关,那么plotfig知不知道呢?
89+
90+
fig, ax = plt.subplots(figsize=(3, 3))
91+
ax = plot_correlation_figure(
92+
data1,
93+
data2,
94+
stats_method="spearman", # 仅有“spearman, pearson”,默认是spearman
95+
ci=True, # 显示95%置信区间
96+
dots_color="green",
97+
line_color="pink",
98+
title_name="Correlation between data1 and data2",
99+
title_fontsize=10,
100+
title_pad=20, # 控制释标题和图的距离,默认是10
101+
x_label_name="Data1",
102+
y_label_name="Data2",
103+
xlim=(0,100),
104+
ylim=(-100,150),
105+
)
106+
```
107+
108+
109+
110+
![png](correlation_files/correlation_10_0.png)
111+
112+
18.6 KB
Loading
-13.6 KB
Binary file not shown.
-13.6 KB
Binary file not shown.
1.9 KB
Loading
-18.8 KB
Binary file not shown.
-18.8 KB
Binary file not shown.
49 KB
Loading
-59.3 KB
Binary file not shown.
-52.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)