# How to Convert this Code to For loop / Function?

**URL:** <https://discourse.julialang.org/t/how-to-convert-this-code-to-for-loop-function/87260>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [September 14, 2022, 8:19am UTC](https://discourse.julialang.org/t/how-to-convert-this-code-to-for-loop-function/87260 "2022-09-14T08:19:04Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [September 14, 2022, 8:19am UTC](https://discourse.julialang.org/t/how-to-convert-this-code-to-for-loop-function/87260/1 "2022-09-14T08:19:04Z")

</div>

Hi all,

I plot a series manually, then I thought I want to learn to use `for loop` / `function`. I don’t know which one is better.

The series are famous:

a\_{n} = \frac{(-1)^{n}}{n}

For n even it will be \frac{1}{n}

For n odd it will be - \frac{1}{n}

When I try this code below I am unable to use `&&` I can’t use `i` and `j`, moreover the graph does not even show up:

```julia
using Plots, LaTeXStrings
gr()

scatter([1], [1], color = "red", markersize = 4,
    legend=:outerright, label="", framestyle=:zerolines, 
    xlims = (0,30), xticks = 0:1:30, 
    ylims = (-2,2), yticks = -2:4:2, 
    linestyle=:dot, size=(720, 360))

for i in 1:2:33
    scatter!([i], [(-1)^(i)/i], color = "red", label="", markersize = 4)

end

for i in 2:2:33 
    scatter!([i], [(-1)^(i)/i], color = "red", label="", markersize = 4)
end

```

This is the manual code:

```julia
using Plots, LaTeXStrings
gr()

a, b = -10, 10

h(x) = 1

plot(h, a, b; legend=:outerright, label="", framestyle=:zerolines, 
    xlims = (0,10), xticks = 0:1:10, 
    ylims = (-2,2), yticks = -2:4:2, 
    linestyle=:dot, size=(720, 360))
plot!([1,0],[1,1], label="", linecolor=:green, linestyle=:dash)
plot!([2,0],[2,2], label="", linecolor=:green, linestyle=:dash)
plot!([3,0],[3,3], label="", linecolor=:green, linestyle=:dash)
plot!([4,0],[4,4], label="", linecolor=:green, linestyle=:dash)

scatter!([1], [-1], color = "red", label=L"a_{n}=\frac{1}{n}", markersize = 4)
scatter!([2], [1/2], color = "green", label=L"a_{n}=- \frac{1}{n}", markersize = 4)
scatter!([3], [-1/3], color = "red", label="", markersize = 4)
scatter!([4], [1/4], color = "green", label="", markersize = 4)
scatter!([5], [-1/5], color = "red", label="", markersize = 4)
scatter!([6], [1/6], color = "green", label="", markersize = 4)
scatter!([7], [-1/7], color = "red", label="", markersize = 4)
scatter!([8], [1/8], color = "green", label="", markersize = 4)
scatter!([9], [-1/9], color = "red", label="", markersize = 4)
scatter!([10], [1/10], color = "green", label="", markersize = 4)

annotate!([(5.1,0.2, (L"\mathbb{R}", 10, :black)), 
           (0.3,4.82, (L"\mathbb{R}", 10, :black))])

```

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [September 14, 2022, 8:45am UTC](https://discourse.julialang.org/t/how-to-convert-this-code-to-for-loop-function/87260/2 "2022-09-14T08:45:10Z")

</div>

take a look at the `display` function as follows

```julia
using Plots
using LaTeXStrings
gr()

p = scatter([1], [1], color = "red", markersize = 4,
   legend=:outerright, label="", framestyle=:zerolines, 
   xlims = (0,30), xticks = 0:1:30, 
   ylims = (-2,2), yticks = -2:4:2, 
   linestyle=:dot, size=(720, 360))

for i in 1:2:33
   scatter!([i], [(-1)^(i)/i], color = "red", label="", markersize = 4)
end

display(p)

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 14, 2022, 9:03am UTC](https://discourse.julialang.org/t/how-to-convert-this-code-to-for-loop-function/87260/3 "2022-09-14T09:03:59Z")

</div>

Do you really need to plot each point in a separate scatter plot? You can scatterplot all the points at once:

```julia
f(n::Integer) = (-1)^isodd(n) / n # or just use an anonymous function inside the plot command
p = scatter(1:33, f; color=:red, label=L"a_n=n", markersize=4)
display(p)

```

It’s better to do `(-1)^isodd(n)` or `(isodd(n) ? 1 : -1)` or something like that, instead of `(-1)^n`, which will take a long time, because it has to do a lot of power calculations.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 14, 2022, 10:01am UTC](https://discourse.julialang.org/t/how-to-convert-this-code-to-for-loop-function/87260/4 "2022-09-14T10:01:58Z")

</div>

Full code:

```julia
p = hline([1]; legend=:outerright, label="", framestyle=:zerolines, 
    xlims = (0,10), xticks = 0:1:10, 
    ylims = (-2,5.2), yticks = -2:4:2, 
    linestyle=:dot, size=(720, 360))
for i in 1:4
    plot!(p, [i, 0], [i, i], label="", linecolor=:green, linestyle=:dash)
end
scatter!(p, 1:33, n->(-1)^isodd(n)/n; color=:red, label=L"a_n=n", markesize=4)

annotate!(p, (5.1,0.2, (L"\mathbb{R}", 10, :black)), 
             (0.3,4.82, (L"\mathbb{R}", 10, :black)))
annotate!(p, [(-0.23, 0.08+i, (L"a_{%$i}", 10, :red)) for i in 1:4])
display(p)

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [September 14, 2022, 12:19pm UTC](https://discourse.julialang.org/t/how-to-convert-this-code-to-for-loop-function/87260/5 "2022-09-14T12:19:01Z")

</div>

(and no need to call display when the last line is a call to `annotate!` as that will return the plot object)

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [September 15, 2022, 2:46pm UTC](https://discourse.julialang.org/t/how-to-convert-this-code-to-for-loop-function/87260/6 "2022-09-15T14:46:47Z")

</div>

Hi @DNF

I gave wrong color before, I edit the code at my post above,

here is what I want to achieve with for loop / function:

 ![Capture d’écran_2022-09-15_21-44-40](https://global.discourse-cdn.com/julialang/original/3X/3/2/32d13b4ee0e65509c482ce831972296d0b6901c2.png)

I try your code:

 ![Capture d’écran_2022-09-15_21-45-33](https://global.discourse-cdn.com/julialang/original/3X/e/1/e170ffc860360e0aa290c085ff6b2db450645c78.png)

I want to ask how to make different color for `n even` the color of the scatter will be green ? and if `n odd` the color of the scatter will be red.

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [September 15, 2022, 2:47pm UTC](https://discourse.julialang.org/t/how-to-convert-this-code-to-for-loop-function/87260/7 "2022-09-15T14:47:50Z")

</div>

> [@bernhard](#):
>
> ```julia
> using Plots
> using LaTeXStrings
> gr()
> 
> p = scatter([1], [1], color = "red", markersize = 4,
> legend=:outerright, label="", framestyle=:zerolines, 
> xlims = (0,30), xticks = 0:1:30, 
> ylims = (-2,2), yticks = -2:4:2, 
> linestyle=:dot, size=(720, 360))
> 
> for i in 1:2:33
> scatter!([i], [(-1)^(i)/i], color = "red", label="", markersize = 4)
> end
> 
> display(p)
> 
> ```

the plot does not show up in Jupyter Notebook without `display(p)`

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [September 15, 2022, 2:49pm UTC](https://discourse.julialang.org/t/how-to-convert-this-code-to-for-loop-function/87260/8 "2022-09-15T14:49:21Z")

</div>

Here is the winner:

 ![Capture d’écran_2022-09-15_21-49-03](https://global.discourse-cdn.com/julialang/original/3X/5/e/5e129854050614bc7b7a839f6d3951bd77ec8b27.png)

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 15, 2022, 2:54pm UTC](https://discourse.julialang.org/t/how-to-convert-this-code-to-for-loop-function/87260/9 "2022-09-15T14:54:21Z")

</div>

There is little reason to make that many plots. Just make one plot for odd and one for even.

```julia
scatter!(p, 1:2:33, n->(-1)^isodd(n)/n; color=:red, label=L"a_n=1/n", markesize=4) 
scatter!(p, 2:2:33, n->(-1)^isodd(n)/n; color=:green, label=L"a_n=-1/n", markesize=4)

```

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [September 15, 2022, 3:31pm UTC](https://discourse.julialang.org/t/how-to-convert-this-code-to-for-loop-function/87260/10 "2022-09-15T15:31:08Z")

</div>

33 is just for fun number, when I try your label it only shows 1, the other code showing 33 label of the same

Anyway, why you use `isodd`?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 15, 2022, 4:11pm UTC](https://discourse.julialang.org/t/how-to-convert-this-code-to-for-loop-function/87260/11 "2022-09-15T16:11:28Z")

</div>

Do you want one label for each point? I thought you wanted one label for the odd values and one for the even ones.

> [@Freya\_the\_Goddess](#):
>
> Anyway, why you use `isodd`?

It’s not really needed anymore when you separate the even and odd points, but I used it instead of `(-1)^n`, because `(-1)^isodd(n)` is much more efficient.

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [September 16, 2022, 7:26am UTC](https://discourse.julialang.org/t/how-to-convert-this-code-to-for-loop-function/87260/12 "2022-09-16T07:26:57Z")

</div>

Alright, I get it so you use `isodd` because it is more efficient.

You are right, maybe my language is not efficient thus create misunderstanding.

I want one label for the odd values and another one for the even values, yours are right. Not one for each point, that is so not efficient.
