-
-
Notifications
You must be signed in to change notification settings - Fork 358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] image tick positions misplaced when specifying image axes #4158
Comments
I'll have a look in the next few days. In the mean time does |
Thanks @t-bltg! i = rand(RGB, 10, 10)
plot(i, xticks=1:10, yticks=1:10) Works! But plot(1:10, 1:10, i, xticks=1:10, yticks=1:10) does not. |
I've had a look. The function involved in the example is Line 1469 in 0f4a0c7
Since the explicit
--- a/Plots.jl/src/recipes.jl 2022-03-29 12:37:35.927406939 +0200
+++ b/Plots.jl/src/recipes.jl 2022-03-29 12:34:27.166126958 +0200
@@ -1450,18 +1450,31 @@
# 3 arguments
# --------------------------------------------------------------------
+function check_image_axes(x, y, z)
+ # fix for github.com/JuliaPlots/Plots.jl/issues/4158
+ if x isa AbstractRange && length(x) == size(z, 2)
+ half_step = step(x) / 2
+ x = range(first(x) - half_step, stop = last(x) + half_step)
+ end
+ if y isa AbstractRange && length(y) == size(z, 1)
+ half_step = step(y) / 2
+ y = range(first(y) - half_step, stop = last(y) + half_step)
+ end
+ x, y, z
+end
+
# images - grays
@recipe function f(x::AVec, y::AVec, mat::AMat{T}) where {T<:Gray}
if is_seriestype_supported(:image)
seriestype := :image
yflip --> true
- SliceIt, x, y, Surface(mat)
+ (SliceIt, check_image_axes(x, y, Surface(mat))...)
else
seriestype := :heatmap
yflip --> true
colorbar --> false
fillcolor --> cgrad([:black, :white])
- SliceIt, x, y, Surface(convert(Matrix{Float64}, mat))
+ (SliceIt, check_image_axes(x, y, Surface(convert(Matrix{Float64}, mat)))...)
end
end
@@ -1470,13 +1483,13 @@
if is_seriestype_supported(:image)
seriestype := :image
yflip --> true
- SliceIt, x, y, Surface(mat)
+ (SliceIt, check_image_axes(x, y, Surface(mat))...)
else
seriestype := :heatmap
yflip --> true
colorbar --> false
z, plotattributes[:fillcolor] = replace_image_with_heatmap(mat)
- SliceIt, x, y, Surface(z)
+ (SliceIt, check_image_axes(x, y, Surface(z))...)
end
end I'd suggest to be explicit about what you want to be drawn by using either: i = rand(RGB, 12, 10)
plot(i, xticks=axes(i, 2), yticks=axes(i, 1)) # should be preferred
plot(1:13, 1:11, i) # nodes (un-shifted, but confuses the ticking algorithm) [1]
plot(.5:12.5, .5:10.5, i) # cell centers (un-shifted, but confuses the ticking algorithm) [2] Note that the It's very easy to use a helper in the form of: plot_img(img) = plot(img, xticks=axes(img, 2), yticks=axes(img, 1)) |
Hi @t-bltg, here's a motivating example for why I believe this is a bug. The root issue is not the tick positions but the location the image is rendered on the plot. using Images
using Plots
i = rand(RGB, 4,4)
p = plot()
# Plot image
plot!(p, 4:2:10, 4:2:10, i)
# Plot other series
plot!(p, 3:10, 3:10) This behaviour is necessary to be able to offset the position of the image in addition to modifying the tick marks. As you can see, the tick location still drifts from the left of the pixels to the right of the pixels. |
1 year bump on this issue |
During my bug search in #4833 I identified the recipe for colored matrices as the cause for this issue. Since the gr backend supports the image seriestype and plotly does not, the recipe overrides the series type to image for the gr backend. Therefore, the adjustment step of the indices ( |
Bump on this! |
Details
See #4087 and #4088, and tagging @t-bltg.
With GR, the tick positions of images are not placed correctly when the axes of the image are passed to
plot
:Image plot without specifying axes:
Image plot with axes specified:
It's subtle, but to match the normal behaviour of images and other backends, the +/- 5s should be at the centres of their pixels. The +/-2.5s are also slightly off.
Thanks!
Backends
This bug occurs on ( insert
x
below )Versions
Plots.jl version:
[91a5bcdd] Plots v1.27.0
Backend version (
]st -m <backend(s)>
):[28b8d3ca] GR v0.64.0
Output of
versioninfo()
:The text was updated successfully, but these errors were encountered: