Last updated: April 29, 2025
Written by: Enes Zvornicanin
Reviewed by: Milos Simic
- Data Science
- Matplotlib
It's finally here:
>> The Road toMembership and Baeldung Pro.
Going into ads, no-ads reading, and bit about howBaeldung works if you're curious :)
1. Introduction
Matplotlib is a popular Python library for creating static, animated, and interactive visualizations.ÂUsing it, we can generate high-quality figures in various formats, making it an essential tool for data analysis and visualization in general.
In this tutorial, we’ll explore how to add value labels to bar plots in Matplotlib. Value labels are numbers on top of each bar in the bar plot. We’ll cover two approaches: using the Axes.annotate() and Axes.bar_label() functions.
2. Why Add Value Labels to a Bar Plot?
Showing value labels on a bar plot helps make the data more accessible and easier to interpret. Instead of forcing viewers to guess or trace values along the axis, labels provide exact numbers at a glance. This is especially useful in presentations, reports, and dashboards where we have multiple plots.
For instance, let’s take a simple bar chart without labels:

From the image above, it’s difficult to determine the exact values that the bars represent. For example, the first bar, A, has a value somewhere around 23, but we cannot be sure if it’s a whole number or a decimal. The last bar, D, is farther away from the y-axis on the left, and without a grid in the background, we would need to visually trace its value across the screen.
We can add value labels to highlight specific trends or differences between categories, and make our visualizations more clear and professional. This is especially helpful when presenting data to non-technical audiences.
3. Using the annotate() Function
The annotate() function in Matplotlib allows us to place text annotations anywhere on the plot. When it comes to bar charts, we can use it to manually add value labels by positioning the text at the top of each bar.
This method is especially useful when we need full control over the positioning, formatting, or style of the labels. For example, we can set labels slightly above the bar, change the font or color, and even add arrows or other decorations.
Below is a code that adds value labels with the annotate() function:
import matplotlib.pyplot as pltcategories = ['A', 'B', 'C', 'D']values = [23, 17, 35, 29]fig, ax = plt.subplots()bars = ax.bar(categories, values)# loop over bars and add annotationfor bar in bars: height = bar.get_height() ax.annotate( text = f'{height}', xy=(bar.get_x() + bar.get_width() / 2, height + 0.5), ha='center', )ax.set_title('Bar Plot with annotate() Labels')plt.show()
We loop through each bar in the plot, put the annotation relative to the xy spot, and center it horizontally. The drawback of this method is that we need to manually calculate the x and y coordinates for each bar. To get the x-coordinate, we add the bar’s starting x-position (the point where the bar begins from the left) to half of the bar’s width. In that way, the label is horizontally centered with respect to the current bar. To get the y-coordinate, we take the height of the bar and add a small margin, ensuring that the label appears slightly above the top of the bar.
After saving the bar plot, it looks like this:

Now, we see the exact values.
4. Using the bar_label() Function?
Starting from Matplotlib version 3.4, the library introduced the bar_label() function, which is designed specifically for adding labels to bars in a more straightforward way.
Instead of manually calculating positions or looping through bars, we simply pass the BarContainer returned from ax.bar() or ax.barh() to bar_label(). The function then automatically places the labels either inside, outside, or at the edge of the bars, depending on the options we choose. This method is great for quick labeling with less code, and it’s the preferred approach if we’re working with a recent version of Matplotlib and don’t need highly customized label placement.Â
Here’s an example:
import matplotlib.pyplot as pltcategories = ['A', 'B', 'C', 'D']values = [23, 17, 35, 29]fig, ax = plt.subplots()bars = ax.bar(categories, values)# add labels with bar_labelax.bar_label(bars, labels=[v for v in values], padding=1 )ax.set_title('Bar Plot with bar_label() Labels')plt.show()
In the example above, we use the function bar_label()Âto put the labels above the bars. The padding parameter controls the distance between labels and bars. The plot looks like this:

4.1. Optional Arguments
The bar_label() function in Matplotlib also supports several optional arguments that let us customize the appearance of the labels. Some of the most useful options include:
- padding – sets the distance between the bar and the label. A positive value moves the label away from the bar (upward for vertical bars, rightward for horizontal bars). A negative value moves the label closer to or even inside the bar.
- fontsize – changes the size of the label text.
- color – changes the color of the label text.
- weight – adjusts the font weight.
- rotation – rotates the label text by a given angle.
Hereâs an example that customizes the labels to be bold, yellow, inside the bars and slightly rotated:
import matplotlib.pyplot as pltcategories = ['A', 'B', 'C', 'D']values = [23, 17, 35, 29]fig, ax = plt.subplots()bars = ax.bar(categories, values)ax.bar_label(bars, labels=[f'{v}' for v in values], padding=-15, fontsize=12, color='yellow', weight='bold', rotation=20 )ax.set_title('Customized Bar Plot with Labels')plt.show()
The plot looks like this:

These options are useful when we have multiple plots and want to adjust the label style for each of them separately.
5. Conclusion
In this article, we presented how to add and value labels on a bar plot using Matplotlib.
The annotate() function offers greater control over label positioning and styling, making it ideal for complex or highly customized charts. On the other hand, bar_label() is simpler and more concise. It’s perfect for standard use cases and quick annotations.