# Math In Plots.jl

**URL:** <https://discourse.julialang.org/t/math-in-plots-jl/14930>\
**Category:** Visualization\
**Created:** [September 14, 2018, 2:41am UTC](https://discourse.julialang.org/t/math-in-plots-jl/14930 "2018-09-14T02:41:15Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![iwelch](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@iwelch](https://discourse.julialang.org/u/iwelch)\
**Post date:** [September 14, 2018, 2:41am UTC](https://discourse.julialang.org/t/math-in-plots-jl/14930/1 "2018-09-14T02:41:16Z")

</div>

could someone please show me how to set a simple equation into a plot, say \sqrt{\frac{a}{b}} ?

---

<div class="post-metadata">

**Author:** ![Sleort](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sleort/32/4875_2.png) [@Sleort](https://discourse.julialang.org/u/Sleort)\
**Post date:** [September 14, 2018, 3:15am UTC](https://discourse.julialang.org/t/math-in-plots-jl/14930/2 "2018-09-14T03:15:26Z")

</div>

```julia
julia> using Plots, LaTeXStrings

julia> x = rand(10); y = rand(10);

julia> s = L"\sqrt{\frac{a}{b}}";

julia> ann = [(x[i],y[i],s) for i ∈ 1:10];

julia> scatter(x, y, title = s, label = s, xaxis = s, yaxis = s, annotations = ann)

```

---

<div class="post-metadata">

**Author:** ![ethanminfang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ethanminfang/32/10511_2.png) [@ethanminfang](https://discourse.julialang.org/u/ethanminfang)\
**Post date:** [October 2, 2019, 1:31am UTC](https://discourse.julialang.org/t/math-in-plots-jl/14930/3 "2019-10-02T01:31:12Z")

</div>

Hi Sleort,

That’s very useful. I have a follow up question. what if a and b are some parameters values and I am running a loop over a and b to plot many graphs which I would be happy to put the value of a and b on graph. Could we do that in Julia?

Best,  
Ethan

---

<div class="post-metadata">

**Author:** ![Sleort](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sleort/32/4875_2.png) [@Sleort](https://discourse.julialang.org/u/Sleort)\
**Post date:** [October 2, 2019, 2:44am UTC](https://discourse.julialang.org/t/math-in-plots-jl/14930/4 "2019-10-02T02:44:43Z")

</div>

Do you mean something like this:

```julia
using Plots, LaTeXStrings, Printf

makenicenumberstring(n) = Printf.@sprintf("%.2f", n)
a = rand(10)
b = rand(10)
astr = makenicenumberstring.(a)
bstr = makenicenumberstring.(b)

x = rand(10)
y = rand(10)

s = L"\sqrt{\frac{a}{b}}"
sab = map(ab -> latexstring("\\sqrt{\\frac{$(ab[1])}{$(ab[2])}}"), zip(astr, bstr))
ann = tuple.(x,y,sab)

scatter(x, y, title = s, label = s, xaxis = s, yaxis = s, annotations = ann)

```

? (See the [documentation of LaTeXStrings.jl](https://github.com/stevengj/LaTeXStrings.jl) for why you need to use `latexstring` instead of just `L`.)

However, if you care about beauty (e.g. for publication), I would suggest that you have a look at [PGFPlotsX.jl](https://github.com/KristofferC/PGFPlotsX.jl) as well…

---

<div class="post-metadata">

**Author:** ![ethanminfang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ethanminfang/32/10511_2.png) [@ethanminfang](https://discourse.julialang.org/u/ethanminfang)\
**Post date:** [October 2, 2019, 2:47am UTC](https://discourse.julialang.org/t/math-in-plots-jl/14930/5 "2019-10-02T02:47:21Z")

</div>

yes! that’s it! very cool! thanks!

---

<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:** [October 2, 2019, 7:26am UTC](https://discourse.julialang.org/t/math-in-plots-jl/14930/6 "2019-10-02T07:26:28Z")

</div>

> [@Sleort](#):
>
> However, if you care about beauty (e.g. for publication), I would suggest that you have a look at [PGFPlotsX.jl](https://github.com/KristofferC/PGFPlotsX.jl) as well…

Plots can write to a pgfplots backend as well, so you can get exactly the same end results. The big difference is the syntax used to create the plots. PGFPlotsX tries to be as close to the original pgfplots syntax as possible. Plots tries to abstract that as well and hide differences between plotting backends.

---

<div class="post-metadata">

**Author:** ![Sleort](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sleort/32/4875_2.png) [@Sleort](https://discourse.julialang.org/u/Sleort)\
**Post date:** [October 2, 2019, 8:19am UTC](https://discourse.julialang.org/t/math-in-plots-jl/14930/7 "2019-10-02T08:19:50Z")

</div>

Sure, but can you access “lower level functionality” (like TikZ) by using Plots?

---

<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:** [October 2, 2019, 8:29am UTC](https://discourse.julialang.org/t/math-in-plots-jl/14930/8 "2019-10-02T08:29:54Z")

</div>

No, you get much more control by using pgfplots(x) directly. So my remark was only on the notion that pgfplotsx created inherently more beautiful results.

---

<div class="post-metadata">

**Author:** ![Sleort](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sleort/32/4875_2.png) [@Sleort](https://discourse.julialang.org/u/Sleort)\
**Post date:** [October 2, 2019, 8:51am UTC](https://discourse.julialang.org/t/math-in-plots-jl/14930/9 "2019-10-02T08:51:22Z")

</div>

Fair enough.

Hmm… yeah, I can see that I should have been more explicit on what I had in mind. What I meant to say was that if [ethanminfang](https://discourse.julialang.org/u/ethanminfang) would like to do something like [this](https://tex.stackexchange.com/a/170361) (or beyond), it may be better to use (something like) PGFPlotsX.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 2, 2019, 9:09am UTC](https://discourse.julialang.org/t/math-in-plots-jl/14930/10 "2019-10-02T09:09:43Z")

</div>

One way to think about PGFPlotsX is as a DSL that allows you to interpolate representations of various Julia objects into LaTeX code.

The disadvantage is that you don’t get the nice, generic, mostly backend-independent syntax of Plots.jl.

The advantage is that porting pgfplots examples found online or in the manual is very easy.

---

<div class="post-metadata">

**Author:** ![ethanminfang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ethanminfang/32/10511_2.png) [@ethanminfang](https://discourse.julialang.org/u/ethanminfang)\
**Post date:** [October 2, 2019, 12:58pm UTC](https://discourse.julialang.org/t/math-in-plots-jl/14930/11 "2019-10-02T12:58:01Z")

</div>

haha. No, actually. I just want to put the parameter values on the plot so I gets to know them if I have too many plots (like hundreds)

---

<div class="post-metadata">

**Author:** ![Sleort](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sleort/32/4875_2.png) [@Sleort](https://discourse.julialang.org/u/Sleort)\
**Post date:** [October 3, 2019, 12:22am UTC](https://discourse.julialang.org/t/math-in-plots-jl/14930/12 "2019-10-03T00:22:52Z")

</div>

If you only want to put parameter values on the plot – and don’t need the Latex typesetting – this is easier:

```julia
using Plots, Printf

a = rand(10)
b = rand(10)
x = rand(10)
y = rand(10)

sab = [@sprintf("(%.2f, %.2f)", ab...) for ab ∈ zip(a,b)]
ann = tuple.(x, y, sab)
scatter(x, y, annotations = ann)

```
