# Annotate outside of plot

**URL:** <https://discourse.julialang.org/t/annotate-outside-of-plot/101696>\
**Category:** Visualization\
**Tags:** latex, plots, gr, latexstrings\
**Created:** [July 17, 2023, 2:58pm UTC](https://discourse.julialang.org/t/annotate-outside-of-plot/101696 "2023-07-17T14:58:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Fernandez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/james_fernandez/32/50665_2.png) [@James\_Fernandez](https://discourse.julialang.org/u/James_Fernandez)\
**Post date:** [July 17, 2023, 2:58pm UTC](https://discourse.julialang.org/t/annotate-outside-of-plot/101696/1 "2023-07-17T14:58:14Z")

</div>

Hello to all,

I would like to make some latex annotations in julia’s `Plots` with `gr` backend outside of the plot area.

I have this example:

```julia
using Plots, LaTeXStrings

gr()

a = 3
x = collect(0:0.01:10)
y = sin.(a*x)

plt = plot(x, y, 
            xlabel = "t[s]", 
            legend=:bottomright,
            legend_column=1
           )

scatter!(plt, x[1:3:end], y[1:3:end],
        markersize = 1,
        legend_column=2,
        )

```

And have the annotations like:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/6/e6c4ea8bfb44cc358c6402eb377d4763a01a159a.png)

How can I achieve this?

---

<div class="post-metadata">

**Author:** ![LeePhillips](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leephillips/32/205514_2.png) [@LeePhillips](https://discourse.julialang.org/u/LeePhillips)\
**Post date:** [July 17, 2023, 7:55pm UTC](https://discourse.julialang.org/t/annotate-outside-of-plot/101696/2 "2023-07-17T19:55:29Z")

</div>

Using the GR backend getting the LaTeX annotations outside the plot is pretty simple:

```julia
annot = L"\textrm{Velocity = }\frac{1}{\sqrt{2π}}\,e^{-x^2/(2σ^2)}" 

plot(x -> gs(x; σ=1); 
       annotation=(6, 0.2, annot), annotationfontsize=10,
       rightmargin=5cm, legend=false)
# gs is a Gaussian distribution

```

![f](https://global.discourse-cdn.com/julialang/original/3X/e/9/e9e98205e71777e46001043f20738f365dc5b7bb.png)

But, as you can see, although the math looks good, the kerning in the text sucks. This is because gr uses its own TeX engine instead of calling out to the real LaTeX; there will be other problems as well. If you want publication quality you can use PGFplotsX, but with this there doesn’t seem to be a straightforward way to place annotations outside the plot area (you will need step way outside the Plots interface and use pgfplots commands, or invent a kludge using subplots, etc.)

---

<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 17, 2023, 8:37pm UTC](https://discourse.julialang.org/t/annotate-outside-of-plot/101696/3 "2023-07-17T20:37:18Z")

</div>

You can use Plots’ grid layout to obtain:

 ![Plots_gr_side_legend_with_grid_layout](https://global.discourse-cdn.com/julialang/original/3X/f/6/f61095f40564549a36d43a1353b45b2075d3490e.png)

> **Plots.jl grid layout code**
>
> ```julia
> using LaTeXStrings, Measures, Plots; gr(dpi=600)
> 
> a = 3
> x = collect(0:0.01:10)
> y = sin.(a*x)
> 
> plt = plot(x, y, xlabel="t[s]", label=true, legend=:bottomright, legend_column=1)
> scatter!(plt, x[1:3:end], y[1:3:end], label=true, ms=1, legend_column=2, rightmargin=-10mm)
> 
> plt2 = plot((0,NaN), c=:transparent, fg_legend=:transparent, framestyle=:none,
> label=L"a=%$(a)" * "\n\n" * L"LaTeXStrings", legend=:left, leftmargin=0mm)
> 
> plot(plt, plt2, layout=grid(1, 2, widths=[0.7, 0.3]))
> 
> ```

---

<div class="post-metadata">

**Author:** ![James\_Fernandez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/james_fernandez/32/50665_2.png) [@James\_Fernandez](https://discourse.julialang.org/u/James_Fernandez)\
**Post date:** [July 19, 2023, 5:03pm UTC](https://discourse.julialang.org/t/annotate-outside-of-plot/101696/4 "2023-07-19T17:03:24Z")

</div>

Anyway to annotate latex matrices in there?

---

<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 19, 2023, 10:53pm UTC](https://discourse.julialang.org/t/annotate-outside-of-plot/101696/5 "2023-07-19T22:53:01Z")

</div>

I don’t know how to automatically plot LaTeX matrices in `Plots.jl`, only some manual approximation.  
Perhaps someone more versed in LaTeX or macros can automate it:

 ![Plots_gr_side_legend_with_grid_layout_and_LaTeX_pseudo_matrix](https://global.discourse-cdn.com/julialang/original/3X/e/a/eaa281706da5099849b42e25c9a21e31e3cb4f8f.png)

> **Plots.jl code for manual LaTeX matrix display**
>
> ```julia
> # (continuation of code in previous post)
> 
> Mlx = L"""
> a=%$(a)
> 
> $ M =$
> $\qquad \quad α \quad 0 \quad 1$
> $\qquad \quad 0 \quad β \quad 2$
> $\qquad \quad 1 \quad 2 \quad γ$
> """
> 
> plt2 = plot((0,NaN), c=:transparent, fg_legend=:transparent, framestyle=:none,
> label=Mlx, legend=:left, leftmargin=0mm)
> 
> plot(plt, plt2, layout=grid(1, 2, widths=[0.7, 0.3]))
> 
> ```
