Một số code biểu diễn biểu đồ bằng matplotlib trong python

Posted by Hao Do on January 9, 2023

Một số code biểu diễn biểu đồ bằng matplotlib trong python

Một số code biểu diễn biểu đồ bằng matplotlib trong python

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
# Importing packages
import matplotlib.pyplot as plt

# Define data values
x1 = [50, 100, 150, 200, 250, 306]
y1 = [72.45, 75.26, 79.32, 84.25, 87.76, 94.77]

x2 = [
50,
100,
150,
200,
250,
295
]

y2 = [
71.29,
76.49,
80.23,
84.16,
89.87,
98.3
]

x3 = [
50,
100,
150,
200,
250,
300,
332
]

y3 = [
73.27,
76.49,
79.56,
83.28,
86.48,
89.11,
96.38
]

fig, ax = plt.subplots(figsize=[12,8])

ax.plot(x1,
         y1,
         marker='o', color='g', linewidth=2, label='Для задания 1', linestyle= 'dashed')
ax.plot(x2,
         y2,
         marker='x', color='r', linewidth=3, label='Для задания 2')

ax.plot(x3,
         y3,
         marker='+', color='b', linewidth=1.5, label='Для задания 3', linestyle= 'dotted')

ax.set_ylabel('Точность (%)', size=24)
ax.set_xlabel('Количество команд', size=24)

plt.xticks(fontsize=16)
plt.yticks(fontsize=16)


for index in range(len(x2)):
  ax.text(x2[index], y2[index], y2[index], size=21)

plt.legend(prop={'size': 21})

#plt.show()
plt.title('', size = 20)
plt.savefig('test.png')

img

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
x = [i + 1 for i in range(20)]
print(x)
x= [1, 2,3, 4, 5, 6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
y1 = []
y2 = []

print(type(x[0]))
import random
hs = 1
rev = True
for i in range(20):
    y1.append(hs * random.random())
    y2.append(hs * random.random())
y1.sort(reverse=rev)
y2.sort(reverse=rev)
print(y1)
type_txt = 'Потеря'

plt.figure(figsize = (10,7))
plt.plot(x, y1, 'g', label='Тренировка ' + type_txt)
plt.plot(x, y2, 'b', label='Тестирование ' + type_txt)
plt.title('Обучение и тестирование ' + type_txt, size = 24)
plt.xlabel('Эпохи', size=18)
plt.ylabel(type_txt, size=18)
plt.legend(prop={'size': 18})
plt.xticks(fontsize=16)
plt.yticks(fontsize=16)

#plt.show()
plt.savefig('test1.png')

img

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
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.metrics import classification_report, confusion_matrix

## test data ###

t1 = 100
t2 = 200
t3 = 300
testy_org = []

for i in range(t1):
  testy_org.append(1)
for i in range(t2):
  testy_org.append(2)
for i in range(t3):
  testy_org.append(3)

test_y_pred = []
for i in range(80):
  test_y_pred.append(1)
for i in range(11):
  test_y_pred.append(2)
for i in range(9):
  test_y_pred.append(3)

for i in range(68):
    test_y_pred.append(1)

for i in range(200-68):
  test_y_pred.append(2)

for i in range(45):
  test_y_pred.append(1)
for i in range(55):
  test_y_pred.append(2)
for i in range(200):
  test_y_pred.append(3)
  
## end test data ####
  
    

cm = confusion_matrix(testy_org, test_y_pred)
cm_df = pd.DataFrame(cm,
                     index = ['задание 1','задание 2','задание 3'], 
                     columns = ['задание 1','задание 2','задание 3'])

plt.figure(figsize=(10,7))
sns.heatmap(cm_df, annot=True, fmt=".0f",cmap="Blues")
plt.title('Матрица путаницы', size = 24)
plt.ylabel('Фактические значения', size=21)
plt.xlabel('Прогнозируемые значения', size=21)
plt.xticks(fontsize=16)
plt.yticks(fontsize=16)
sns.set(font_scale=1.4)
plt.show()

img

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
fig, ax = plt.subplots(figsize=(10,7))
import pandas as pd
import numpy as np

data = {'type': ['CNN', 'LSTM', 'GRU'],
        'central': [388, 1226, 1422],
        'FL': [2628, 8307, 9687]
        }

df = pd.DataFrame(data)

x = np.arange(len(df))
width = 0.4

plt.bar(x-0.2, df['central'],
        width, color='g', label='Centralized data')
plt.bar(x+0.2, df['FL'],
        width, color='b', label='Decentralized data')

plt.title('Time training between centralized and decentralized data', fontsize=25)
plt.xlabel(None)
plt.xticks(df.index, df['type'], fontsize=17)
plt.ylabel('Training time (s)', fontsize=20)
plt.yticks(fontsize=17)
sns.despine(bottom=True)
ax.grid(False)
ax.tick_params(bottom=False, left=True)
plt.legend(frameon=False, fontsize=15)
plt.show()

img

Tài liệu tham khảo

Internet

Hết.