# Ylim shifts image in plot

**URL:** <https://discourse.julialang.org/t/ylim-shifts-image-in-plot/111512>\
**Category:** Visualization\
**Tags:** question, images, plotting, plots\
**Created:** [March 12, 2024, 11:20am UTC](https://discourse.julialang.org/t/ylim-shifts-image-in-plot/111512 "2024-03-12T11:20:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![parzt](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@parzt](https://discourse.julialang.org/u/parzt)\
**Post date:** [March 12, 2024, 11:20am UTC](https://discourse.julialang.org/t/ylim-shifts-image-in-plot/111512/1 "2024-03-12T11:20:34Z")

</div>

I’m trying to plot a section of an image file using Plots.jl and then draw in certain points in the image.

It works nicely when I plot the whole image:

```julia
using Plots
using FileIO, HTTP, ImageIO
bg_img2 = load(HTTP.URI("https://upload.wikimedia.org/wikipedia/commons/thumb/0/0c/GoldenGateBridge-001.jpg/640px-GoldenGateBridge-001.jpg"));

begin
	plot(bg_img2)
	scatter!([385], [315])
end

```

![whole_image](https://global.discourse-cdn.com/julialang/original/3X/e/1/e1158a71c770abfd27c649c56dd9926f80ff3f8b.png)

When introducing a xlim to the plot, everything still behaves as expected:

```julia
begin
	plot(bg_img2,
		xlim=(350, 420),
	)
	scatter!([385], [315])
end

```

![xlim](https://global.discourse-cdn.com/julialang/original/3X/d/e/de93d53c42007e8721381dfa980dfb642d701292.png)

However, as soon as I introduce a ylim to the plot, the image shifts. This get apparent because the plotted point is positioned differently relative to the image.

```julia
begin
	plot(bg_img2,
		ylim=(270, 350)
	)
	scatter!([385], [315])
end

```

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

What am I missing? Or is this a bug in the plotting library?

Pluto notebook to reproduce:  
[ylim\_image\_shift.jl](https://discourse.julialang.org/uploads/short-url/cS5OITyIMQCAtRvYu1gGnL4sSxR.jl) (43.5 KB)

---

<div class="post-metadata">

**Author:** ![parzt](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@parzt](https://discourse.julialang.org/u/parzt)\
**Post date:** [March 12, 2024, 1:24pm UTC](https://discourse.julialang.org/t/ylim-shifts-image-in-plot/111512/2 "2024-03-12T13:24:35Z")

</div>

I’ve done some more digging and this seems to be related to `yflip=true` (the default when plotting an image). Everything works as expected with `yflip=false`, apart from the flipped image of course.

I’ve opened a bug with Plots.jl: [[BUG] `ylims` does not respect `yflip=true` · Issue #4903 · JuliaPlots/Plots.jl · GitHub](https://github.com/JuliaPlots/Plots.jl/issues/4903)

---

<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:** [March 12, 2024, 1:40pm UTC](https://discourse.julialang.org/t/ylim-shifts-image-in-plot/111512/3 "2024-03-12T13:40:37Z")

</div>

I do not see the bug in Julia Version 1.10.0, Plots v1.40.1:

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

_PS:_  
_You can also plot single points as tuples_:  
`scatter!((385, 315))`

---

<div class="post-metadata">

**Author:** ![parzt](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@parzt](https://discourse.julialang.org/u/parzt)\
**Post date:** [March 12, 2024, 2:29pm UTC](https://discourse.julialang.org/t/ylim-shifts-image-in-plot/111512/4 "2024-03-12T14:29:40Z")

</div>

Interesting, I can consistently reproduce using Julia v1.10.2, Plots v1.40.2. What code exactly did you use to make it work on your setup?

---

<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:** [March 12, 2024, 3:03pm UTC](https://discourse.julialang.org/t/ylim-shifts-image-in-plot/111512/5 "2024-03-12T15:03:25Z")

</div>

> [@parzt](#):
>
> What code exactly did you use to make it work on your setup?

Exactly your code.

And tested now also with Plots v1.40.2, and can’t see the issue.

PS:  
I run the code from VS Code on Windows.

---

<div class="post-metadata">

**Author:** ![parzt](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@parzt](https://discourse.julialang.org/u/parzt)\
**Post date:** [March 12, 2024, 5:03pm UTC](https://discourse.julialang.org/t/ylim-shifts-image-in-plot/111512/6 "2024-03-12T17:03:06Z")

</div>

Mysterious. I’m able to reproduce the problem on different Linux machines, as well as on Windows, in Pluto and in VS Code.

I have created another Pluto notebook as a minimal working example:  
[mwe\_yflip\_ylim\_shift.jl](https://discourse.julialang.org/uploads/short-url/gTbZrRSXbjdRzfvq50VksjyttIU.jl) (43.2 KB)  
Is nobody else able to reproduce the issue?
