# Plots.jl annotation with group colours in grouped plot

**URL:** <https://discourse.julialang.org/t/plots-jl-annotation-with-group-colours-in-grouped-plot/88979>\
**Category:** Visualization\
**Tags:** gr, statsplots, plot, grouping\
**Created:** [October 19, 2022, 6:55pm UTC](https://discourse.julialang.org/t/plots-jl-annotation-with-group-colours-in-grouped-plot/88979 "2022-10-19T18:55:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![klwlevy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klwlevy/32/25272_2.png) [@klwlevy](https://discourse.julialang.org/u/klwlevy)\
**Post date:** [October 19, 2022, 6:55pm UTC](https://discourse.julialang.org/t/plots-jl-annotation-with-group-colours-in-grouped-plot/88979/1 "2022-10-19T18:55:02Z")

</div>

Dear Plots.jl wizards,

could someone give me advice how to best get annotations in the same colour as the lines when using the group option?

The following is a working example:

```julia
using DataFrames, Plots
gr(size=(1200, 800))

dates = [Date(2022, 10, 17), Date(2022, 10, 18), Date(2022, 10, 19)]

baln_df = DataFrame(
    date_ = dates,
    return_index = [1, 1.001407, 1.016973],
    annotation_return = ["", "", "1.7%"],
    stock = "BALN",
)

vow_df = DataFrame(
    date_ = dates,
    stock = "VOW",
    return_index = [1, 0.997758, 1.008594],
    annotation_return = ["", "", "0.86%"],
)

data_df = vcat(baln_df, vow_df)

p1 = plot(
    data_df.date_,
    data_df.return_index,
    group = data_df.stock,
    legend = :topleft,
    series_annotation = data_df.annotation_return,
)`Preformatted text`

```

 ![p1](https://global.discourse-cdn.com/julialang/original/3X/e/2/e2a7d13568c8ae719f21ef6ecb518f87378732b0.png)

I realize I can do the following, but maybe there is something more elegant and reliable?

```julia
anno_df = subset(data_df,
    :date_ => ByRow(d -> d == Date(2022, 10, 19))
)

p2 = plot(
    data_df.date_,
    data_df.return_index,
    group = data_df.stock,
    legend = :topleft,
)

for i in 1:length(p2.series_list)
    plot!(
        p2,
        annotations = (
            anno_df.date_[i],
            anno_df.return_index[i],
            Plots.text("$(anno_df.annotation_return[i])", color=p2[1][i][:linecolor], :center)
        )
    )
end

```

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

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [October 19, 2022, 9:58pm UTC](https://discourse.julialang.org/t/plots-jl-annotation-with-group-colours-in-grouped-plot/88979/2 "2022-10-19T21:58:47Z")

</div>

If I want to get colors right across multiple aspects of the plot, What I’ve been doing is grabbing a color palette I like and providing the colors myself rather than using groups.

---

<div class="post-metadata">

**Author:** ![klwlevy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klwlevy/32/25272_2.png) [@klwlevy](https://discourse.julialang.org/u/klwlevy)\
**Post date:** [October 19, 2022, 10:23pm UTC](https://discourse.julialang.org/t/plots-jl-annotation-with-group-colours-in-grouped-plot/88979/3 "2022-10-19T22:23:12Z")

</div>

Well, that is another workaround, for sure. I am looking for a general solution.

Thanks

---

<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:** [October 20, 2022, 6:53am UTC](https://discourse.julialang.org/t/plots-jl-annotation-with-group-colours-in-grouped-plot/88979/4 "2022-10-20T06:53:11Z")

</div>

An alternative, perhaps simpler, is to use StatsPlots and to preprocess the annotation colors before calling `plot()`:

```julia
using StatsPlots

g = groupby(data_df, :stock)
n, m = length(g), nrow(first(g))
colors = permutedims(palette(:default)[1:n])
cmat = reduce(vcat, fill.(colors[1:n],m))

@df data_df plot(:date_, :return_index, group=:stock, legend=:topleft, c=colors[:,1:n],
    series_annotation=text.(:annotation_return, cmat, 11, :bottom, "Computer Moderna")
)

```

 ![StatsPlots_plot_groupby_series_annotation_color](https://global.discourse-cdn.com/julialang/original/3X/2/a/2a4c50f657d660b377c7bd5b42b14650a7b9c098.png)

---

<div class="post-metadata">

**Author:** ![klwlevy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klwlevy/32/25272_2.png) [@klwlevy](https://discourse.julialang.org/u/klwlevy)\
**Post date:** [October 20, 2022, 3:32pm UTC](https://discourse.julialang.org/t/plots-jl-annotation-with-group-colours-in-grouped-plot/88979/5 "2022-10-20T15:32:09Z")

</div>

Thanks a lot Rafael!

Your answer taught me a lot so thank you very much.

As far as I can tell, your solution is not really dependant on StatsPlots, i.e. I would consider this the solution:

```julia
using DataFrames, Plots
gr(size=(1200, 800))

dates = [Date(2022, 10, 17), Date(2022, 10, 18), Date(2022, 10, 19)]

baln_df = DataFrame(
    date_ = dates,
    return_index = [1, 1.001407, 1.016973],
    annotation_return = ["", "", "1.7%"],
    stock = "BALN",
)

vow_df = DataFrame(
    date_ = dates,
    stock = "VOW",
    return_index = [1, 0.997758, 1.008594],
    annotation_return = ["", "", "0.86%"],
)

data_df = vcat(baln_df, vow_df)

g = groupby(data_df, :stock)
n, m = length(g), nrow(first(g))
colors = permutedims(palette(:default)[1:n])
cmat = reduce(vcat, fill.(colors[1:n],m))

p3 = plot(
    data_df.date_,
    data_df.return_index,
    group = data_df.stock,
    legend = :topleft,
    series_annotation = text.(data_df.annotation_return, cmat, :bottom, "Computer Moderna", 11)
)

```

 ![p3](https://global.discourse-cdn.com/julialang/original/3X/6/c/6c519f53f8091e76b807f941d7cca3e361307e21.png)

---

<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:** [October 20, 2022, 3:56pm UTC](https://discourse.julialang.org/t/plots-jl-annotation-with-group-colours-in-grouped-plot/88979/6 "2022-10-20T15:56:25Z")

</div>

> [@klwlevy](#):
>
> is not really dependant on StatsPlots

Of course not. It just provides shorter syntax and it might be the right place for a PR.
