-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy path鳶尾花分類-iris檔案-06.py
117 lines (97 loc) · 4.14 KB
/
鳶尾花分類-iris檔案-06.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import numpy as np
import matplotlib.pyplot as plt
# 讀入資料
#依序為花萼長度(sepal_length), 花萼寬度(sepal_width), 花瓣長度(petal_length), 花瓣寬度(petal_width), 品種(species)
data=np.genfromtxt('iris.csv', dtype=None, delimiter=',')
# 整理資料
# 待繪製的資料
sepal_length_setosa=[]
sepal_width_setosa=[]
petal_length_setosa=[]
petal_width_setosa=[]
sepal_length_versicolor=[]
sepal_width_versicolor=[]
petal_length_versicolor=[]
petal_width_versicolor=[]
sepal_length_virginica=[]
sepal_width_virginica=[]
petal_length_virginica=[]
petal_width_virginica=[]
for (sepal_length, sepal_width, petal_length, petal_width, species) in data:
s=species.decode('UTF-8')
if s=='setosa':
sepal_length_setosa.append(sepal_length)
sepal_width_setosa.append(sepal_width)
petal_length_setosa.append(petal_length)
petal_width_setosa.append(petal_width)
if s=='versicolor':
sepal_length_versicolor.append(sepal_length)
sepal_width_versicolor.append(sepal_width)
petal_length_versicolor.append(petal_length)
petal_width_versicolor.append(petal_width)
if s=='virginica':
sepal_length_virginica.append(sepal_length)
sepal_width_virginica.append(sepal_width)
petal_length_virginica.append(petal_length)
petal_width_virginica.append(petal_width)
# 設定字型及大小
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['font.size'] = 14
# 設定圖標題
fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(6, 6))
#(1)
axes[0, 0].set_xlabel('花萼寬')
axes[0, 0].set_ylabel('花萼高')
axes[0, 0].get_xaxis().set_ticks([])
axes[0, 0].get_yaxis().set_ticks([])
setosa=axes[0, 0].scatter(sepal_length_setosa, sepal_width_setosa, color='r')
versicolor=axes[0, 0].scatter(sepal_length_versicolor, sepal_width_versicolor, color='b')
virginica=axes[0, 0].scatter(sepal_length_virginica, sepal_width_virginica, color='g')
axes[0, 0].grid()
#(2)
axes[0, 1].set_xlabel('花瓣寬')
axes[0, 1].set_ylabel('花瓣高')
axes[0, 1].get_xaxis().set_ticks([])
axes[0, 1].get_yaxis().set_ticks([])
setosa=axes[0, 1].scatter(petal_length_setosa, petal_width_setosa, color='r')
versicolor=axes[0, 1].scatter(petal_length_versicolor, petal_width_versicolor, color='b')
virginica=axes[0, 1].scatter(petal_length_virginica, petal_width_virginica, color='g')
axes[0, 1].grid()
#(3)
axes[0, 2].set_xlabel('花萼寬')
axes[0, 2].set_ylabel('花瓣高')
axes[0, 2].get_xaxis().set_ticks([])
axes[0, 2].get_yaxis().set_ticks([])
setosa=axes[0, 2].scatter(sepal_length_setosa, petal_width_setosa, color='r')
versicolor=axes[0, 2].scatter(sepal_length_versicolor, petal_width_versicolor, color='b')
virginica=axes[0, 2].scatter(sepal_length_virginica, petal_width_virginica, color='g')
axes[0, 2].grid()
#(4)
axes[1, 0].set_xlabel('花瓣寬')
axes[1, 0].set_ylabel('花萼高')
axes[1, 0].get_xaxis().set_ticks([])
axes[1, 0].get_yaxis().set_ticks([])
setosa=axes[1, 0].scatter(petal_width_setosa, sepal_length_setosa, color='r')
versicolor=axes[1, 0].scatter(petal_width_versicolor, sepal_length_versicolor, color='b')
virginica=axes[1, 0].scatter(petal_width_virginica, sepal_length_virginica, color='g')
axes[1, 0].grid()
#(5)
axes[1, 1].set_xlabel('花瓣高')
axes[1, 1].set_ylabel('花萼寬')
axes[1, 1].get_xaxis().set_ticks([])
axes[1, 1].get_yaxis().set_ticks([])
setosa=axes[1, 1].scatter(petal_length_setosa, sepal_width_setosa, color='r')
versicolor=axes[1, 1].scatter(petal_length_versicolor, sepal_width_versicolor, color='b')
virginica=axes[1, 1].scatter(petal_length_virginica, sepal_width_virginica, color='g')
axes[1, 1].grid()
#(6)
axes[1, 2].set_xlabel('花萼高')
axes[1, 2].set_ylabel('花瓣寬')
axes[1, 2].get_xaxis().set_ticks([])
axes[1, 2].get_yaxis().set_ticks([])
setosa=axes[1, 2].scatter(sepal_width_setosa, petal_length_setosa, color='r')
versicolor=axes[1, 2].scatter(sepal_width_versicolor, petal_length_versicolor, color='b')
virginica=axes[1, 2].scatter(sepal_width_virginica, petal_length_virginica, color='g')
axes[1, 2].grid()
# 顯示圖表
plt.show()