# Broken range

**URL:** <https://discourse.julialang.org/t/broken-range/95347>\
**Category:** Visualization\
**Tags:** makie\
**Created:** [February 28, 2023, 5:53pm UTC](https://discourse.julialang.org/t/broken-range/95347 "2023-02-28T17:53:32Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [February 28, 2023, 5:53pm UTC](https://discourse.julialang.org/t/broken-range/95347/1 "2023-02-28T17:53:32Z")

</div>

Hello, i try plot next graphic for range  
The problem is that the picture looks too discrete. I built it with a modified range and it looked smoother. Why is that?

```julia
I0range = range( -1.509460, -1.509490, length = 5000 )
f = Figure(resolution = (1200, 400))
axis = Axis(f[1, 1], xlabel = L"I_0", ylabel = L"\Lambda", xlabelsize = 35, ylabelsize = 35,
            xticklabelsize = 25,yticklabelsize = 25 )
lines!(axis, I0range, Λs[:, 1], color = :red, linewidth = 1.0)
lines!(axis, I0range, Λs[:, 2], color = :green, linewidth = 1.0)
f

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/1/8198d2a494833a6eb684ebb8b2694e21a12c1d16.png)

```julia
I0range = range( -1.509400, -1.509490, length = 5000 )

f = Figure(resolution = (1200, 400))
axis = Axis(f[1, 1], xlabel = L"I_0", ylabel = L"\Lambda", xlabelsize = 35, ylabelsize = 35,
            xticklabelsize = 25,yticklabelsize = 25 )
lines!(axis, I0range, Λs[:, 1], color = :red, linewidth = 1.0)
lines!(axis, I0range, Λs[:, 2], color = :green, linewidth = 1.0)

f

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/d/2de4018a6fa1cf8e65a1fe97c07b3174248120fa.png)

---

<div class="post-metadata">

**Author:** ![brianguenter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brianguenter/32/29519_2.png) [@brianguenter](https://discourse.julialang.org/u/brianguenter)\
**Post date:** [February 28, 2023, 6:29pm UTC](https://discourse.julialang.org/t/broken-range/95347/2 "2023-02-28T18:29:05Z")

</div>

Your code doesn’t execute as is, it errors:

```julia
julia> lines!(axis, I0range, Λs[:, 1], color = :red, linewidth = 1.0)
ERROR: UndefVarError: Λs not defined

```

Do you have a complete minimum working example that executes? More people are likely to respond to your question if you do.

---

<div class="post-metadata">

**Author:** ![ffreyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffreyer/32/21569_2.png) [@ffreyer](https://discourse.julialang.org/u/ffreyer)\
**Post date:** [February 28, 2023, 10:49pm UTC](https://discourse.julialang.org/t/broken-range/95347/3 "2023-02-28T22:49:03Z")

</div>

> [@Sergey\_Novak](#):
>
> The problem is that the picture looks too discrete. I built it with a modified range and it looked smoother. Why is that?

That’s because Makie uses Float32 internally for gpu compatability.

---

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [March 1, 2023, 6:14am UTC](https://discourse.julialang.org/t/broken-range/95347/4 "2023-03-01T06:14:44Z")

</div>

I load data from file, i can share it.

> **[LSE\_right\_space\_5000\_points.jld](https://drive.google.com/file/d/1nxizuSLQkfUETr3kvrIGzSn0LowkmCoC/view?usp=sharing)**
>
> Google Drive file.

for load file need use next code

```julia
using JLD
Λs = load("your path to file//LSE_right_space_5000_points.jld")["data"]

```

---

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [March 1, 2023, 6:15am UTC](https://discourse.julialang.org/t/broken-range/95347/5 "2023-03-01T06:15:13Z")

</div>

How can i fix this?  
EDIT: I’m reading about that problem and as I understand it there is no way to fix it. Because of this problem, I recalculated the data several times, thinking that I had an error. Apparently CairoMakie is not a very good visualization package at least for articles.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [March 1, 2023, 8:18am UTC](https://discourse.julialang.org/t/broken-range/95347/6 "2023-03-01T08:18:07Z")

</div>

There’s this PR that could fix the problem at least for CairoMakie [Fix float issues for CairoMakie by using Float64 by ffreyer · Pull Request #2573 · MakieOrg/Makie.jl · GitHub](https://github.com/MakieOrg/Makie.jl/pull/2573)

> Apparently CairoMakie is not a very good visualization package at least for articles.

I wouldn’t say that. The issue here is that it’s not obvious for end users that there’s this internal precision issue at play, which is due to the special requirements of the GPU backends. But other packages would have the same problems with Float64, too, if you selected sufficiently small values that are close together but relatively far away from 0. It’s even true with any algorithm you can run using floating points, they will pretty much all run into numerical issues if you push your input values too far. Here’s a really simple example with Float64:

```julia
julia> for x in [1e16, 1e17, 1e18]
           println(x + 10 - x)
       end
10.0
16.0
0.0

```

I see the problem here more as that there’s no easy way to tell the user “you’re running into visible quantization problems” because it depends not only on the input data but also on the projection, so we can’t do it right at the input step. And there’s always “some” loss but it might not matter visually in the end.

So while I don’t like that we have this issue at all, I don’t think it prohibits you from making visualizations fit for articles, it just means that in some cases you have to be more careful not to use data that’s too tightly packed for representation in Float32.

---

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [March 1, 2023, 8:40am UTC](https://discourse.julialang.org/t/broken-range/95347/7 "2023-03-01T08:40:25Z")

</div>

Thank you, I’ll take a look PR. I plot image with matplotlib and have not encountered such a problem. Also, I had previously built pictures with a small step and everything was fine, for example for range

```julia
range( -1.51075, -1.51090, length = 5000 )

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/a/3a9d27f838fcc8354ac14ea76932dc207290b6ed.png)  
And it’s kind of weird

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 1, 2023, 8:53am UTC](https://discourse.julialang.org/t/broken-range/95347/8 "2023-03-01T08:53:03Z")

</div>

One workaround could be let your plot range just be `1:5000`, and then fix the x-tick labels afterwards.

Note that even your modified range: `range( -1.509400, -1.509490, length = 5000 )` is undersampled:

```julia
julia> range( -1.509400, -1.509490, 5000 ) .|> Float32 |> unique |> length
756

```

though it might not be visible, due to screen resolution.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [March 1, 2023, 8:55am UTC](https://discourse.julialang.org/t/broken-range/95347/9 "2023-03-01T08:55:01Z")

</div>

It’s about how many Float32s you have available between your axis limits stretched over the axis canvas:

```julia
julia> function count_floats(a, b)
           count = 0
           if a > b
               a, b = b, a
           end
           while a < b
               a = nextfloat(a)
               count += 1
           end
           return count
       end
count_floats (generic function with 2 methods)

julia> count_floats(Float32(-1.51075), Float32(-1.51090))
1258

```

So with those values you’d have 1258 quantization steps which should mostly be enough.  
Earlier you used different values:

```julia
julia> count_floats(Float32(-1.509460), Float32(-1.509490))
252

```

Here you got visible quantization.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [March 1, 2023, 8:55am UTC](https://discourse.julialang.org/t/broken-range/95347/10 "2023-03-01T08:55:44Z")

</div>

@DNF made the same point a bit faster 🙂

---

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [March 1, 2023, 9:23am UTC](https://discourse.julialang.org/t/broken-range/95347/11 "2023-03-01T09:23:20Z")

</div>

Thank you. How can i fix xlabel ticks? I tried to do this and it didn’t work out

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 1, 2023, 9:25am UTC](https://discourse.julialang.org/t/broken-range/95347/12 "2023-03-01T09:25:59Z")

</div>

Another thing, a bit off-topic, but is there something off with your sampling range?

```julia
julia> range( -1.509400, -1.509490, 5000 )
-1.5094:-1.800360072014403e-8:-1.50949

```

You see the step is a bit odd. If you use 5001 points you get a better steplength:

```julia
julia> range( -1.509400, -1.509490, 5001 )
-1.5094:-1.8e-8:-1.50949

```

Or, if you are locked at 5000 points, maybe your endpoints are a bit off?

```julia
julia> range( -1.509400, step=-1.8e-8, length=5000)
-1.5094:-1.8e-8:-1.509489982

```

This depends on how your data is collected, of course, but I often find that it’s the sampling rate (or the inverse sampling rate) that is a relatively ‘nice’ number.

---

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [March 1, 2023, 10:13am UTC](https://discourse.julialang.org/t/broken-range/95347/13 "2023-03-01T10:13:41Z")

</div>

I can change length of range, thank you  
Are you can share code how can i plot image without `range( -1.509400, -1.509490, 5000 )` with using `xlims`? I have already tried to do this, but all the xtick stuck together on the left

EDIT: It is also interesting how to do the same for the following code

```julia
f = Figure(resolution = (1200, 400))
axis = Axis(f[1,1]; xlabel = L"I_0", ylabel = L"E", xlabelsize = 30, ylabelsize = 30,
            xticklabelsize = 25, yticklabelsize = 25)
for (j, p) in enumerate(I0range)
    scatter!(axis, fill(p, length(output[j])), output[j]; color = ("deeppink", 0.5), markersize = 1.0)
end
f

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 1, 2023, 11:33am UTC](https://discourse.julialang.org/t/broken-range/95347/14 "2023-03-01T11:33:38Z")

</div>

You can try something like this (using dummy data here):

```julia
r1 = range(-1.509400, -1.509490, 5001) # original range, but with 5001 samples
y = (sin.(r1.*400000) .+ cos.(r1.*100000).^2) # dummy data

# r1_ = 1:5001 # this range should have no discretization issues for Float32
r1_ = 5001:-1:1 # oops should reverse this.
tickvals = r1_[1:1000:end] # manually selecting tick locations
ticklabels = string.(r1[1:1000:end]) # select corresponding tick locations in original range
lines(r1_, y; axis=(xticks=(tickvals, ticklabels),)) # use clever makie trick ;)

```

 ![plot_46](https://global.discourse-cdn.com/julialang/original/3X/f/3/f37d4d110a85a9a905a40deccc53758b89422758.png)

Regular plotting:

```julia
lines(r1, y)

```

 ![plot_43](https://global.discourse-cdn.com/julialang/original/3X/9/1/91920f83710902c538862367c6659abdf11b31c9.png)

**Edit:** Oops! Had my first plot reversed, had to fix. Play around with how to get the best tick values.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [March 1, 2023, 2:28pm UTC](https://discourse.julialang.org/t/broken-range/95347/15 "2023-03-01T14:28:40Z")

</div>

Honestly,I don’t know what GMT is doing under the hood but it doesn’t show that quantization effect.

```julia
using GMT

I0range = range( -1.509400, -1.509490, length = 5000 );
lines(I0range,Λs[:, 1], lc=:red, region=(-1.509492,-1.509398,-3,0.5), figsize=(14,4))
lines!(I0range,Λs[:, 2], lc=:green, xlabel="I@-0@-", ylabel="@~L@~", show=1)

```

 ![GMTjl_tmp](https://global.discourse-cdn.com/julialang/original/3X/9/2/92dea742a0ae04597b9bd1f4799241bb63a9c88f.png)

---

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [March 1, 2023, 4:50pm UTC](https://discourse.julialang.org/t/broken-range/95347/16 "2023-03-01T16:50:19Z")

</div>

Thank you!

---

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [March 1, 2023, 4:51pm UTC](https://discourse.julialang.org/t/broken-range/95347/17 "2023-03-01T16:51:51Z")

</div>

I have not seen such a package before, thank you

---

<div class="post-metadata">

**Author:** ![ederag](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ederag/32/4106_2.png) [@ederag](https://discourse.julialang.org/u/ederag)\
**Post date:** [March 1, 2023, 9:21pm UTC](https://discourse.julialang.org/t/broken-range/95347/18 "2023-03-01T21:21:19Z")

</div>

Beware that using integers work here because the data was evenly spaced.  
Otherwise subtracting the offset (in double precision) should work ?

As an inspiration, here is how `matplotlib` _displays_ things when the offset is much larger than extent:  
 ![index](https://global.discourse-cdn.com/julialang/original/3X/8/1/8169ef919c2c629bcb8477652f36a9328c7ed5a4.png)

> **Code**
>
> ```python
> >>> import matplotlib as mpl
> >>> import numpy as np
> >>> import matplotlib.pyplot as plt
> >>> offset = -1.5094
> >>> extent = -9e-5
> >>> x = np.linspace(offset, offset + extent, 5000)
> >>> y = offset + extent * (np.sin(x * 400000) + np.cos(x * 100000)**2)
> >>> fig, ax = plt.subplots()
> >>> ax.plot(x, y)
> >>> plt.show(fig)
> 
> ```

Not sure this is pretty and appropriate for a publication,  
but I once found that quite practical for results exploration,  
because that separates cleanly offset from extent.
