In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use matplotlib to add data tags bar_label () to bar charts in Python." interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to use matplotlib in Python to add data tags to bar charts bar_label ()"!
0. Update the matplotlib library
The subsequent experiments in this article are based on the fact that the matplotlib version is greater than or equal to 3.4.1. If the version is lower, it is impossible to perform subsequent operations. How to update the version of the matplotlib library directly in Pycharm, please refer to the method: take the tensorflow library as an example, update the third-party library with Pycharm.
1. Import library
Import the matplotlib.pyplot library directly, the code is:
Import matplotlib.pyplot as plt2. Data preparation
Prepare Abscissa and ordinate data with list.
# Construction data X_set = [1,2,3,4,5] # X axis values Y_set = [128,211,136,234,150] # Y axis data 3. Draw a bar chart
The drawing code is as follows:
P1 = plt.bar (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 to place the data value label on the top of the column, and label_type='center' means to place the data value label in the middle of the column. Plt.title ('The distribution of XXX') plt.show () 4. Drawing result
The above drawing results are as follows:
5. Complete code
The complete code is as follows:
Import matplotlib.pyplot as plt# construction data X_set = [1,2,3,4,5] Y_set = [128,211,136,234,150] p1 = plt.bar (X_set, Y_set, width= 0.35,label='value') plt.bar_label (p1, label_type='edge') plt.title ('The distribution of XXX') plt.show () 6.bar_label () related parameters
The signature of the function is matplotlib.pyplot.bar_label (container, labels=None, *, fmt='%g', label_type='edge', padding=0, * * kwargs).
The arguments to the function are:
(1) container: the container object of a column, which is usually returned by the bar or barh function. .BarContainer object. Required parameters.
(2) labels: list of label text. Class array object. Optional parameters. If None, the value is the data (height of the column) of the column formatted with the fmt parameter.
(3) fmt: the format string of the tag. String. The default value is'% g', which formats the label value as a floating-point number.
(4) label_type: tag type. The value range is {'edge',' center'}, and the default value is' edge'. For normal bar charts, this parameter is only used to control the location of labels, and for stacked bar charts, different label types correspond to different label values.
The 'edge': tag is located at the end of the column. The displayed value is the end position of the column. Be careful! For a stacked column chart, that is, the total length of multiple columns stacked.
The center': tag is located in the middle of the column. The displayed value 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: other parameters passed to annotate (). Returns a list of Text objects whose value is a label.
At this point, I believe you have a deeper understanding of "how to use matplotlib in Python to add data tags to bar charts bar_label ()". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.