# Ask Whether The Plot Step function variation $x + \\lfloor x \\rfloor$ is Correct

**URL:** <https://discourse.julialang.org/t/ask-whether-the-plot-step-function-variation-x-lfloor-x-rfloor-is-correct/90529>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [November 20, 2022, 7:51am UTC](https://discourse.julialang.org/t/ask-whether-the-plot-step-function-variation-x-lfloor-x-rfloor-is-correct/90529 "2022-11-20T07:51:51Z")\
**Posts on this page:** 3\
**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:** [November 20, 2022, 7:51am UTC](https://discourse.julialang.org/t/ask-whether-the-plot-step-function-variation-x-lfloor-x-rfloor-is-correct/90529/1 "2022-11-20T07:51:51Z")

</div>

Hi all I try to plot this function: x + \lfloor x \rfloor

I have this code:

```julia
using Plots,LaTeXStrings,Colors
gr()

f(x) = x + floor(x)
x = -2:1:4
y = f.(x)
plot(x[1:2], [y[1], y[1]], label=L"f(x) = x + \lfloor x \rfloor", legend=:topleft,legend_foreground_color=:white, legend_font_color=:blue3, color=:blue3, showaxis=false, tickfontcolor=:white)
# axis and 
hline!((0,0), color=:black, label="")
vline!((0,0), color=:black, label="")
for i in x
	if i!= 0
		annotate!(i,-0.2, (i, 7))
	end
end
for i in y
	if i != 0
		annotate!(-.2, i, (i, 7))
	end
end
annotate!(-.2, -.2, ("0", 7))
for i in 2:length(x)-1
	plot!(x[i:i+1], [y[i], y[i]], label="", color=:blue3, lw=2)
	scatter!(x[i], y[i], marker=:circle, markersize=8, label="")
end
scatter!(x[1:end-1], y[1:end-1], markersize=3, markercolor=:blue4, label="")
scatter!(x[2:end], y[1:end-1], markersize=3, markercolor=:white, label="")

```

resulting in this:  
 ![Capture d’écran_2022-11-20_14-47-15](https://global.discourse-cdn.com/julialang/original/3X/c/1/c1f1216fd30868087ca236122ce0e06949ca17b9.png)

I doubt the plot I made since the book shows the plot of so called function to be like this:  
 ![Capture d’écran_2022-11-20_14-50-58](https://global.discourse-cdn.com/julialang/original/3X/a/5/a5469bff95f20168d26706546051b9f6be81ff06.png)

Which one is correct ? If the latter, please tell me how to achieve that plot. Thanks a lot!

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [November 20, 2022, 11:36am UTC](https://discourse.julialang.org/t/ask-whether-the-plot-step-function-variation-x-lfloor-x-rfloor-is-correct/90529/2 "2022-11-20T11:36:52Z")

</div>

> [@Freya\_the\_Goddess](#):
>
> ```julia
> plot!(x[i:i+1], [y[i], y[i]], label="", color=:blue3, lw=2)
> 
> ```

Seems like there is a wrong index, it should be

```julia
	plot!(x[i:i+1], [y[i], y[i+1]], label="", color=:blue3, lw=2)

```

---

<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:** [November 20, 2022, 12:14pm UTC](https://discourse.julialang.org/t/ask-whether-the-plot-step-function-variation-x-lfloor-x-rfloor-is-correct/90529/3 "2022-11-20T12:14:42Z")

</div>

No the index is right, because it will make the step function that way.

Anyway the fault is on me. The function is actually x + |x|.

I already find the solution thanks!
