# How to plot step function \[x\]/x Correctly with limit

**URL:** https://discourse.julialang.org/t/how-to-plot-step-function-x-x-correctly-with-limit/86780
**Category:** General Usage
**Tags:** plotting
**Created:** [September 5, 2022, 1:41am UTC](https://discourse.julialang.org/t/how-to-plot-step-function-x-x-correctly-with-limit/86780 "2022-09-05T01:41:20Z")
**Posts on this page:** 7
**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 5, 2022, 1:41am UTC](https://discourse.julialang.org/t/how-to-plot-step-function-x-x-correctly-with-limit/86780/1 "2022-09-05T01:41:20Z")

</div>

Hi all,

I see a problem set and I want to plot this one:

\frac{[x]}{x}

`[x]` is a step function

I know the code to create step function (from my old thread), but how to create the above function?

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

f(x) = floor(x)
x = 0: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
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="")

```

---

<div class="post-metadata">

### Author: ![rmsmsgood](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rmsmsgood/32/20544_2.png) [@rmsmsgood](https://discourse.julialang.org/u/rmsmsgood)
#### Post date: [September 5, 2022, 7:10am UTC](https://discourse.julialang.org/t/how-to-plot-step-function-x-x-correctly-with-limit/86780/2 "2022-09-05T07:10:27Z")

</div>

Did you mean, this?

```julia
using Plots

saw_blade(x) = floor(x) / x
xspan = 0.01:0.01:10
plot(xspan, saw_blade.(xspan), legend = :none)

```

![1](https://global.discourse-cdn.com/julialang/original/3X/e/d/ed3f369d88388d50aa1e2b4961a84d704c1d25cb.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: [September 5, 2022, 3:14pm UTC](https://discourse.julialang.org/t/how-to-plot-step-function-x-x-correctly-with-limit/86780/3 "2022-09-05T15:14:33Z")

</div>

Is it really works with just `floor(x)`?

Anyway thanks a lot, I just want to admit the first time I saw it just like a shark fin…

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [September 5, 2022, 3:26pm UTC](https://discourse.julialang.org/t/how-to-plot-step-function-x-x-correctly-with-limit/86780/4 "2022-09-05T15:26:48Z")

</div>

If that is the function you mean, you are not looking for the word “step function”. The step function is `step(x, a=0) = x ≥ a`.

---

<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 6, 2022, 7:22am UTC](https://discourse.julialang.org/t/how-to-plot-step-function-x-x-correctly-with-limit/86780/5 "2022-09-06T07:22:32Z")

</div>

I try this:

```julia
using Plots, LaTeXStrings
pyplot(fmt=:svg)

step(x, a=0) = x ≥ a
xspan = 0.01:0.01:10
plot(xspan, step.(xspan), legend = :none)

```

I want to divide this `step(x, a=0) = x ≥ a` with `x` and then plot it. Not just plotting the step function.

---

<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 6, 2022, 7:35am UTC](https://discourse.julialang.org/t/how-to-plot-step-function-x-x-correctly-with-limit/86780/7 "2022-09-06T07:35:23Z")

</div>

This is the plot with expanded domain and codomain

```julia
using Plots, LaTeXStrings
pyplot(fmt=:svg)

saw_blade(x) = floor(x) / x
xspan = -1.0:0.1:5
plot(xspan, step.(xspan), legend = :none)

```

![Capture d’écran_2022-09-06_14-33-57](https://global.discourse-cdn.com/julialang/original/3X/c/f/cf4718feaaaa3d118ca09c474a78373604bf1f14.png)

I got it now… if anyone wants to explain it rigorously go ahead… I will love your post.

---

<div class="post-metadata">

### Author: ![rmsmsgood](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rmsmsgood/32/20544_2.png) [@rmsmsgood](https://discourse.julialang.org/u/rmsmsgood)
#### Post date: [September 6, 2022, 10:48am UTC](https://discourse.julialang.org/t/how-to-plot-step-function-x-x-correctly-with-limit/86780/8 "2022-09-06T10:48:02Z")

</div>

You didn’t use the `saw_blade` function, you used `step.` function in `plot()`.
