# 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:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ivan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ivan/32/208442_2.png) [@Ivan](https://discourse.julialang.org/u/Ivan)\
**Post date:** [February 28, 2021, 12:42am UTC](https://discourse.julialang.org/t/how-to-add-data-labels-to-bar-plot/56168/1 "2021-02-28T00:42:00Z")

</div>

I want to add data labels to the bars of my bar plot, as seen here:

![image](https://global.discourse-cdn.com/julialang/original/3X/e/9/e911bafd7615ab86bb4acb5244343bdf9356fc40.png)  
(chart and python code accessible [here](https://www.pythonprogramming.in/matplotlib/how-to-plot-a-very-simple-bar-chart-using-matplotlib.html)

How do I do it? Right now what I have is:

```julia
plotly()
y = rand(5)
x = string.([1,2,3,4,5])
using Plots
bar = plot(x, y, st = :bar, txt = x)

```

which returns:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/7/47bef66d73943a8ca8aa40853c6e3d1439d26d27.png)

Note that if I change `st = :bar` to `st = :scatter`, it works as expected:

```julia
plotly()
y = rand(5)
x = string.([1,2,3,4,5])
using Plots
scatter = plot(x, y, st = :scatter, txt = x)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/8/8878016ad77f23d54c87034b8878eec762d31981.png)  
(well, I’d like the labels to not overlap with the markers but I guess that is easy to change)

---

<div class="post-metadata">

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

</div>

I’m looking at a fix for this, but a workaround is to add

```julia
plot!(y .+ 0.1, st=:scatter,marker_shape=:none,primary=false,series_annotations=x)

```

---

<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)

---

<div class="post-metadata">

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

</div>

Took me a while to figure out why that works. Cool!

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [April 21, 2021, 5:34pm UTC](https://discourse.julialang.org/t/how-to-add-data-labels-to-bar-plot/56168/5 "2021-04-21T17:34:24Z")

</div>

For posterity: Now it works natively as your initial example.

---

<div class="post-metadata">

**Author:** ![jlnunes](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@jlnunes](https://discourse.julialang.org/u/jlnunes)\
**Post date:** [November 4, 2022, 7:00pm UTC](https://discourse.julialang.org/t/how-to-add-data-labels-to-bar-plot/56168/6 "2022-11-04T19:00:01Z")

</div>

It does work, but changing the fontsize does not seem to work like the other attributes. Any information on that and chaking the x/y location of the annotations?

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [December 12, 2022, 6:05pm UTC](https://discourse.julialang.org/t/how-to-add-data-labels-to-bar-plot/56168/7 "2022-12-12T18:05:56Z")

</div>

Does using `series_annotations=text.(strings, 13)` work? Either way this should probably be reported on GitHub. There’s a couple of issues related to ignored font arguments, perhaps tag on to one of those.
