# Adding annotations with custom font and color in plot recipes

**URL:** <https://discourse.julialang.org/t/adding-annotations-with-custom-font-and-color-in-plot-recipes/4740>\
**Category:** Visualization\
**Tags:** question, plotting, recipe\
**Created:** [July 8, 2017, 5:01pm UTC](https://discourse.julialang.org/t/adding-annotations-with-custom-font-and-color-in-plot-recipes/4740 "2017-07-08T17:01:07Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [July 8, 2017, 5:01pm UTC](https://discourse.julialang.org/t/adding-annotations-with-custom-font-and-color-in-plot-recipes/4740/1 "2017-07-08T17:01:07Z")

</div>

I have created a plot recipe for one of my types in [GeoStats.jl](https://github.com/juliohm/GeoStats.jl):

```julia
@recipe function f(γ::EmpiricalVariogram; bincounts=true)
  # get the data
  x, y, n = values(γ)

  # discard empty bins
  x = x[n .> 0]; y = y[n .> 0]; n = n[n .> 0]

  # draw bin counts as a measure of confidence
  if bincounts
    @series begin
      # plot a "frequency" instead of raw counts
      f = n*(maximum(y) / maximum(n)) / 10.

      seriestype := :bar
      fillalpha := .5
      color := :blue
      label := "bin counts"

      x, f
    end

    # annotate the bins
    @series begin
      seriestype := :scatter
      primary := false
      markersize := 0
      series_annotations := string.(n) # how to set the font size and color?

      x, zeros(x) + .001
    end
  end

  seriestype := :scatter
  xlim := (0, γ.maxlag)
  color --> :orange
  xlabel --> "Lag h"
  ylabel --> "Variogram(h)"
  label --> "variogram"

  x, y
end

```

It is working nicely, except that I don’t know how to set the font size and color for the annotations in the bins. Could someone help? Is it possible to control font and color without depending on Plots.jl? I would like to have RecipesBase.jl as my only dependency.

The problem is that with too many bins, the annotations overlap. If you know a better approach to annotating in this case, I’d love to hear.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [July 11, 2017, 12:20am UTC](https://discourse.julialang.org/t/adding-annotations-with-custom-font-and-color-in-plot-recipes/4740/2 "2017-07-11T00:20:08Z")

</div>

@mkborregaard could you please give a hand? Sorry pinging you, I couldn’t find another resource online to solve this issue by myself, and you are the only knowledgeable Plots.jl adviser I know of 🙂

---

<div class="post-metadata">

**Author:** ![MA\_Laforge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ma_laforge/32/385_2.png) [@MA\_Laforge](https://discourse.julialang.org/u/MA_Laforge)\
**Post date:** [July 12, 2017, 3:29am UTC](https://discourse.julialang.org/t/adding-annotations-with-custom-font-and-color-in-plot-recipes/4740/3 "2017-07-12T03:29:48Z")

</div>

Hi Julio,

I’m not really an expert with plot recipes, but I’ll give this a shot to try and help out @mkborregaard…

It appears that `series[:series_annotations]` is supposed to be a sort of iterable (collection) called `Plots.SeriesAnnotations`.

It also appears that the best way to construct this object is by using the `series_annotations()` function. I would therefore try replacing:

```julia
#Old code:
series_annotations := string.(n)

#New code:
series_annotations := Plots.series_annotations(string.(n), Plots.font("Sans", 12))
#NOTE: You can add more parameters to better control "font" (including a "Colorant")

```

Note that Plots would then use the same font for all annotations.

It also appears that if you want different fonts for each entry of `n`, then the first argument of `series_annotations()` must be a vector of `PlotText` (instead of a vector of `String`).

If you want to see how to construct a `PlotText` object (simple structure storing `String`, `Font`), than take a look at the components.jl file. The “constructor” function is called `text()` (defined just below definition of `PlotText`).

I would give you executable code… but like I said, I have never figured out how to build my own plot recipes… sorry.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [July 12, 2017, 8:44pm UTC](https://discourse.julialang.org/t/adding-annotations-with-custom-font-and-color-in-plot-recipes/4740/4 "2017-07-12T20:44:03Z")

</div>

Thank you very much @MA_Laforge, that is very helpful 🙂

So we can say that in order to control font size and color we have to depend on Plots.jl, correct?

---

<div class="post-metadata">

**Author:** ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)\
**Post date:** [July 13, 2017, 2:26am UTC](https://discourse.julialang.org/t/adding-annotations-with-custom-font-and-color-in-plot-recipes/4740/5 "2017-07-13T02:26:17Z")

</div>

If you qualify the references like this:

```julia
series_annotations := Main.Plots.series_annotations(string.(n), Main.Plots.font("Sans", 12))

```

you don’t need to have `using Plots` in your module, so it wouldn’t be a formal package dependency.

I sometimes use this trick to speed up development of my projects (no wait for Plots to load when I am working on computational code), but I don’t know if it violates any packaging rules.

---

<div class="post-metadata">

**Author:** ![MA\_Laforge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ma_laforge/32/385_2.png) [@MA\_Laforge](https://discourse.julialang.org/u/MA_Laforge)\
**Post date:** [July 13, 2017, 12:20pm UTC](https://discourse.julialang.org/t/adding-annotations-with-custom-font-and-color-in-plot-recipes/4740/6 "2017-07-13T12:20:49Z")

</div>

> @juliohm: So we can say that in order to control font size and color we have to depend on Plots.jl, correct?

I was not aware of this (Still a bit ignorant of how recipes work).

### My best guess:

Unless `RecipesBase` (or some other module) defines another similar structure to what I described above (that supposedly gets converted back to `Plots.series_annotations` for Plots.jl), then my guess is: yes… you _technically should_ depend on Plots.jl.

### However:

To me, this would seem to indicate that the definition of `series_annotations` should be moved to `RecipesBase` in order to avoid overly complex dependency trees.

### Update request:

@juliohm: could you please inform me whether this solution actually works. I am quite curious to know if there is not something else that needs to be addressed to get this working.

---

<div class="post-metadata">

**Author:** ![MA\_Laforge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ma_laforge/32/385_2.png) [@MA\_Laforge](https://discourse.julialang.org/u/MA_Laforge)\
**Post date:** [July 13, 2017, 12:21pm UTC](https://discourse.julialang.org/t/adding-annotations-with-custom-font-and-color-in-plot-recipes/4740/7 "2017-07-13T12:21:34Z")

</div>

@Ralph_Smith: Very interesting comment. I am quite surprised to learn this makes a difference.

I personally get the feeling it _is_ violating a packaging rule of some sort… but I am not certain either.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [July 13, 2017, 3:19pm UTC](https://discourse.julialang.org/t/adding-annotations-with-custom-font-and-color-in-plot-recipes/4740/8 "2017-07-13T15:19:53Z")

</div>

> [@Ralph\_Smith](#):
>
> If you qualify the references like this:
> 
> series\_annotations := Main.Plots.series\_annotations(string.(n), Main.Plots.font(“Sans”, 12))
> 
> you don’t need to have using Plots in your module, so it wouldn’t be a formal package dependency.
> 
> I sometimes use this trick to speed up development of my projects (no wait for Plots to load when I am working on computational code), but I don’t know if it violates any packaging rules.

That is a good trick @Ralph_Smith 🙂

@tkelman, could you share your thoughts on this? Does the trick violate the packaging rules? Having control over fonts and size would be very useful in my plot recipes.

---

<div class="post-metadata">

**Author:** ![tkelman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkelman/32/692_2.png) [@tkelman](https://discourse.julialang.org/u/tkelman)\
**Post date:** [July 13, 2017, 3:29pm UTC](https://discourse.julialang.org/t/adding-annotations-with-custom-font-and-color-in-plot-recipes/4740/9 "2017-07-13T15:29:06Z")

</div>

Will the code ever execute without Plots having been loaded? Note that the fact that all packages load into Main is likely to change in the future.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [July 13, 2017, 3:34pm UTC](https://discourse.julialang.org/t/adding-annotations-with-custom-font-and-color-in-plot-recipes/4740/10 "2017-07-13T15:34:04Z")

</div>

The code would only execute with Plots.jl installed, it is written inside of a plot recipe. I think the issue here is in the Plots/RecipesBase separation. Currently, font and size settings are not part of RecipesBase.

I will open an issue to make sure this is the case.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 19, 2017, 6:51pm UTC](https://discourse.julialang.org/t/adding-annotations-with-custom-font-and-color-in-plot-recipes/4740/11 "2017-07-19T18:51:50Z")

</div>

This is just a current Plots.jl limitation due to it using the `Font` type which is not in RecipesBase.jl. Honestly, the true answer is to get rid of that type to make font choices more similar to other arguments.

[https://github.com/JuliaPlots/Plots.jl/issues/555](https://github.com/JuliaPlots/Plots.jl/issues/555)

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [July 24, 2017, 11:04am UTC](https://discourse.julialang.org/t/adding-annotations-with-custom-font-and-color-in-plot-recipes/4740/12 "2017-07-24T11:04:12Z")

</div>

Nice idea, @ChrisRackauckas . Given your recent refactor it makes sense to try to put this functionality into RecipesBase too, if possible. I’d be happy to consider a PR.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 24, 2017, 4:04pm UTC](https://discourse.julialang.org/t/adding-annotations-with-custom-font-and-color-in-plot-recipes/4740/13 "2017-07-24T16:04:18Z")

</div>

> [@mkborregaard](#):
>
> Given your recent refactor it makes sense to try to put this functionality into RecipesBase too, if possible.

If it’s all standard attributes and no font object, then there shouldn’t be anything in RecipesBase needed for this to work.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [July 25, 2017, 9:19am UTC](https://discourse.julialang.org/t/adding-annotations-with-custom-font-and-color-in-plot-recipes/4740/14 "2017-07-25T09:19:54Z")

</div>

Hmm OK I’ll give it some thought.
