Bạn có thể sử dụng chức năng Show
Đây là một ví dụ:
PS: Vì tuổi là phân phối riêng biệt, nên đặt rõ ràng các ranh giới thùng, ví dụ: Thêm nhãn vào các thanh biểu đồ trong matplotlib với các ví dụ mã Chào mọi người! Trong bài đăng này, chúng tôi sẽ điều tra cách khám phá câu trả lời để thêm nhãn vào các thanh biểu đồ trong matplotlib bằng ngôn ngữ máy tính. import numpy as np import pandas as pd import matplotlib.pyplot as plt # Bring some raw data. frequencies = [6, -16, 75, 160, 244, 260, 145, 73, 16, 4, 1] # In my original code I create a series and run on that, # so for consistency I create a series from the list. freq_series = pd.Series.from_array(frequencies) x_labels = [108300.0, 110540.0, 112780.0, 115020.0, 117260.0, 119500.0, 121740.0, 123980.0, 126220.0, 128460.0, 130700.0] # Plot the figure. plt.figure(figsize=(12, 8)) ax = freq_series.plot(kind='bar') ax.set_title('Amount Frequency') ax.set_xlabel('Amount ($)') ax.set_ylabel('Frequency') ax.set_xticklabels(x_labels) def add_value_labels(ax, spacing=5): """Add labels to the end of each bar in a bar chart. Arguments: ax (matplotlib.axes.Axes): The matplotlib object containing the axes of the plot to annotate. spacing (int): The distance between the labels and the bars. """ # For each bar: Place a label for rect in ax.patches: # Get X and Y placement of label from rect. y_value = rect.get_height() x_value = rect.get_x() + rect.get_width() / 2 # Number of points between bar and label. Change to your liking. space = spacing # Vertical alignment for positive values va = 'bottom' # If value of bar is negative: Place label below bar if y_value < 0: # Invert space to place label below space *= -1 # Vertically align label at top va = 'top' # Use Y value as label and format number with one decimal place label = "{:.1f}".format(y_value) # Create annotation ax.annotate( label, # Use `label` as label (x_value, y_value), # Place label at end of the bar xytext=(0, space), # Vertically shift label by `space` textcoords="offset points", # Interpret `xytext` as offset in points ha='center', # Horizontally center label va=va) # Vertically align label differently for # positive and negative values. # Call the function above. All the magic happens there. add_value_labels(ax) plt.savefig("image.png") Có nhiều cách để giải quyết vấn đề tương tự thêm nhãn vào các thanh biểu đồ trong matplotlib. Các giải pháp khác được khám phá dưới đây. # Bring some raw data. frequencies = [6, -16, 75, 160, 244, 260, 145, 73, 16, 4, 1] freq_series = pd.Series(frequencies) y_labels = [108300.0, 110540.0, 112780.0, 115020.0, 117260.0, 119500.0, 121740.0, 123980.0, 126220.0, 128460.0, 130700.0] # Plot the figure. plt.figure(figsize=(12, 8)) ax = freq_series.plot(kind='barh') ax.set_title('Amount Frequency') ax.set_xlabel('Frequency') ax.set_ylabel('Amount ($)') ax.set_yticklabels(y_labels) ax.set_xlim(-40, 300) # expand xlim to make labels easier to read rects = ax.patches # For each bar: Place a label for rect in rects: # Get X and Y placement of label from rect. x_value = rect.get_width() y_value = rect.get_y() + rect.get_height() / 2 # Number of points between bar and label. Change to your liking. space = 5 # Vertical alignment for positive values ha = 'left' # If value of bar is negative: Place label left of bar if x_value < 0: # Invert space to place label to the left space *= -1 # Horizontally align label at right ha = 'right' # Use X value as label and format number with one decimal place label = "{:.1f}".format(x_value) # Create annotation plt.annotate( label, # Use `label` as label (x_value, y_value), # Place label at end of the bar xytext=(space, 0), # Horizontally shift label by `space` textcoords="offset points", # Interpret `xytext` as offset in points va='center', # Vertically center label ha=ha) # Horizontally align label differently for # positive and negative values. plt.savefig("image.png") Như chúng ta đã thấy, một số lượng lớn các ví dụ đã được sử dụng để giải quyết các nhãn bổ sung vào các thanh biểu đồ trong vấn đề matplotlib có mặt. Làm cách nào để thêm nhãn vào thanh trong matplotlib?Các bước
Làm cách nào để thêm nhãn vào biểu đồ matplotlib?Nhãn và tiêu đề matplotlib
Làm thế nào để bạn dán nhãn biểu đồ thanh trong Python?Biểu đồ thanh được nhóm matplotlib với nhãn
Làm thế nào để bạn thêm một tiêu đề vào một biểu đồ trong Python?Matplotlib với Python
Làm thế nào để bạn thêm một nhãn vào một biểu đồ?Để cung cấp nhãn, hãy sử dụng các hàm set_xlabel () và set_ylabel (). Chúng tôi thêm nhãn vào mỗi thanh trong biểu đồ và đối với điều đó, chúng tôi lặp qua từng thanh và sử dụng hàm text () để thêm văn bản vào nó. Chúng tôi cũng tính toán chiều cao và chiều rộng của mỗi thanh để nhãn của chúng tôi không trùng với nhau.24-FEB-2021 Làm thế nào để bạn thêm nhãn vào biểu đồ thanh?Thêm nhãn dữ liệu Nhấp vào biểu đồ, sau đó nhấp vào tab Thiết kế Biểu đồ. Nhấp vào Thêm phần tử biểu đồ và chọn Nhãn dữ liệu, sau đó chọn một vị trí cho tùy chọn Nhãn Dữ liệu. Lưu ý: Các tùy chọn sẽ khác nhau tùy thuộc vào loại biểu đồ của bạn. Làm thế nào để bạn thêm nhãn dữ liệu?Trên tab Bố cục, trong nhóm nhãn, nhấp vào nhãn dữ liệu, sau đó nhấp vào tùy chọn mà bạn muốn. Để biết các tùy chọn nhãn dữ liệu bổ sung, nhấp vào tùy chọn nhãn dữ liệu khác, nhấp vào tùy chọn nhãn nếu không được chọn và sau đó chọn các tùy chọn bạn muốn. Làm cách nào để hiển thị tất cả các nhãn trong matplotlib?Để hiển thị tất cả các giá trị nhãn, chúng ta có thể sử dụng set_xticklabels () và set_yticklabels () Phương thức.11-may-2021 Làm thế nào để bạn tạo nhãn trong Python?Chương trình
3 nhãn của biểu đồ thanh là gì?Trục dọc ở bên trái hoặc bên phải của biểu đồ thanh được gọi là trục y. Trục ngang ở dưới cùng của biểu đồ thanh được gọi là trục x. Chiều cao hoặc chiều dài của các thanh biểu thị giá trị của dữ liệu. |