# How to change \`annotate!\` background color?

**URL:** <https://discourse.julialang.org/t/how-to-change-annotate-background-color/40606>\
**Category:** New to Julia\
**Tags:** plotting\
**Created:** [June 2, 2020, 11:18am UTC](https://discourse.julialang.org/t/how-to-change-annotate-background-color/40606 "2020-06-02T11:18:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![takuizum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/takuizum/32/12040_2.png) [@takuizum](https://discourse.julialang.org/u/takuizum)\
**Post date:** [June 2, 2020, 11:18am UTC](https://discourse.julialang.org/t/how-to-change-annotate-background-color/40606/1 "2020-06-02T11:18:01Z")

</div>

I’m trying to use Plots and `annotate` function to plot a numerical value onto the line.

```julia
plotly()
plot(sin)
annotate!(0, sin(0), text("0"))

```

It works not bad.

Furthermore, I want to put a text on the line with removing a part of a line on which to put a text.  
I used a white square Unicode to fill a part of the line, and plot a text on it.

```julia
plotly()
plot(sin)
annotate!(0, sin(0), text("■", :white, 30))
annotate!(0, sin(0), text("0"))

```

Is there any more sophisticated way?  
i.e. Control a background color of a text.

---

<div class="post-metadata">

**Author:** ![NoFishLikeIan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nofishlikeian/32/20917_2.png) [@NoFishLikeIan](https://discourse.julialang.org/u/NoFishLikeIan)\
**Post date:** [July 26, 2021, 5:10pm UTC](https://discourse.julialang.org/t/how-to-change-annotate-background-color/40606/2 "2021-07-26T17:10:20Z")

</div>

Late reply but I could not find an answer so I put together a short hack.

```julia
function annotatewithbox!(
		fig::Plots.Plot,
		text::Plots.PlotText,
		x::Real, y::Real, Δx::Real, Δy::Real = Δx;
		kwargs...)
	
	box = Plots.Shape(:rect)
	
	Plots.scale!(box, Δx, Δy)
	Plots.translate!(box, x, y)

	Plots.plot!(fig, box, c = :white, linestroke = :black, label = false; kwargs...)
	Plots.annotate!(fig, x, y, text)
	
	fig
end

```

There is definitely a better way to do it but it does the trick,

```julia
fig = plot(xlim = (-10, 10), ylims = (-10, 10))
annotatewithbox!(fig, text("hello how are you?!"), 2, 2, 4, 1)

```

 ![test](https://global.discourse-cdn.com/julialang/original/3X/9/3/935abaabc8d0773536701a014907aa125f7ee0a3.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:** [July 26, 2021, 7:12pm UTC](https://discourse.julialang.org/t/how-to-change-annotate-background-color/40606/3 "2021-07-26T19:12:01Z")

</div>

FWIW, the workaround from @takuizum could be further improved by using a `FULL BLOCK` Unicode character instead of the `BLACK SQUARE`:

```julia
using Plots; gr()
fntcm = "Computer Modern"
fntsz = 12
default(fontfamily= fntcm)
str = "Hello Julia!"
n = length(str)
str0 = "█"^(n÷2+1) # FULL BLOCK Unicode character
plot(sin, legend = false)
annotate!(0, sin(0), text(str0,:white, fntcm,fntsz+2))
annotate!(0, sin(0), text(str, fntcm,fntsz))

```

---

<div class="post-metadata">

**Author:** ![abehersan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abehersan/32/47221_2.png) [@abehersan](https://discourse.julialang.org/u/abehersan)\
**Post date:** [October 11, 2023, 5:03pm UTC](https://discourse.julialang.org/t/how-to-change-annotate-background-color/40606/4 "2023-10-11T17:03:17Z")

</div>

One case where this hack breaks down is in the case where the string is multi-line.  
In which case, `n = length(str)` gives a spuriously large value and even worse, the “background” will not cover the whole text area.

See:

 ![Screenshot 2023-10-11 at 19.01.10](https://global.discourse-cdn.com/julialang/original/3X/7/a/7abe8fec318fefcec1bcc2ccde978eeefc5826eb.png)

@takuizum of @rafael.guerra did you file an issue in the `Plots.jl` repo? I think it makes sense to do.

---

<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 11, 2023, 6:53pm UTC](https://discourse.julialang.org/t/how-to-change-annotate-background-color/40606/5 "2023-10-11T18:53:21Z")

</div>

I did not fill an issue.

Note that if we use a monospaced font such as “Courier”, then the character masking will be much easier to perform. See your example reproduced below, quick and dirty.

> **CODE: multi-line mask with monospaced font**
>
> ```julia
> using Plots; gr()
> 
> fntcm = "Courier"
> fntsz = 8
> default(fontfamily= fntcm)
> str = "Hello Julia!\nThis is a second line\nand a third one!!!\nThis will result in a ton of whitespace"
> n = length.(eachline(IOBuffer(str)))
> str0 = join(hcat("█".^n), "\n") # FULL BLOCK Unicode character
> plot(sin, legend = false)
> annotate!(0, sin(0), text(str0,:white, fntcm, fntsz))
> annotate!(0, sin(0), text(str, fntcm, fntsz))
> 
> ```
