-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy path鳶尾花分類-iris檔案-05.py
77 lines (59 loc) · 2.2 KB
/
鳶尾花分類-iris檔案-05.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
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, ax = plt.subplots()
ax.set_title('鳶尾花特徵分佈圖')
# 設定x軸及y軸標題
plt.xlabel('花萼寬度')
plt.ylabel('花萼高度')
# 將測試資料畫在圖上
setosa=plt.scatter(sepal_length_setosa, sepal_width_setosa, color='r')
versicolor=plt.scatter(sepal_length_versicolor, sepal_width_versicolor, color='b')
virginica=plt.scatter(sepal_length_virginica, sepal_width_virginica, color='g')
# 產生左上角的標籤說明
plt.legend((setosa, versicolor, virginica),
('setosa', 'versicolor', 'virginica'),
scatterpoints=1,
loc='upper left',
fontsize=14)
# 畫出格線
plt.grid()
# 顯示圖表
plt.show()