# How to plot correctly a discontinuous function?

**URL:** <https://discourse.julialang.org/t/how-to-plot-correctly-a-discontinuous-function/26931>\
**Category:** New to Julia\
**Tags:** plotting\
**Created:** [July 29, 2019, 9:21am UTC](https://discourse.julialang.org/t/how-to-plot-correctly-a-discontinuous-function/26931 "2019-07-29T09:21:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Masacroso](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/masacroso/32/9412_2.png) [@Masacroso](https://discourse.julialang.org/u/Masacroso)\
**Post date:** [July 29, 2019, 9:21am UTC](https://discourse.julialang.org/t/how-to-plot-correctly-a-discontinuous-function/26931/1 "2019-07-29T09:21:33Z")

</div>

If you want to plot a discontinuous functions, by example with this

```julia
using Plots
r = -5:.1:5
plot(r, floor.(r))

```

you get a graph with a continuous line, however the function have a discontinuity at any integer. How to prevent that the graph would be a continuous line, that it plot correctly jumps at each discontinuity point?

---

<div class="post-metadata">

**Author:** ![Karajan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karajan/32/8545_2.png) [@Karajan](https://discourse.julialang.org/u/Karajan)\
**Post date:** [July 29, 2019, 9:52am UTC](https://discourse.julialang.org/t/how-to-plot-correctly-a-discontinuous-function/26931/2 "2019-07-29T09:52:21Z")

</div>

`Plots` doesn’t know whether or not your function is continuous. _You_ know that, but `plot` just gets a bunch of points that could have come from anywhere. I think you have two options:

- plot the function piecewise yourself (relying on your knowledge where the jumps will be) or
- prevent the problem altogether and use `scatter`.

But maybe there exists some plot knowledge that I’m not aware of to make this happen more easily.

---

<div class="post-metadata">

**Author:** ![fruno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fruno/32/5013_2.png) [@fruno](https://discourse.julialang.org/u/fruno)\
**Post date:** [July 29, 2019, 10:52am UTC](https://discourse.julialang.org/t/how-to-plot-correctly-a-discontinuous-function/26931/3 "2019-07-29T10:52:16Z")

</div>

Injecting NaNs where you want the plot to be discontinuous appears to work, for example (in a very inelegant fashion):

```julia
using Plots
r = -5:.1:5
r1 = [r[1]]
for k = 2:length(r)
  if floor(r[k-1])!=floor(r[k])
    push!(r1,NaN)
  end
  push!(r1,r[k])
end
plot(r, floor.(r))
plot!(r1, floor.(r1))

```

You could also add “extrapolated points” to the subinterval before each discontinuity to “fill the full plot interval”, but that of course requires choosing how to extrapolate the continuous data before the discontinuity.
