# Plot step function \[x+1/2\]

**URL:** <https://discourse.julialang.org/t/plot-step-function-x-1-2/87114>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [September 12, 2022, 7:40am UTC](https://discourse.julialang.org/t/plot-step-function-x-1-2/87114 "2022-09-12T07:40:50Z")\
**Posts on this page:** 9\
**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 12, 2022, 7:40am UTC](https://discourse.julialang.org/t/plot-step-function-x-1-2/87114/1 "2022-09-12T07:40:50Z")

</div>

Hi all,

I have been trying to plot `[x+1/2]` learning from the codes at my old thread:

> [@How to Plot Step Functions \[x\] Correctly in Julia?](https://discourse.julialang.org/t/how-to-plot-step-functions-x-correctly-in-julia/84087):
>
> Hi all, I have this code: using Plots, LaTeXStrings gr() f(x)=floor(x) plot(f,0,10,ylim=(-12,12)) Since [x] denotes the gretest integer less than or equal to x, I want to plot it like this: [Capture d’écran\_2022-07-12\_14-04-59] Anyone know how?

But it returns not the plot that I want. I change `1` into `1.5` it still not working. Maybe it takes more than just that.

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [September 12, 2022, 8:02am UTC](https://discourse.julialang.org/t/plot-step-function-x-1-2/87114/2 "2022-09-12T08:02:59Z")

</div>

So you want the plot from your old post, but shifted 0.5 to the left? or am I interpreting that correctly?

You probably need to make sure you have the x-grid at correct locations to be able to draw that (i.e. at x.5 instead of x.0).

---

<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 12, 2022, 8:13am UTC](https://discourse.julialang.org/t/plot-step-function-x-1-2/87114/3 "2022-09-12T08:13:12Z")

</div>

yes shifted 0.5 to the left, that is correct, because basically it is still using the rules of step function.

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [September 12, 2022, 8:19am UTC](https://discourse.julialang.org/t/plot-step-function-x-1-2/87114/4 "2022-09-12T08:19:36Z")

</div>

Yeah, but have you changed your x values so they are actually hitting the half steps instead of the integers? So `x=-5.5:1:3.5` or something like that?

---

<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 12, 2022, 8:31am UTC](https://discourse.julialang.org/t/plot-step-function-x-1-2/87114/5 "2022-09-12T08:31:29Z")

</div>

I use this:

```julia
using Plots; gr()

f(x) = floor(x)
x = -5.5:1:4.5
y = f.(x)
plot(x[1:2], [y[1], y[1]], label=L"f(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

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

```

got error:  
**LoadError: Cannot convert Float64 to series data for plotting**  
**Stacktrace:**

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [September 12, 2022, 8:38am UTC](https://discourse.julialang.org/t/plot-step-function-x-1-2/87114/6 "2022-09-12T08:38:14Z")

</div>

Seems like it is unhappy about getting floats, so maybe the line with `scatter!` could be changed to something like `scatter!(x[i:i], y[i:i], ....)` which would make them one element arrays. Does this work?

---

<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 12, 2022, 8:43am UTC](https://discourse.julialang.org/t/plot-step-function-x-1-2/87114/7 "2022-09-12T08:43:11Z")

</div>

It does not show error but shows no plot as well.

---

<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 12, 2022, 8:50am UTC](https://discourse.julialang.org/t/plot-step-function-x-1-2/87114/8 "2022-09-12T08:50:51Z")

</div>

Yes `scatter` needs arrays, so the usual way to write this would be `scatter([x], [y])`.

For a plot to be shown it has to be the return value of the last evaluated expression. As loops in Julia return `nothing`, you need to either explicitly create a plot object and call it like this

```julia
p = plot()

for x ∈ 2:length(x)-1
    plot!(...)
    scatter!(...)
end
 
p

```

or add a call to `current()` to your code, which will just display the last plot object you’ve plotted to.

---

<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 21, 2022, 12:45pm UTC](https://discourse.julialang.org/t/plot-step-function-x-1-2/87114/9 "2022-09-21T12:45:23Z")

</div>

This works finally, after few days / week

```julia
using Plots; gr()

f(x) = floor(x + 1/2)
x = -5.5:1:4.5
y = f.(x)
plot(x[1:2], [y[1], y[1]], label=L"f(x) = \lfloor x + 1/2 \rfloor", legend=:bottomleft,legend_foreground_color=:white, legend_font_color=:blue, color=:blue, 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=:blue, lw=2)	
end
scatter!(x[1:end-1], y[1:end-1], markersize=3, markercolor=:blue, label="")
scatter!(x[2:end], y[1:end-1], markersize=3, markercolor=:white, label="")

```
