Ví dụ sau đây cho thấy cách hiển thị số lượng quan sát trên mỗi nhóm trên biểu đồ thanh. Hàm
no_of_students=[10,24,45,30,23,56,67,34,45,50]
2 của matplotlib được sử dụng để vẽ barplot với các đối số sau
3. Tọa độ x của các thanhno_of_students=[10,24,45,30,23,56,67,34,45,50]
4. Chiều cao của các thanhno_of_students=[10,24,45,30,23,56,67,34,45,50]
5. [Các] chiều rộng của các thanhno_of_students=[10,24,45,30,23,56,67,34,45,50]
6. Màu sắc của các mặt thanhno_of_students=[10,24,45,30,23,56,67,34,45,50]
7. Nhãn của các thanhno_of_students=[10,24,45,30,23,56,67,34,45,50]
Lưu ý rằng, hàm
no_of_students=[10,24,45,30,23,56,67,34,45,50]
8 được sử dụng để thêm chú thích, hàm no_of_students=[10,24,45,30,23,56,67,34,45,50]
9 được sử dụng để thêm nhãn của các thanh vào trục x và hàm import matplotlib.pyplot as plt
def add_value_label[x_list,y_list]:
for i in range[1, len[x_list]+1]:
plt.text[i,y_list[i-1],y_list[i-1]]
no_of_students=[10,24,45,30,23,56,67,34,45,50]
class_number=[1,2,3,4,5,6,7,8,9,10]
plt.bar[class_number,no_of_students]
add_value_label[class_number,no_of_students]
plt.title["No of Students in a class"]
plt.xlabel["Class"]
plt.ylabel["Number of Students"]
plt.show[]
0 được sử dụng để thêm văn bản trên đầu mỗi thanhNếu bạn cần thêm số lượng quan sát vào đầu mỗi thanh, điều đó có nghĩa là bạn có nhiều quan sát cho mỗi nhóm. Trong trường hợp này, barplot có lẽ không phải là phương pháp thích hợp nhất để trực quan hóa dữ liệu của bạn. Thật vậy, tất cả thông tin đằng sau mỗi thanh đều bị mất
Có lẽ bạn nên thử sử dụng biểu đồ vĩ cầm hoặc biểu đồ hình hộp. Một giải pháp khác có thể là thêm các thanh lỗi ở đầu mỗi thanh
Biểu đồ thanh Matplotlib là một cách tốt để trực quan hóa dữ liệu trong python. Trong biểu đồ thanh, chúng ta thường cần thêm nhãn để trực quan hóa dữ liệu. Bài viết này sẽ xem xét các cách khác nhau để thêm nhãn giá trị trên biểu đồ thanh Matplotlib
Thêm nhãn giá trị trên biểu đồ thanh Matplotlib bằng phương pháp import matplotlib.pyplot as plt
def add_value_label[x_list,y_list]:
for i in range[1, len[x_list]+1]:
plt.text[i,y_list[i-1],y_list[i-1]]
no_of_students=[10,24,45,30,23,56,67,34,45,50]
class_number=[1,2,3,4,5,6,7,8,9,10]
plt.bar[class_number,no_of_students]
add_value_label[class_number,no_of_students]
plt.title["No of Students in a class"]
plt.xlabel["Class"]
plt.ylabel["Number of Students"]
plt.show[]
1
import matplotlib.pyplot as plt
def add_value_label[x_list,y_list]:
for i in range[1, len[x_list]+1]:
plt.text[i,y_list[i-1],y_list[i-1]]
no_of_students=[10,24,45,30,23,56,67,34,45,50]
class_number=[1,2,3,4,5,6,7,8,9,10]
plt.bar[class_number,no_of_students]
add_value_label[class_number,no_of_students]
plt.title["No of Students in a class"]
plt.xlabel["Class"]
plt.ylabel["Number of Students"]
plt.show[]
Để thêm nhãn giá trị trên biểu đồ thanh Matplotlib, chúng ta có thể sử dụng hàm
import matplotlib.pyplot as plt
def add_value_label[x_list,y_list]:
for i in range[1, len[x_list]+1]:
plt.text[i,y_list[i-1],y_list[i-1]]
no_of_students=[10,24,45,30,23,56,67,34,45,50]
class_number=[1,2,3,4,5,6,7,8,9,10]
plt.bar[class_number,no_of_students]
add_value_label[class_number,no_of_students]
plt.title["No of Students in a class"]
plt.xlabel["Class"]
plt.ylabel["Number of Students"]
plt.show[]
1Hàm
import matplotlib.pyplot as plt
def add_value_label[x_list,y_list]:
for i in range[1, len[x_list]+1]:
plt.text[i,y_list[i-1],y_list[i-1]]
no_of_students=[10,24,45,30,23,56,67,34,45,50]
class_number=[1,2,3,4,5,6,7,8,9,10]
plt.bar[class_number,no_of_students]
add_value_label[class_number,no_of_students]
plt.title["No of Students in a class"]
plt.xlabel["Class"]
plt.ylabel["Number of Students"]
plt.show[]
1 từ mô-đun no_of_students=[10,24,45,30,23,56,67,34,45,50]
2 được sử dụng để thêm các giá trị văn bản vào bất kỳ vị trí nào trong biểu đồ. Cú pháp của hàm import matplotlib.pyplot as plt
def add_value_label[x_list,y_list]:
for i in range[1, len[x_list]+1]:
plt.text[i,y_list[i-1],y_list[i-1]]
no_of_students=[10,24,45,30,23,56,67,34,45,50]
class_number=[1,2,3,4,5,6,7,8,9,10]
plt.bar[class_number,no_of_students]
add_value_label[class_number,no_of_students]
plt.title["No of Students in a class"]
plt.xlabel["Class"]
plt.ylabel["Number of Students"]
plt.show[]
1 như saumatplotlib.pyplot.text[x, y, s, fontdict=None, **kwargs]
Đây,
- Tham số
4 vàno_of_students=[10,24,45,30,23,56,67,34,45,50]
5 là tọa độ của đồ thị nơi sẽ đặt văn bản. Tham sốno_of_students=[10,24,45,30,23,56,67,34,45,50]
6 chứa chuỗi văn bảnno_of_students=[10,24,45,30,23,56,67,34,45,50]
- Tham số
7 được sử dụng để thay đổi các thuộc tính như phông chữ và kích thước của văn bản. Giá trị mặc định của nó làno_of_students=[10,24,45,30,23,56,67,34,45,50]
8no_of_students=[10,24,45,30,23,56,67,34,45,50]
- Tham số
9 đại diện cho các tham số khác được sử dụng để quyết định vị trí của văn bản trong biểu đồ thanhno_of_students=[10,24,45,30,23,56,67,34,45,50]
Trong bài viết này, chúng ta sẽ sử dụng tham số
import matplotlib.pyplot as plt
def add_value_label[x_list,y_list]:
for i in range[1, len[x_list]+1]:
plt.text[i,y_list[i-1],y_list[i-1]]
no_of_students=[10,24,45,30,23,56,67,34,45,50]
class_number=[1,2,3,4,5,6,7,8,9,10]
plt.bar[class_number,no_of_students]
add_value_label[class_number,no_of_students]
plt.title["No of Students in a class"]
plt.xlabel["Class"]
plt.ylabel["Number of Students"]
plt.show[]
0 để quyết định căn lề ngang của văn bản. Bạn có thể hiểu rõ hơn về các tham số khác của hàm từ tài liệu chính thức của hàm import matplotlib.pyplot as plt
def add_value_label[x_list,y_list]:
for i in range[1, len[x_list]+1]:
plt.text[i,y_list[i-1],y_list[i-1]]
no_of_students=[10,24,45,30,23,56,67,34,45,50]
class_number=[1,2,3,4,5,6,7,8,9,10]
plt.bar[class_number,no_of_students]
add_value_label[class_number,no_of_students]
plt.title["No of Students in a class"]
plt.xlabel["Class"]
plt.ylabel["Number of Students"]
plt.show[]
1Bây giờ chúng ta hãy vẽ biểu đồ thanh và sau đó thêm nhãn giá trị cho nó. Giả sử ta có dữ liệu về số lượng học sinh của một lớp trong trường từ lớp 1 đến lớp 10 dưới dạng danh sách như sau
no_of_students=[10,24,45,30,23,56,67,34,45,50]
Ở đây, chỉ số
import matplotlib.pyplot as plt
def add_value_label[x_list,y_list]:
for i in range[1, len[x_list]+1]:
plt.text[i,y_list[i-1],y_list[i-1]]
no_of_students=[10,24,45,30,23,56,67,34,45,50]
class_number=[1,2,3,4,5,6,7,8,9,10]
plt.bar[class_number,no_of_students]
add_value_label[class_number,no_of_students]
plt.title["No of Students in a class"]
plt.xlabel["Class"]
plt.ylabel["Number of Students"]
plt.show[]
2 đại diện cho số học sinh trong lớp import matplotlib.pyplot as plt
def add_value_label[x_list,y_list]:
for i in range[1, len[x_list]+1]:
plt.text[i,y_list[i-1],y_list[i-1]]
no_of_students=[10,24,45,30,23,56,67,34,45,50]
class_number=[1,2,3,4,5,6,7,8,9,10]
plt.bar[class_number,no_of_students]
add_value_label[class_number,no_of_students]
plt.title["No of Students in a class"]
plt.xlabel["Class"]
plt.ylabel["Number of Students"]
plt.show[]
3. Để vẽ dữ liệu, chúng tôi sẽ tạo một danh sách import matplotlib.pyplot as plt
def add_value_label[x_list,y_list]:
for i in range[1, len[x_list]+1]:
plt.text[i,y_list[i-1],y_list[i-1]]
no_of_students=[10,24,45,30,23,56,67,34,45,50]
class_number=[1,2,3,4,5,6,7,8,9,10]
plt.bar[class_number,no_of_students]
add_value_label[class_number,no_of_students]
plt.title["No of Students in a class"]
plt.xlabel["Class"]
plt.ylabel["Number of Students"]
plt.show[]
4 chứa các số từ 1 đến 10. Sau đó, chúng ta sẽ vẽ biểu đồ thanh bằng hàm import matplotlib.pyplot as plt
def add_value_label[x_list,y_list]:
for i in range[1, len[x_list]+1]:
plt.text[i,y_list[i-1],y_list[i-1]]
no_of_students=[10,24,45,30,23,56,67,34,45,50]
class_number=[1,2,3,4,5,6,7,8,9,10]
plt.bar[class_number,no_of_students]
add_value_label[class_number,no_of_students]
plt.title["No of Students in a class"]
plt.xlabel["Class"]
plt.ylabel["Number of Students"]
plt.show[]
5. Sau đó, chúng tôi sẽ thêm import matplotlib.pyplot as plt
def add_value_label[x_list,y_list]:
for i in range[1, len[x_list]+1]:
plt.text[i,y_list[i-1],y_list[i-1]]
no_of_students=[10,24,45,30,23,56,67,34,45,50]
class_number=[1,2,3,4,5,6,7,8,9,10]
plt.bar[class_number,no_of_students]
add_value_label[class_number,no_of_students]
plt.title["No of Students in a class"]
plt.xlabel["Class"]
plt.ylabel["Number of Students"]
plt.show[]
6, import matplotlib.pyplot as plt
def add_value_label[x_list,y_list]:
for i in range[1, len[x_list]+1]:
plt.text[i,y_list[i-1],y_list[i-1]]
no_of_students=[10,24,45,30,23,56,67,34,45,50]
class_number=[1,2,3,4,5,6,7,8,9,10]
plt.bar[class_number,no_of_students]
add_value_label[class_number,no_of_students]
plt.title["No of Students in a class"]
plt.xlabel["Class"]
plt.ylabel["Number of Students"]
plt.show[]
7 và import matplotlib.pyplot as plt
def add_value_label[x_list,y_list]:
for i in range[1, len[x_list]+1]:
plt.text[i,y_list[i-1],y_list[i-1]]
no_of_students=[10,24,45,30,23,56,67,34,45,50]
class_number=[1,2,3,4,5,6,7,8,9,10]
plt.bar[class_number,no_of_students]
add_value_label[class_number,no_of_students]
plt.title["No of Students in a class"]
plt.xlabel["Class"]
plt.ylabel["Number of Students"]
plt.show[]
8 vào biểu đồ bằng cách sử dụng các hàm import matplotlib.pyplot as plt
def add_value_label[x_list,y_list]:
for i in range[1, len[x_list]+1]:
plt.text[i,y_list[i-1],y_list[i-1]]
no_of_students=[10,24,45,30,23,56,67,34,45,50]
class_number=[1,2,3,4,5,6,7,8,9,10]
plt.bar[class_number,no_of_students]
add_value_label[class_number,no_of_students]
plt.title["No of Students in a class"]
plt.xlabel["Class"]
plt.ylabel["Number of Students"]
plt.show[]
9, matplotlib.pyplot.text[x, y, s, fontdict=None, **kwargs]
90 và matplotlib.pyplot.text[x, y, s, fontdict=None, **kwargs]
91 tương ứngĐể thêm các nhãn giá trị, chúng ta sẽ định nghĩa một hàm
matplotlib.pyplot.text[x, y, s, fontdict=None, **kwargs]
92. Ở đây, matplotlib.pyplot.text[x, y, s, fontdict=None, **kwargs]
93 và matplotlib.pyplot.text[x, y, s, fontdict=None, **kwargs]
94 là danh sách chứa dữ liệu cho trục x và trục y i. e import matplotlib.pyplot as plt
def add_value_label[x_list,y_list]:
for i in range[1, len[x_list]+1]:
plt.text[i,y_list[i-1],y_list[i-1]]
no_of_students=[10,24,45,30,23,56,67,34,45,50]
class_number=[1,2,3,4,5,6,7,8,9,10]
plt.bar[class_number,no_of_students]
add_value_label[class_number,no_of_students]
plt.title["No of Students in a class"]
plt.xlabel["Class"]
plt.ylabel["Number of Students"]
plt.show[]
4 và matplotlib.pyplot.text[x, y, s, fontdict=None, **kwargs]
96Trong hàm
matplotlib.pyplot.text[x, y, s, fontdict=None, **kwargs]
97, chúng tôi sẽ chỉ định tọa độ của các nhãn giá trị là matplotlib.pyplot.text[x, y, s, fontdict=None, **kwargs]
98 để nhãn giá trị sẽ được thêm vào đầu biểu đồ thanh. Giá trị văn bản được chuyển đến hàm văn bản sẽ là chiều cao của biểu đồ thanh tại no_of_students=[10,24,45,30,23,56,67,34,45,50]
4. Ở đây, no_of_students=[10,24,45,30,23,56,67,34,45,50]
4 sẽ là các phần tử của import matplotlib.pyplot as plt
def add_value_label[x_list,y_list]:
for i in range[1, len[x_list]+1]:
plt.text[i,y_list[i-1],y_list[i-1]]
no_of_students=[10,24,45,30,23,56,67,34,45,50]
class_number=[1,2,3,4,5,6,7,8,9,10]
plt.bar[class_number,no_of_students]
add_value_label[class_number,no_of_students]
plt.title["No of Students in a class"]
plt.xlabel["Class"]
plt.ylabel["Number of Students"]
plt.show[]
4, và no_of_students=[10,24,45,30,23,56,67,34,45,50]
12 sẽ là số học sinh tương ứng của lớp no_of_students=[10,24,45,30,23,56,67,34,45,50]
4Chẳng hạn, chúng tôi sẽ thêm số lượng học sinh trong mỗi lớp dưới dạng nhãn giá trị vào đầu biểu đồ thanh
________số 8
đầu ra
Bạn có thể quan sát thấy rằng các nhãn giá trị không nằm ở trung tâm ở đầu biểu đồ thanh ở đầu ra. Chúng ta có thể làm như vậy bằng cách thao tác các đối số đầu vào như được thảo luận trong các phần sau
Thêm nhãn giá trị trên biểu đồ thanh Matplotlib ở giữa mỗi thanh
Để thêm các nhãn giá trị trên biểu đồ thanh Matplotlib ở giữa ở trên cùng của biểu đồ thanh, chúng tôi sẽ chỉ định căn chỉnh theo chiều ngang là
no_of_students=[10,24,45,30,23,56,67,34,45,50]
14 trong hàm import matplotlib.pyplot as plt
def add_value_label[x_list,y_list]:
for i in range[1, len[x_list]+1]:
plt.text[i,y_list[i-1],y_list[i-1]]
no_of_students=[10,24,45,30,23,56,67,34,45,50]
class_number=[1,2,3,4,5,6,7,8,9,10]
plt.bar[class_number,no_of_students]
add_value_label[class_number,no_of_students]
plt.title["No of Students in a class"]
plt.xlabel["Class"]
plt.ylabel["Number of Students"]
plt.show[]
1 như saumatplotlib.pyplot.text[x, y, s, fontdict=None, **kwargs]
9đầu ra
Thêm nhãn giá trị trên biểu đồ thanh Matplotlib ở giữa chiều cao của mỗi thanh
Bạn cũng có thể thêm nhãn giá trị ở giữa chiều cao của mỗi thanh. Đối với điều này, chúng ta phải chỉ định tọa độ
no_of_students=[10,24,45,30,23,56,67,34,45,50]
5 trong hàm import matplotlib.pyplot as plt
def add_value_label[x_list,y_list]:
for i in range[1, len[x_list]+1]:
plt.text[i,y_list[i-1],y_list[i-1]]
no_of_students=[10,24,45,30,23,56,67,34,45,50]
class_number=[1,2,3,4,5,6,7,8,9,10]
plt.bar[class_number,no_of_students]
add_value_label[class_number,no_of_students]
plt.title["No of Students in a class"]
plt.xlabel["Class"]
plt.ylabel["Number of Students"]
plt.show[]
1 là no_of_students=[10,24,45,30,23,56,67,34,45,50]
18. Bạn có thể thấy điều này trong mã ví dụ sauno_of_students=[10,24,45,30,23,56,67,34,45,50]
1đầu ra
Thêm nhãn giá trị trên biểu đồ thanh Matplotlib bằng hàm no_of_students=[10,24,45,30,23,56,67,34,45,50]
19
no_of_students=[10,24,45,30,23,56,67,34,45,50]
Thay vì hàm
import matplotlib.pyplot as plt
def add_value_label[x_list,y_list]:
for i in range[1, len[x_list]+1]:
plt.text[i,y_list[i-1],y_list[i-1]]
no_of_students=[10,24,45,30,23,56,67,34,45,50]
class_number=[1,2,3,4,5,6,7,8,9,10]
plt.bar[class_number,no_of_students]
add_value_label[class_number,no_of_students]
plt.title["No of Students in a class"]
plt.xlabel["Class"]
plt.ylabel["Number of Students"]
plt.show[]
1, chúng ta có thể sử dụng hàm no_of_students=[10,24,45,30,23,56,67,34,45,50]
19 để thêm nhãn giá trị trên biểu đồ thanh matplotlib. Cú pháp của hàm no_of_students=[10,24,45,30,23,56,67,34,45,50]
19 như sauno_of_students=[10,24,45,30,23,56,67,34,45,50]
2Đây,
- Tham số
23 là nhãn sẽ được thêm vào biểu đồno_of_students=[10,24,45,30,23,56,67,34,45,50]
- Tham số
24 chấp nhận một tupleno_of_students=[10,24,45,30,23,56,67,34,45,50]
25 trong đóno_of_students=[10,24,45,30,23,56,67,34,45,50]
4 vàno_of_students=[10,24,45,30,23,56,67,34,45,50]
5 là tọa độ nơi nhãn sẽ được thêm vào biểu đồno_of_students=[10,24,45,30,23,56,67,34,45,50]
- Hàm chấp nhận nhiều đối số khác nhau. Bài viết này sẽ chỉ sử dụng tham số
0 để thiết lập căn chỉnh ngang cho nhãn trong biểu đồ thanhimport matplotlib.pyplot as plt def add_value_label[x_list,y_list]: for i in range[1, len[x_list]+1]: plt.text[i,y_list[i-1],y_list[i-1]] no_of_students=[10,24,45,30,23,56,67,34,45,50] class_number=[1,2,3,4,5,6,7,8,9,10] plt.bar[class_number,no_of_students] add_value_label[class_number,no_of_students] plt.title["No of Students in a class"] plt.xlabel["Class"] plt.ylabel["Number of Students"] plt.show[]
Chúng ta sẽ vẽ biểu đồ thanh bằng hàm
import matplotlib.pyplot as plt
def add_value_label[x_list,y_list]:
for i in range[1, len[x_list]+1]:
plt.text[i,y_list[i-1],y_list[i-1]]
no_of_students=[10,24,45,30,23,56,67,34,45,50]
class_number=[1,2,3,4,5,6,7,8,9,10]
plt.bar[class_number,no_of_students]
add_value_label[class_number,no_of_students]
plt.title["No of Students in a class"]
plt.xlabel["Class"]
plt.ylabel["Number of Students"]
plt.show[]
5. Sau đó, chúng tôi sẽ thêm import matplotlib.pyplot as plt
def add_value_label[x_list,y_list]:
for i in range[1, len[x_list]+1]:
plt.text[i,y_list[i-1],y_list[i-1]]
no_of_students=[10,24,45,30,23,56,67,34,45,50]
class_number=[1,2,3,4,5,6,7,8,9,10]
plt.bar[class_number,no_of_students]
add_value_label[class_number,no_of_students]
plt.title["No of Students in a class"]
plt.xlabel["Class"]
plt.ylabel["Number of Students"]
plt.show[]
6, no_of_students=[10,24,45,30,23,56,67,34,45,50]
11 và import matplotlib.pyplot as plt
def add_value_label[x_list,y_list]:
for i in range[1, len[x_list]+1]:
plt.text[i,y_list[i-1],y_list[i-1]]
no_of_students=[10,24,45,30,23,56,67,34,45,50]
class_number=[1,2,3,4,5,6,7,8,9,10]
plt.bar[class_number,no_of_students]
add_value_label[class_number,no_of_students]
plt.title["No of Students in a class"]
plt.xlabel["Class"]
plt.ylabel["Number of Students"]
plt.show[]
8 vào biểu đồ bằng cách sử dụng các hàm import matplotlib.pyplot as plt
def add_value_label[x_list,y_list]:
for i in range[1, len[x_list]+1]:
plt.text[i,y_list[i-1],y_list[i-1]]
no_of_students=[10,24,45,30,23,56,67,34,45,50]
class_number=[1,2,3,4,5,6,7,8,9,10]
plt.bar[class_number,no_of_students]
add_value_label[class_number,no_of_students]
plt.title["No of Students in a class"]
plt.xlabel["Class"]
plt.ylabel["Number of Students"]
plt.show[]
9, matplotlib.pyplot.text[x, y, s, fontdict=None, **kwargs]
90 và matplotlib.pyplot.text[x, y, s, fontdict=None, **kwargs]
91 tương ứngĐể thêm các nhãn giá trị trên biểu đồ thanh Matplotlib, chúng ta sẽ xác định một hàm
matplotlib.pyplot.text[x, y, s, fontdict=None, **kwargs]
92. Ở đây, no_of_students=[10,24,45,30,23,56,67,34,45,50]
4 và no_of_students=[10,24,45,30,23,56,67,34,45,50]
5 là danh sách chứa dữ liệu cho trục x và trục y. Trong hàm matplotlib.pyplot.text[x, y, s, fontdict=None, **kwargs]
97, chúng ta sẽ chuyển các bộ dữ liệu được tạo từ dữ liệu được cung cấp cho tọa độ no_of_students=[10,24,45,30,23,56,67,34,45,50]
4 và no_of_students=[10,24,45,30,23,56,67,34,45,50]
5 làm đối số đầu vào cho tham số no_of_students=[10,24,45,30,23,56,67,34,45,50]
24. Cùng với đó, chúng ta sẽ chuyển các giá trị khác cho hàm no_of_students=[10,24,45,30,23,56,67,34,45,50]
33 để thêm nhãn giá trị trên biểu đồ thanh Matplotlib như sauno_of_students=[10,24,45,30,23,56,67,34,45,50]
1đầu ra
Một lần nữa, bạn có thể quan sát thấy rằng các nhãn không được căn chỉnh với biểu đồ thanh. Để căn chỉnh chúng, bạn có thể sử dụng tham số căn chỉnh ngang
import matplotlib.pyplot as plt
def add_value_label[x_list,y_list]:
for i in range[1, len[x_list]+1]:
plt.text[i,y_list[i-1],y_list[i-1]]
no_of_students=[10,24,45,30,23,56,67,34,45,50]
class_number=[1,2,3,4,5,6,7,8,9,10]
plt.bar[class_number,no_of_students]
add_value_label[class_number,no_of_students]
plt.title["No of Students in a class"]
plt.xlabel["Class"]
plt.ylabel["Number of Students"]
plt.show[]
0 để đặt căn chỉnh sauno_of_students=[10,24,45,30,23,56,67,34,45,50]
3đầu ra
Bạn cũng có thể thêm nhãn giá trị ở giữa chiều cao của mỗi thanh. Đối với điều này, chúng ta phải chỉ định tọa độ
no_of_students=[10,24,45,30,23,56,67,34,45,50]
5 trong các bộ dữ liệu là no_of_students=[10,24,45,30,23,56,67,34,45,50]
18. Bạn có thể thấy điều này trong mã ví dụ sauno_of_students=[10,24,45,30,23,56,67,34,45,50]
9đầu ra
Phần kết luận
Bài viết này đã thảo luận về hai cách để thêm nhãn giá trị trên biểu đồ thanh Matplotlib bằng hàm
import matplotlib.pyplot as plt
def add_value_label[x_list,y_list]:
for i in range[1, len[x_list]+1]:
plt.text[i,y_list[i-1],y_list[i-1]]
no_of_students=[10,24,45,30,23,56,67,34,45,50]
class_number=[1,2,3,4,5,6,7,8,9,10]
plt.bar[class_number,no_of_students]
add_value_label[class_number,no_of_students]
plt.title["No of Students in a class"]
plt.xlabel["Class"]
plt.ylabel["Number of Students"]
plt.show[]
1 và hàm no_of_students=[10,24,45,30,23,56,67,34,45,50]
33. Chúng tôi cũng đã thấy cách chúng tôi có thể quyết định căn chỉnh và vị trí của các nhãn bằng cách thay đổi tham số. Bạn có thể sử dụng bất kỳ cách nào đã thảo luận để thêm nhãn giá trị trên biểu đồ thanh Matplotlib tùy theo sự thuận tiện của bạn