ziotom78 / Healpix.jl

Healpix library written in Julia
GNU General Public License v2.0
51 stars 18 forks source link

Annotating with Plots.jl or Makie.jl - getting the text coordinates right #111

Open tamasgal opened 4 months ago

tamasgal commented 4 months ago

I am having trouble to find out how to get the pixel coordinates for a given angle.

julia> using Plots
[ Info: Precompiling Plots [91a5bcdd-55d7-5caf-9e0b-520d859cae80]

julia> m = HealpixMap{Float64, RingOrder}(32);

julia> m[ang2pixRing(m.resolution, π/4, 0.0)] = 250
250

julia> m[ang2pixRing(m.resolution, 0.42, 0.23)] = 300
300

julia> plot(m)

julia> annotate!(π/4, 0.0, "some text")
Screenshot 2024-03-12 at 15 15 52

Here, obviously annotate! is not printed in the coordinate system of the plot. I found ang2pix but that only gives the pixel number. How can I find the "plot coordinate" for a given angle or vector?

ziotom78 commented 4 months ago

Hi @tamasgal , unfortunately Healpix.jl is not as sophisticated as Healpy when producing plots. Basically, when you plot a map there is a small raytracer (see the project function) that trace rays and find where they hit the unitary sphere according to the inverse projection function (which converts a 2D point x,y into a 3D direction θ,φ).

Thus, the output plot produced by Plots.jl has no knowledge of the coordinate transformation that was done internally by Healpix.jl. Thus, your annotate! call interprets the (x,y) point in the 2D space of the window and not in the 3D space of the surface of the sphere 🙁.

Implementing proper coordinate conversions would be a very welcomed PR. However, I do not know the amount of support for coordinate transformations provided by Plots.jl. Perhaps it would be better to provide a plotting backend for Healpix.jl based on Makie.jl: judging from some demos I've seen, it might be that it is perfectly capable of doing this.

tamasgal commented 4 months ago

Yes that's what I also understood.

Well I used Plots.jl because there was a recipe but I would definitely prefer Makie.jl ;)

It's actually not that complicated to display a similar plot in Makie.jl:

img, mask, anymasked = mollweide(m, Dict())  # m is a HealpixMap like above

fig = Figure(size = (1800, 900))
ax = CairoMakie.Axis(fig[1, 1], aspect = 2)
hidedecorations!(ax)
heatmap!(ax, img', show_axis = false)
hidespines!(ax)
fig

gives

Screenshot 2024-03-12 at 20 01 47

So now back to the original problem of transforming the coordinates but in Makie.jl ;) I'll come back if I have some news.