【Python】matplotlib生成散点矩阵图

迁移自CSDN:【python】matplotlib生成散点矩阵图

数据贴后面,先上代码:

比较简单就不解释了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# scatter
import numpy as np
import matplotlib.pyplot as plt
data = np.loadtxt('scatterData.txt')
X = [-3, -2, -1, 0, 1, 2, 3]
Y = [-2, -1, 0, 1, 2]
plt.scatter(data[:,0], data[:,1], c='r')
#move the spines to center
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))
#export the picture
plt.savefig('scatter.png', dpi = 300)
plt.show()

scatterData.txt里面的数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
-3 -2
-2 -2
-1 -2
0 -2
1 -2
2 -2
3 -2
-3 -1
-2 -1
-1 -1
0 -1
1 -1
2 -1
3 -1
-3 0
-2 0
-1 0
0 0
1 0
2 0
3 0
-3 1
-2 1
-1 1
0 1
1 1
2 1
3 1
-3 2
-2 2
-1 2
0 2
1 2
2 2
3 2

2016.04.24更新

忘了把上面的画贴出来了,补上:

1