Adding value labels on a matplotlib bar chart

As of matplotlib v3.4.2

  • Use matplotlib.pyplot.bar_label
    • The default label position, set with the parameter label_type, is 'edge'. To center the labels in the middle of the bar, use 'center'
    • Additional kwargs are passed to Axes.annotate, which accepts Text kwargs.
      • Properties like color, rotation, fontsize, etc., can be used.
  • See the matplotlib: Bar Label Demo page for additional formatting options.
  • Tested with pandas 1.3.3, which is using matplotlib 3.4.3 as the plot engine.
  • ax.containers is a list of BarContainer artists
    • With a single level bar plot, it’s a list of len 1, hence [0] is used.
    • For grouped and stacked bar plots there will be more objects in the list
    • See How to annotate each segment of a stacked bar chart or Stacked Bar Chart with Centered Labels.
    • For grouped bar examples
      • How to plot and annotate grouped bars in seaborn
      • How to plot and annotate a grouped bar chart
  • Simple label formatting can be done with the fmt parameter, as shown in the Demo examples and at How to annotate a seaborn barplot with the aggregated value.
  • More sophisticated label formatting should use the label parameter, as shown in the Demo examples and the following
    • stack bar plot in matplotlib and add label to each section
    • How to add multiple annotations to a barplot
    • How to plot a horizontal stacked bar with annotations
    • How to align annotations at the end of a horizontal bar plot
    • How to annotate a stacked bar plot and add legend labels
    • How to customize bar annotations to not show selected values
    • How to annotate bar plots when adding error bars
import pandas as pd

# dataframe using frequencies and x_labels from the OP
df = pd.DataFrame({'Frequency': frequencies}, index=x_labels)

# display(df)
          Frequency
108300.0          6
110540.0         16
112780.0         75
115020.0        160
117260.0        244

# plot
ax = df.plot(kind='bar', figsize=(12, 8), title="Amount Frequency",
             xlabel="Amount ($)", ylabel="Frequency", legend=False)

# annotate
ax.bar_label(ax.containers[0], label_type="edge")

# pad the spacing between the number and the edge of the figure
ax.margins(y=0.1)

enter image description here

  • Specify additional kwargs for additional customization
    • Accepts parameters from matplotlib.axes.Axes.text
ax.bar_label(ax.containers[0], label_type="edge", color="red", rotation=90, fontsize=7, padding=3)

enter image description here

Examples with bar_label

  • How to create and annotate a stacked proportional bar chart
  • How to annotate bar plots when adding error bars
  • How to calculate percent by row and annotate 100 percent stacked bars
  • Stacked bars are unexpectedly annotated with the sum of bar heights
  • How to plot and annotate grouped bars
  • How to annotate bar chart with values different to those from get_height()
  • Pandas bar how to label desired values
  • How to display percentage above grouped bar chart
  • How to customize bar annotations to not show selected values
  • How to set ticklabel rotation and add bar annotations
  • How to aggregate group metrics and plot data with pandas
  • How to plot a stacked bar with annotations for multiple groups
  • How to annotate a stackplot or area plot
  • How to plot grouped bars
  • How to add multiple data labels in a bar chart in matplotlib
  • Python matplotlib multiple bars
  • plt grid ALPHA parameter not working in matplotlib
  • Matplotlib pie chart label does not match value
  • How to wrap long tick labels in a seaborn figure-level plot
  • How to annotate barplot with percent by hue/legend group
  • How to add percentages on top of bars in seaborn
  • How to plot percentage with seaborn distplot / histplot / displot
  • How to annotate a stacked bar plot and add legend labels
  • How to plot grouped bars in the correct order
  • Problem with plotting two lists with different sizes using matplotlib
  • How to annotate only one category of a stacked bar plot
  • How to Increase subplot text size and add custom bar plot annotations
  • How to get a grouped bar plot of categorical data
  • How to create grouped bar plots in a single figure from a wide dataframe
  • How to determine if the last value in all columns is greater than n
  • How to plot element count and add annotations
  • Seaborn Catplot set values over the bars
  • Adding value labels on a matplotlib bar chart
  • How to annotate a seaborn barplot with the aggregated value
  • stack bar plot in matplotlib and add label to each section
  • How to annotate each segment of a stacked bar chart
  • How to plot and annotate a grouped bar chart
  • How to align annotations at the end of a horizontal bar plot
  • How to plot and annotate grouped bars in seaborn / matplotlib
  • How to horizontally center a bar plot annotation
  • How to plot a horizontal stacked bar with annotations
  • How to add multiple annotations to a barplot
  • Stacked Bar Chart with Centered Labels

Leave a Comment