# How to add data labels to bar plot?

**URL:** <https://discourse.julialang.org/t/how-to-add-data-labels-to-bar-plot/56168>\
**Category:** General Usage\
**Tags:** question, plots\
**Created:** [February 28, 2021, 12:41am UTC](https://discourse.julialang.org/t/how-to-add-data-labels-to-bar-plot/56168 "2021-02-28T00:41:59Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [February 28, 2021, 7:21am UTC](https://discourse.julialang.org/t/how-to-add-data-labels-to-bar-plot/56168/3 "2021-02-28T07:21:27Z")

</div>

The following syntax seems to work fine with both `plotly()` and `gr()`, using `annotate` with automatic bottom alignment.

```julia
using Printf, Plots
x = [1,2,3,4,7]
y = rand(5)
str = [@sprintf("%.2f", yi) for yi in y]
Plots.bar(x,y, title="Annotated bar plot",legend=false)
annotate!(x,y, str, :bottom)

```

![Annotated_bar_plot](https://global.discourse-cdn.com/julialang/original/3X/9/d/9d71ade801bc08b80a86f77561d2f9d939fd9a66.png)

_PS: included also a trick shown [here](https://discourse.julialang.org/t/font-of-annotations/14998) to control the annotations’ font size, via tuples. But then the vertical positioning of the labels needs to be refined manually_

```julia
using Printf, Plots
x = [1,2,3,4,7]
y = rand(5)
str = [(@sprintf("%.2f", yi),10) for yi in y] # tuple with fontsize=10 (by: @pdeffebach)
(ymin,ymax) = extrema(y)
dy = 0.05*(ymax-ymin)
Plots.bar(x,y, title="Annotated bar plot",legend=false)
annotate!(x,y.+dy, str, ylim = (0,ymax+2dy))

```

![Annotated_bar_plot_with_font_size](https://global.discourse-cdn.com/julialang/original/3X/4/2/42a8e6efdc8880cb0e57d513d29892ac983e04e1.png)

---

_[View the full topic](https://discourse.julialang.org/t/how-to-add-data-labels-to-bar-plot/56168)._
