MakieOrg / Makie.jl

Interactive data visualizations and plotting in Julia
https://docs.makie.org/stable
MIT License
2.41k stars 310 forks source link

Plot methods that don't work with units #3947

Open eliascarv opened 4 months ago

eliascarv commented 4 months ago

I made a list of some plot methods that I use that don't work with units. MWE:

using GLMakie
using Unitful
using StaticArrays
using GLMakie.GeometryBasics

#---------
# SCATTER
#---------

# 2D
x = rand(10) * u"m"
y = rand(10) * u"m"
# positions
tuples = collect(zip(x, y))
svectors = SVector.(x, y)
points = Point.(x, y)

scatter(x, y) # works
scatter(tuples) # doesn't work
scatter(svectors) # doesn't work
scatter(points) # doesn't work

# 3D
x = rand(10) * u"m"
y = rand(10) * u"m"
z = rand(10) * u"m"
# positions
tuples = collect(zip(x, y, z))
svectors = SVector.(x, y, z)
points = Point.(x, y, z)

scatter(x, y, z) # works, but doesn't show units on the axis
scatter(tuples) # doesn't work
scatter(svectors) # doesn't work
scatter(points) # doesn't work

#--------
# ARROWS
#--------

# 2D
x = rand(10) * u"m"
y = rand(10) * u"m"
u = rand(10) * u"m"
v = rand(10) * u"m"

points = Point.(x, y)
directions = Vec.(u, v)

arrows(x, y, u, v) # doesn't work
arrows(points, directions) # doesn't work

# 3D
x = rand(10) * u"m"
y = rand(10) * u"m"
z = rand(10) * u"m"
u = rand(10) * u"m"
v = rand(10) * u"m"
w = rand(10) * u"m"

points = Point.(x, y, z)
directions = Vec.(u, v, w)

arrows(x, y, z, u, v, w) # doesn't work
arrows(points, directions) # doesn't work

#------
# POLY
#------

x = [0.0, 2.0, 3.0, 1.0] * u"m"
y = [0.0, 0.0, 1.0, 1.0] * u"m"
points = Point.(x, y)
polygon = Polygon(points) # doesn't work

poly(points) # doesn't work

#-------
# LINES
#-------

# 2D
x = (0:0.01:10) * u"m"
y = 5u"m" .* sin.(0:0.01:10)
# positions
tuples = collect(zip(x, y))
svectors = SVector.(x, y)
points = Point.(x, y)

lines(x, y) # works
lines(tuples) # doesn't work
lines(svectors) # doesn't work
lines(points) # doesn't work

# 3D
x = (0:0.01:10) * u"m"
y = 5u"m" .* sin.(0:0.01:10)
z = 5u"m" .* cos.(0:0.01:10)
# positions
tuples = collect(zip(x, y, z))
svectors = SVector.(x, y, z)
points = Point.(x, y, z)

lines(x, y, z) # incorrect plot
lines(tuples) # doesn't work
lines(svectors) # doesn't work
lines(points) # doesn't work

#------
# MESH
#------

x = [0.0, 1.0, 0.0, 0.0] * u"m"
y = [0.0, 0.0, 1.0, 0.0] * u"m"
z = [0.0, 0.0, 0.0, 1.0] * u"m"
tuples = collect(zip(x, y, z))
svectors = SVector.(x, y, z)
points = Point.(x, y, z)

faces = [
  1 2 3
  1 2 4
  2 3 4
  1 3 4
]

mesh(tuples, faces) # doesn't work
mesh(svectors, faces) # doesn't work
mesh(points, faces) # doesn't work

#-------------
# MESHSCATTER
#-------------

x = cos.(1:0.5:20) * u"m"
y = sin.(1:0.5:20) * u"m"
z = LinRange(0, 3, length(x)) * u"m"

meshscatter(x, y, z) # incorrect plot

marker = Rect3((-1.0u"m", -1.0u"m", -1.0u"m"), (1.0u"m", 1.0u"m", 1.0u"m"))
meshscatter(x, y, z, marker=marker) # doesn't work

#-------
# IMAGE
#-------

x = (0u"m", 10u"m")
y = (0u"m", 10u"m")
img = rand(10, 10)
image(x, y, img) # doesn't work
juliohm commented 4 months ago

We are trying to make use of the new unit support in Makie, but can't do it currently because of the issues listed above. Is there a specific place in the source code that we can look into to help fix all of them?

ffreyer commented 4 months ago

Most of the plot object setup starts in interfaces.jl. For units it should be this: https://github.com/MakieOrg/Makie.jl/blob/225d0ae0e7c9db3bc43b2642327ee9a8ee84fed4/src/interfaces.jl#L220 Which continues in https://github.com/MakieOrg/Makie.jl/blob/225d0ae0e7c9db3bc43b2642327ee9a8ee84fed4/src/dim-converts/dim-converts.jl#L177-L193 and the other files in that folder. I'd guess you'll need to add a bunch of should_dim_convert() and convert_dim_value() methods.

juliohm commented 2 months ago

Thank you @ffreyer for sharing directions. We didn't have time to look into it. Any help is appreciated.