# How to Plot Step Functions \[x\] Correctly in Julia?

**URL:** <https://discourse.julialang.org/t/how-to-plot-step-functions-x-correctly-in-julia/84087>\
**Category:** General Usage\
**Tags:** plots\
**Created:** [July 12, 2022, 7:05am UTC](https://discourse.julialang.org/t/how-to-plot-step-functions-x-correctly-in-julia/84087 "2022-07-12T07:05:50Z")\
**Posts on this page:** 16\
**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:** [July 12, 2022, 7:05am UTC](https://discourse.julialang.org/t/how-to-plot-step-functions-x-correctly-in-julia/84087/1 "2022-07-12T07:05:50Z")

</div>

Hi all,

I have this code:

```julia
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](https://global.discourse-cdn.com/julialang/original/3X/7/c/7c0bc0e83ac04e75a75339323d4e9ecb19d70ab9.png)

Anyone know how?

---

<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 12, 2022, 8:33am UTC](https://discourse.julialang.org/t/how-to-plot-step-functions-x-correctly-in-julia/84087/2 "2022-07-12T08:33:30Z")

</div>

One way:

```julia
using Plots; gr()

xp = [[i, i+1 - 5*eps()] for i in -12:11]
p = plot(ylim=(-12,12), framestyle = :origin)
for x in xp
    plot!(x, floor.(x), c=:black)
    scatter!(x, floor.(x), ms=3, msc=:black, mc=[:black, :white])
end
p

```

_ **NB:** it would be faster not to loop and plot everything in one go. It would require inserting NaNs at the breaks._

---

<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:** [July 12, 2022, 9:49am UTC](https://discourse.julialang.org/t/how-to-plot-step-functions-x-correctly-in-julia/84087/3 "2022-07-12T09:49:34Z")

</div>

Creating step functions is not as simple as just using floor().

---

<div class="post-metadata">

**Author:** ![runjaj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/runjaj/32/23628_2.png) [@runjaj](https://discourse.julialang.org/u/runjaj)\
**Post date:** [July 12, 2022, 10:08am UTC](https://discourse.julialang.org/t/how-to-plot-step-functions-x-correctly-in-julia/84087/4 "2022-07-12T10:08:08Z")

</div>

I tried to reproduce the original plot (not the best code, but it has been fun):

```julia
f(x) = floor(x)
x = -5:1:4
y = f.(x)
plot(x[1:2], [y[1], y[1]], label=L"f(x) = \lfloor x \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)
	scatter!(x[i], y[i], marker=:circle, markersize=8, label="")
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="")

```

 ![steps](https://global.discourse-cdn.com/julialang/original/3X/e/0/e04365540f35ed38942f02b6490cefe55d69e3c1.png)

---

<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 12, 2022, 10:10am UTC](https://discourse.julialang.org/t/how-to-plot-step-functions-x-correctly-in-julia/84087/5 "2022-07-12T10:10:13Z")

</div>

Here is a version without the plot loop using `NaN` at the breaks:

```julia
using Plots; gr()

x = -12:12
xp = [[i, i+1 - 5*eps(), NaN] for i in x[1:end-1]]
xx = collect(Iterators.flatten(xp))
yy = floor.(xx)
plot(xx, yy, c=:black, label=false, framestyle = :origin)
scatter!(xx', yy', msc=:black, mc=[:black :white :white], ms=3)

```

---

<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:** [July 12, 2022, 12:12pm UTC](https://discourse.julialang.org/t/how-to-plot-step-functions-x-correctly-in-julia/84087/6 "2022-07-12T12:12:58Z")

</div>

> [@runjaj](#):
>
> ```julia
> f(x) = floor(x)
> x = -5:1:4
> y = f.(x)
> plot(x[1:2], [y[1], y[1]], label=L"f(x) = \lfloor x \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)
> scatter!(x[i], y[i], marker=:circle, markersize=8, label="")
> 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="")
> 
> ```

It is really beautiful,

I want to ask

```julia
plot(x[1:2], [y[1], y[1]]

```

why there are two y[1] ? I want to know the details behind it.

---

<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 12, 2022, 12:41pm UTC](https://discourse.julialang.org/t/how-to-plot-step-functions-x-correctly-in-julia/84087/7 "2022-07-12T12:41:54Z")

</div>

Note that if you want the axes centered at the origin, you should use plot argument: `framestyle = :origin`. _NB: updated in examples above_

---

<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:** [July 12, 2022, 12:49pm UTC](https://discourse.julialang.org/t/how-to-plot-step-functions-x-correctly-in-julia/84087/8 "2022-07-12T12:49:16Z")

</div>

Thanks for the explanation.

---

<div class="post-metadata">

**Author:** ![runjaj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/runjaj/32/23628_2.png) [@runjaj](https://discourse.julialang.org/u/runjaj)\
**Post date:** [July 12, 2022, 2:50pm UTC](https://discourse.julialang.org/t/how-to-plot-step-functions-x-correctly-in-julia/84087/9 "2022-07-12T14:50:25Z")

</div>

> [@Freya\_the\_Goddess](#):
>
> I want to ask
> 
> ```julia
> plot(x[1:2], [y[1], y[1]]
> 
> ```
> 
> why there are two y[1] ? I want to know the details behind it.

Because the first step goes from point `(x[1], y[1])` to `(x[2], y[1])`. It’s the same for the rest of the steps inside the for loop.

I have cleaned my version a little bit (thanks to @rafael.guerra):

```julia
# Plot configuration
plot(legend_title=L"f(x) = \lfloor x \rfloor", legend=:bottomleft,legend_foreground_color=:white, legend_title_font_color=:blue, color=:blue, framestyle=:origin, ticks=x)
# Steps drawing
for i in 1:length(x)-1
	plot!(x[i:i+1], [y[i], y[i]], label="", color=:blue, lw=2)
end
# Points at the beginning ot the steps
scatter!(x[1:end-1], y[1:end-1], markersize=3, markercolor=:blue, label="")
# End points
scatter!(x[2:end], y[1:end-1], markersize=3, markercolor=:white, label="")
# Adds '0' close to origin
annotate!(-.22, -.26, ("0", 8))

```

I’m sure it could be improved, but I think it’s quite close to the original.

 ![steps](https://global.discourse-cdn.com/julialang/original/3X/a/b/abf101cf59d9499d34e2ea46e815cbe6f78da085.png)

---

<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:** [August 2, 2022, 2:04pm UTC](https://discourse.julialang.org/t/how-to-plot-step-functions-x-correctly-in-julia/84087/10 "2022-08-02T14:04:55Z")

</div>

Tried this in Jupyter Notebook and compare with your old code.

It is simpler, so we can remove the old

```julia
f(x) = floor(x)
x = -5:1:4
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

```

---

<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:33am UTC](https://discourse.julialang.org/t/how-to-plot-step-functions-x-correctly-in-julia/84087/11 "2022-09-12T08:33:04Z")

</div>

> [@rafael.guerra](#):
>
> `using Plots; gr()`

Hi @runjaj

I am trying to plot `[x + 1/2]` do you know what to edit in your code? Thanks

---

<div class="post-metadata">

**Author:** ![runjaj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/runjaj/32/23628_2.png) [@runjaj](https://discourse.julialang.org/u/runjaj)\
**Post date:** [September 12, 2022, 2:42pm UTC](https://discourse.julialang.org/t/how-to-plot-step-functions-x-correctly-in-julia/84087/12 "2022-09-12T14:42:40Z")

</div>

> [@Freya\_the\_Goddess](#):
>
> `, LaTeXStrings`

Hi!

One simple option is just changing the function to:

```julia
f(x) = floor(x + 1/2)

```

And the step of the `x` to `0.5`:

```julia
x = -5:.5:4

```

In this case, you’ll get:

![floor x+0.5](https://global.discourse-cdn.com/julialang/original/3X/7/6/76b56c95c77a53663a7802053803f2b85c5d0f3e.png)

Is this what you want?

If you want to avoid plotting the central point of each step, the easiest way is set `x` to:

```julia
x = -5.5:1:4.5

```

![floor x+0.5 v2](https://global.discourse-cdn.com/julialang/original/3X/f/4/f4d703cfb48fd8579646823634bae464dffe4b4d.png)

This is the code I used:

```julia
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
hline!((0,0), color=:black, label="")
vline!((0,0), color=:black, label="")

# Scale values
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))

# Plot function
for i in 2:length(x)-1
	plot!(x[i:i+1], [y[i], y[i]], label="", color=:blue, lw=2)
end

```

Javier

---

<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 13, 2022, 12:03pm UTC](https://discourse.julialang.org/t/how-to-plot-step-functions-x-correctly-in-julia/84087/13 "2022-09-13T12:03:45Z")

</div>

I actually tried this

```julia
using Plots, LaTeXStrings, Plots.PlotMeasures
gr()

f(x) = floor(x + 1/2)
x = -5:.5:4
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

```

and it returns error:

**LoadError: Cannot convert Float64 to series data for plotting**

seems the scatter function is problematic for using float or decimal value.

---

<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 13, 2022, 12:04pm UTC](https://discourse.julialang.org/t/how-to-plot-step-functions-x-correctly-in-julia/84087/14 "2022-09-13T12:04:26Z")

</div>

Then I try your code:

```julia
using Plots, LaTeXStrings, Plots.PlotMeasures
pyplot()

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
hline!((0,0), color=:black, label="")
vline!((0,0), color=:black, label="")

# Scale values
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))

# Plot function
for i in 2:length(x)-1
	plot!(x[i:i+1], [y[i], y[i]], label="", color=:blue, lw=2)
end

```

the GUI window is not showing up, weird, but the REPL shows it is done…

---

<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 13, 2022, 12:39pm UTC](https://discourse.julialang.org/t/how-to-plot-step-functions-x-correctly-in-julia/84087/15 "2022-09-13T12:39:01Z")

</div>

`scatter` expects a vector of numbers, you need `scatter!([x[i]], [y[i]])` although at that point you might as well just pull it out of the loop and write `scatter!(x[2:length(x)-1], y[2:length(x)-1])` (which will also make the plot window show up as per my response in the other thread)

---

<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:39pm UTC](https://discourse.julialang.org/t/how-to-plot-step-functions-x-correctly-in-julia/84087/16 "2022-09-21T12:39:29Z")

</div>

I just read your reply more carefully and after lots of trial it works with this code:

```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="")

```
