# Weird bug when plotting step function

**URL:** <https://discourse.julialang.org/t/weird-bug-when-plotting-step-function/9172>\
**Category:** Visualization\
**Created:** [February 19, 2018, 4:34pm UTC](https://discourse.julialang.org/t/weird-bug-when-plotting-step-function/9172 "2018-02-19T16:34:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![j2b2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j2b2/32/3387_2.png) [@j2b2](https://discourse.julialang.org/u/j2b2)\
**Post date:** [February 19, 2018, 4:34pm UTC](https://discourse.julialang.org/t/weird-bug-when-plotting-step-function/9172/1 "2018-02-19T16:34:19Z")

</div>

First I define two step functions:

```julia
julia> f(x)=floor(5x)-floor(2x)-floor(3x)
f (generic function with 1 method)
julia> g(x)=f(2x)-f(x)
g (generic function with 1 method)

```

and then I plot `g`:

```julia
julia> using Plots
julia> gr()
Plots.GRBackend()
julia> plot(g,0,1,ylim=(-1.2,1.2))

```

![plot1](https://global.discourse-cdn.com/julialang/original/3X/8/6/869df4a79a6b5981e21138a507075930b30c2239.png)

So far, so good. Because `g` is periodic with period 1, I try:

```julia
julia> plot(g,0,2,ylim=(-1.2,1.2))

```

![plot2](https://global.discourse-cdn.com/julialang/original/3X/e/b/eb0f14e21d0c3ed25b6e1300f9b3a62957d17683.png)  
The plotted graph is that of _the zero function for x between 0 and 1_, and is correct for x between 1 and 2 !!! To get the right graph, I must use something like

```julia
julia> plot(g,0:0.001:2,ylim=(-1.2,1.2))

```

![plot3](https://global.discourse-cdn.com/julialang/original/3X/a/0/a0519718bb3ce1c920df9f1afb9d090bf0cdcd8d.png)

Puzzling!

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [February 19, 2018, 4:46pm UTC](https://discourse.julialang.org/t/weird-bug-when-plotting-step-function/9172/2 "2018-02-19T16:46:45Z")

</div>

I don’t know how Plots chooses the sample points when you just give it endpoints, but all that’s happening is that its samples happen to miss all of the non-zero values in your function in [0, 1]. Try `scatter()` instead of `plot()` to see this:

 ![scatter](https://global.discourse-cdn.com/julialang/original/3X/8/c/8c24369a963686dd96c733e02817d01bb5297e4c.png)

Clearly the sample choice happened to miss the regions of interest for your particular function (inevitably there will be some clever function like yours for which their sample selection algorithm fails). Your solution of manually specifying the sample points seems like a good one.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [February 19, 2018, 4:53pm UTC](https://discourse.julialang.org/t/weird-bug-when-plotting-step-function/9172/3 "2018-02-19T16:53:41Z")

</div>

I wrote the algorithm for choosing sample points when given a function (without really knowing what I was doing, so I’m sure improvements are possible). However, I think there will always be some tricky functions where you can fool the algorithm that the function is constant on an interval. Explicitly specifying the points in those cases seems reasonable.

---

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [February 19, 2018, 4:57pm UTC](https://discourse.julialang.org/t/weird-bug-when-plotting-step-function/9172/4 "2018-02-19T16:57:05Z")

</div>

It’d be interesting to try this example in Mathematica, as Plots uses a very similar [algorithm](https://github.com/JuliaPlots/PlotUtils.jl/blob/master/src/adapted_grid.jl) to select the x values on which to plot. The algorithm, from my understanding, goes more or less as follows:

- Start with a few points
- For each two consecutive points, estimate whether the function between those two points is approximately linear: if it isn’t, add points in the middle
- Repeat

I can imagine that the algorithm started by sampling only points with value `0`, got convinced that the linear approximation (constant equal to `0`) was good and stopped there. Definitely worth opening an issue on the Plots repository (or PlotUtils, where the algorithm is implemented).

EDIT: I’ve just seen @kristoffer.carlsson 's reply and agree in principle but it is still good to see if this case can be fixed easily (maybe just starting the algorithm with more points)

---

<div class="post-metadata">

**Author:** ![j2b2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j2b2/32/3387_2.png) [@j2b2](https://discourse.julialang.org/u/j2b2)\
**Post date:** [February 19, 2018, 5:02pm UTC](https://discourse.julialang.org/t/weird-bug-when-plotting-step-function/9172/5 "2018-02-19T17:02:33Z")

</div>

What puzzles me about the sampling algorithm is that it fails for x between 0 and 1, and succeeds for x between 1 and 2, for a periodic function with period 1. The scatter plot of `rdeits` looks very significant.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [February 19, 2018, 5:12pm UTC](https://discourse.julialang.org/t/weird-bug-when-plotting-step-function/9172/6 "2018-02-19T17:12:37Z")

</div>

I think you are just a bit unlucky with where the initial sample points happen to go. The algorithm should not be sensitive to the specific `x` values. It does a small amount of “wiggling” to try to avoid aliasing but if two neighboring initial samples have the same function value, then it will assume the function is constant there and not refine that interval. The only remedy would be to increase the number of sample points.

For example, if I slightly shift the interval:

![12](https://global.discourse-cdn.com/julialang/original/3X/2/e/2ed3eb66598c455453a2c05d8dbf893cb85197ce.png)

By the way, giving a step function with narrow steps to an adaptive plotting algorithm is probably the most evil input 😛

---

<div class="post-metadata">

**Author:** ![j2b2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j2b2/32/3387_2.png) [@j2b2](https://discourse.julialang.org/u/j2b2)\
**Post date:** [February 19, 2018, 5:30pm UTC](https://discourse.julialang.org/t/weird-bug-when-plotting-step-function/9172/7 "2018-02-19T17:30:24Z")

</div>

> [@kristoffer.carlsson](#):
>
> By the way, giving a step function with narrow steps to an adaptive plotting algorithm is probably the most evil input 😛

Sorry, it wasn’t on purpose 🙂

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [February 19, 2018, 5:40pm UTC](https://discourse.julialang.org/t/weird-bug-when-plotting-step-function/9172/8 "2018-02-19T17:40:09Z")

</div>

If anyone is interested in improving the algorithm, taking into account explicit y-limits would be a nice improvement. Refining regions where the function values are outside the y-limits is useless.
