Làm cách nào để bạn thêm số vào biểu đồ thanh trong python?

nhập khẩu cốt truyện. graph_objects as go years = [1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012] fig = go. Hình [] hình. add_trace[đi. Thanh[x=năm, y=[219, 146, 112, 127, 124, 180, 236, 207, 236, 263, 350, 430, 474, 526, 488, 537, 500, 439], tên='Phần còn lại . add_trace[đi. Thanh[x=năm, y=[16, 13, 10, 11, 28, 37, 43, 55, 56, 88, 105, 156, 270, 299, 340, 403, 549, 499], name='Trung Quốc . update_layout[ title='Xuất khẩu phế liệu nhựa của Mỹ', xaxis_tickfont_size=14, yaxis=dict[ title='USD [triệu]', titlefont_size=16, tickfont_size=14, ], legend=dict[ x=0, y=1. 0, bgcolor='rgba[255, 255, 255, 0]', bordercolor='rgba[255, 255, 255, 0]' ], barmode='group', bargap=0. 15, # khoảng cách giữa các thanh của tọa độ vị trí liền kề. khoảng cách nhóm = 0. 1 # khoảng cách giữa các thanh có cùng tọa độ vị trí. ] quả sung. trình diễn[]

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

  • no_of_students=[10,24,45,30,23,56,67,34,45,50]
    
    3. Tọa độ x của các thanh
  • no_of_students=[10,24,45,30,23,56,67,34,45,50]
    
    4. Chiều cao của các thanh
  • no_of_students=[10,24,45,30,23,56,67,34,45,50]
    
    5. [Các] chiều rộng của các thanh
  • no_of_students=[10,24,45,30,23,56,67,34,45,50]
    
    6. Màu sắc của các mặt thanh
  • no_of_students=[10,24,45,30,23,56,67,34,45,50]
    
    7. Nhãn của các thanh

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 thanh

Nế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

Để 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[]
1

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 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ư sau

matplotlib.pyplot.text[x, y, s, fontdict=None, **kwargs]

Đây,

  • Tham số
    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 độ 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ản
  • Tham số
    no_of_students=[10,24,45,30,23,56,67,34,45,50]
    
    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]
    
    8
  • Tham số
    no_of_students=[10,24,45,30,23,56,67,34,45,50]
    
    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 đồ thanh

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[]
1

Bâ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]
96

Trong 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]
4

Chẳ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ư sau

matplotlib.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ụ sau

no_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

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ư sau

no_of_students=[10,24,45,30,23,56,67,34,45,50]
2

Đây,

  • Tham số
    no_of_students=[10,24,45,30,23,56,67,34,45,50]
    
    23 là nhãn sẽ được thêm vào biểu đồ
  • Tham số
    no_of_students=[10,24,45,30,23,56,67,34,45,50]
    
    24 chấp nhận một tuple
    no_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 đồ
  • 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ố
    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 để thiết lập căn chỉnh ngang cho nhãn trong biểu đồ thanh

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ư sau

no_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 sau

no_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ụ sau

no_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

Chủ Đề