SoFunction
Updated on 2024-10-29

Python using matplotlib to add data labels to bar charts bar_label

0. Update matplotlib library

The subsequent experimental process of this paper are based on matplotlib version greater than or equal to 3.4.1, if the version is lower, it is not possible to implement the subsequent operations, how to update the version of the matplotlib library directly in Pycharm, please refer to the method:Updating third-party libraries with Pycharm using the tensorflow library as an example

1. Importing

Import directly into the library with the code:

import  as plt

2. Data preparation

Use list to prepare data in horizontal and vertical coordinates separately.

# Constructed data
X_set = [1, 2, 3, 4, 5]  # X-axis values
Y_set = [128, 211, 136, 234, 150]  # Yaxis data

3. Plotting bar charts

The drawing code is as follows:

p1 = (X_set, Y_set, width= 0.35, label='value')  # width indicates the width of the column
plt.bar_label(p1, label_type='edge')   # label_type='edge' means place the data value label at the top of the column, label_type='center' means place the data value label in the middle of the column.
('The distribution of XXX')
()

4. Mapping results

The results of the above mapping are as follows:

5. Complete Code

The full code is below:

import  as plt

# Constructed data
X_set = [1, 2, 3, 4, 5]
Y_set = [128, 211, 136, 234, 150]
p1 = (X_set, Y_set, width= 0.35, label='value')
plt.bar_label(p1, label_type='edge')
('The distribution of XXX')
()

6. bar_label() additional description of relevant parameters

Signature of the functionbecause of.bar_label(container, labels=None, *, fmt='%g', label_type='edge', padding=0, **kwargs)

The parameters of the function are:

  • (1)container:Container object for the column, usually the bar or barh function return value. .BarContainer object. Required parameters.
  •  (2)labels : List of labeled text. Class array object. Optional parameters. If None, the value is the data for the column (height of the column) formatted using the fmt parameter.
  •  (3)fmt:The format string for the label. String. The default value is '%g', i.e., format the label value as a floating point number.
  •  (4)label_type :The type of the label. The range is {'edge', 'center'} and the default value is 'edge'. For normal bar charts, this parameter is only used to control the position of the label, for stacked bar charts, different label types correspond to different label values.
    • (4.1)'edge': The label is located at the endpoint of the column. The value displayed is the position of the endpoint of the column. Notes! For stacked bar charts i.e. the total length of stacked multiple bars.
    • (4.2)'center': The label is located in the middle of the column. The value displayed is the length of the column.
  • (5)padding : The distance between the label and the column in pixels. Floating point number. The default value is 0.
  • (6)**kwargs:pass on toannotate()of the other parameters of the label. The return value is a list of Text objects for the label.

to this article on Python using matplotlib to add data labels bar_label () of the article is introduced to this, more related to matplotlib to add data labels to the bar chart content please search my previous posts or continue to browse the following articles I hope you will support me more in the future!