Bản đồ nhiệt được định nghĩa là biểu diễn đồ họa của dữ liệu bằng cách sử dụng màu sắc để trực quan hóa giá trị của ma trận. Trong đó, để biểu thị các giá trị phổ biến hơn hoặc các hoạt động cao hơn, các màu sáng hơn về cơ bản là màu đỏ được sử dụng và để biểu thị các giá trị hoạt động hoặc ít phổ biến hơn, các màu tối hơn được ưu tiên. Bản đồ nhiệt cũng được xác định theo tên của ma trận bóng. Bản đồ nhiệt trong Seaborn có thể được vẽ bằng cách sử dụng seaborn. chức năng bản đồ nhiệt []
sinh ra biển. bản đồ nhiệt[]
cú pháp. sinh ra biển. bản đồ nhiệt[data, *, vmin=None, vmax=None, cmap=None, center=None, annot_kws=None, linewidths=0, linecolor=’white’, cbar=True, **kwargs]
Thông số quan trọng
- dữ liệu. Tập dữ liệu 2D có thể được ép thành một ndarray
- vmin, vmax. Các giá trị để neo bản đồ màu, nếu không chúng được suy ra từ dữ liệu và các đối số từ khóa khác
- cmap. Ánh xạ từ các giá trị dữ liệu sang không gian màu
- trung tâm. Giá trị để căn giữa bản đồ màu khi vẽ dữ liệu phân kỳ
- chú thích. Nếu Đúng, hãy ghi giá trị dữ liệu vào từng ô
- fmt. Mã định dạng chuỗi để sử dụng khi thêm chú thích
- băng thông đường truyền. Chiều rộng của các dòng sẽ phân chia từng ô
- màu đường kẻ. Màu của các đường sẽ phân chia từng ô
- xà đơn. Có nên vẽ một thanh màu không
Tất cả các tham số ngoại trừ dữ liệu là tùy chọn
trả lại. Một đối tượng kiểu matplotlib. trục. _subplots. Trục Subplot
Hãy để chúng tôi hiểu bản đồ nhiệt với các ví dụ
Bản đồ nhiệt cơ bản
Tạo bản đồ nhiệt với các tham số mặc định. Chúng tôi sẽ tạo dữ liệu 10×10 2-D bằng cách sử dụng hàm randint[] của mô-đun NumPy
Python3
# importing the modules
import
numpy as np
import
seaborn as sn
import
matplotlib.pyplot as plt
________số 8
# generating 2-D 10x10 matrix of random numbers
# importing the modules
0
# importing the modules
1# importing the modules
2 # importing the modules
3# importing the modules
2 # importing the modules
5# importing the modules
6
# importing the modules
7# importing the modules
8# importing the modules
2 import
0# importing the modules
6
# importing the modules
7__import
3# importing the modules
2 import
5import
6# importing the modules
6import
6import
9
numpy as np
0import
5numpy as np
2numpy as np
3
numpy as np
0numpy as np
5
________số 8
numpy as np
7
numpy as np
8# importing the modules
2 import
0# importing the modules
2 import
2
________số 8
import
4
import
5
đầu ra
The data to be plotted: [[46 30 55 86 42 94 31 56 21 7] [68 42 95 28 93 13 90 27 14 65] [73 84 92 66 16 15 57 36 46 84] [ 7 11 41 37 8 41 96 53 51 72] [52 64 1 80 33 30 91 80 28 88] [19 93 64 23 72 15 39 35 62 3] [51 45 51 17 83 37 81 31 62 10] [ 9 28 30 47 73 96 10 43 30 2] [74 28 34 26 2 70 82 53 97 96] [86 13 60 51 95 26 22 29 14 29]]
Chúng tôi sẽ sử dụng cùng một dữ liệu này trong tất cả các ví dụ
Neo bản đồ màu
Nếu chúng ta đặt giá trị vmin là 30 và giá trị vmax là 70 thì chỉ những ô có giá trị từ 30 đến 70 mới được hiển thị. Điều này được gọi là neo bản đồ màu
Python3
# importing the modules
import
numpy as np
import
seaborn as sn
import
matplotlib.pyplot as plt
________số 8
# generating 2-D 10x10 matrix of random numbers
# importing the modules
0
# importing the modules
1# importing the modules
2 # importing the modules
3# importing the modules
2# importing the modules
5# importing the modules
6
# importing the modules
7__# importing the modules
8# importing the modules
2import
0# importing the modules
6
# importing the modules
7__import
3# importing the modules
2import
5import
6# importing the modules
6import
6import
9
________số 8
matplotlib.pyplot as plt
6
matplotlib.pyplot as plt
7____12 matplotlib.pyplot as plt
9
0# importing the modules
2
2
________số 8
numpy as np
7
numpy as np
8# importing the modules
2 import
0# importing the modules
2
9
# generating 2-D 10x10 matrix of random numbers
0matplotlib.pyplot as plt
7____12# generating 2-D 10x10 matrix of random numbers
3
# generating 2-D 10x10 matrix of random numbers
0
0# importing the modules
2# generating 2-D 10x10 matrix of random numbers
7
________số 8
import
4
import
5
đầu ra
Chọn bản đồ màu
Trong phần này, chúng ta sẽ xem xét tham số cmap. Matplotlib cung cấp cho chúng ta nhiều bản đồ màu, bạn có thể xem tất cả chúng tại đây. Trong ví dụ của chúng tôi, chúng tôi sẽ sử dụng tab20
Python3
# importing the modules
import
numpy as np
import
seaborn as sn
import
matplotlib.pyplot as plt
________số 8
# generating 2-D 10x10 matrix of random numbers
# importing the modules
0
# importing the modules
1# importing the modules
2 # importing the modules
3# importing the modules
2# importing the modules
5# importing the modules
6
# importing the modules
7__# importing the modules
8# importing the modules
2import
0# importing the modules
6
# importing the modules
7__import
3# importing the modules
2import
5import
6# importing the modules
6import
6import
9
________số 8
matplotlib.pyplot as plt
6
# importing the modules
32# importing the modules
2 # importing the modules
34
________số 8
numpy as np
7
numpy as np
8# importing the modules
2 import
0# importing the modules
2
9
# generating 2-D 10x10 matrix of random numbers
0# importing the modules
32# importing the modules
2# importing the modules
45
________số 8
import
4
import
5
đầu ra
Định tâm bản đồ màu
Định tâm cmap thành 0 bằng cách chuyển tham số trung tâm là 0
Python3
# importing the modules
import
numpy as np
import
seaborn as sn
import
matplotlib.pyplot as plt
________số 8
# generating 2-D 10x10 matrix of random numbers
# importing the modules
0
# importing the modules
1# importing the modules
2 # importing the modules
3# importing the modules
2# importing the modules
5# importing the modules
6
# importing the modules
7__# importing the modules
8# importing the modules
2import
0# importing the modules
6
# importing the modules
7__import
3# importing the modules
2import
5import
6# importing the modules
6import
6import
9
________số 8
matplotlib.pyplot as plt
6
# importing the modules
32# importing the modules
2 # importing the modules
34
________ 183 ________ 12 ________ 185
________số 8
numpy as np
7
numpy as np
8# importing the modules
2 import
0# importing the modules
2
9
# generating 2-D 10x10 matrix of random numbers
0# importing the modules
32# importing the modules
2# importing the modules
96
# generating 2-D 10x10 matrix of random numbers
0# importing the modules
83____12import
00
________số 8
import
4
import
5
đầu ra
Hiển thị các giá trị ô
Nếu muốn hiển thị giá trị của các ô thì ta truyền tham số annot là True. fmt được sử dụng để chọn kiểu dữ liệu của nội dung của các ô được hiển thị.
Python3
# importing the modules
import
numpy as np
import
seaborn as sn
import
matplotlib.pyplot as plt
________số 8
# generating 2-D 10x10 matrix of random numbers
# importing the modules
0
# importing the modules
1# importing the modules
2 # importing the modules
3# importing the modules
2# importing the modules
5# importing the modules
6
# importing the modules
7__# importing the modules
8# importing the modules
2import
0# importing the modules
6
# importing the modules
7__import
3# importing the modules
2import
5import
6# importing the modules
6import
6import
9
________số 8
matplotlib.pyplot as plt
6
import
35# importing the modules
2 import
37
________số 8
numpy as np
7
numpy as np
8# importing the modules
2 import
0# importing the modules
2
9
# generating 2-D 10x10 matrix of random numbers
0import
35____12import
48
________số 8
import
4
import
5
đầu ra
Tùy chỉnh đường phân cách
Chúng ta có thể thay đổi độ dày và màu sắc của các đường ngăn cách các ô bằng cách sử dụng các tham số linewidths và linecolor tương ứng
Python3
# importing the modules
import
numpy as np
import
seaborn as sn
import
matplotlib.pyplot as plt
________số 8
# generating 2-D 10x10 matrix of random numbers
# importing the modules
0
# importing the modules
1# importing the modules
2 # importing the modules
3# importing the modules
2# importing the modules
5# importing the modules
6
# importing the modules
7__# importing the modules
8# importing the modules
2import
0# importing the modules
6
# importing the modules
7__import
3# importing the modules
2import
5import
6# importing the modules
6import
6import
9
________số 8
matplotlib.pyplot as plt
6
import
83# importing the modules
2 import
85
import
86______12 import
88
________số 8
numpy as np
7
numpy as np
8# importing the modules
2 import
0# importing the modules
2
9
# generating 2-D 10x10 matrix of random numbers
0import
83____12import
99
# generating 2-D 10x10 matrix of random numbers
0import
86# importing the modules
2numpy as np
03
________số 8
import
4
import
5
đầu ra
Ẩn thanh màu
Chúng tôi có thể tắt thanh màu bằng cách đặt tham số cbar thành Sai
Python3
# importing the modules
import
numpy as np
import
seaborn as sn
import
matplotlib.pyplot as plt
________số 8
# generating 2-D 10x10 matrix of random numbers
# importing the modules
0
# importing the modules
1# importing the modules
2 # importing the modules
3# importing the modules
2# importing the modules
5# importing the modules
6
# importing the modules
7__# importing the modules
8# importing the modules
2import
0# importing the modules
6
# importing the modules
7__import
3# importing the modules
2import
5import
6# importing the modules
6import
6import
9
________số 8
matplotlib.pyplot as plt
6
numpy as np
38# importing the modules
2 numpy as np
40
________số 8
numpy as np
7
numpy as np
8# importing the modules
2 import
0# importing the modules
2
9
# generating 2-D 10x10 matrix of random numbers
0numpy as np
38____12numpy as np
51
________số 8
import
4
import
5
đầu ra
Loại bỏ các nhãn
Chúng ta có thể vô hiệu hóa nhãn x và nhãn y bằng cách chuyển Sai trong các tham số xticklabels và yticklabels tương ứng