rafaqz / DimensionalData.jl

Named dimensions and indexing for julia arrays and other data
https://rafaqz.github.io/DimensionalData.jl/stable/
MIT License
281 stars 42 forks source link

Support plot mutation with global Axis #719

Open xzackli opened 5 months ago

xzackli commented 5 months ago

As described in https://github.com/rafaqz/DimensionalData.jl/pull/712#issuecomment-2117150302, plot mutation is no longer supported. i.e.

using DimensionalData, CairoMakie

A = rand(X(10:10:100), Y([:a, :b, :c]))

f = Figure(); ax = Axis(f[1,1])
Makie.plot!(A; colormap=:inferno)
f
Stacktrace ``` ERROR: MethodError: Cannot `convert` an object of type DimMatrix{ColorTypes.RGBA{Float32}, Tuple{X{DimensionalData.Dimensions.Lookups.Sampled{Int64, StepRange{Int64, Int64}, DimensionalData.Dimensions.Lookups.ForwardOrdered, DimensionalData.Dimensions.Lookups.Regular{Int64}, DimensionalData.Dimensions.Lookups.Points, DimensionalData.Dimensions.Lookups.NoMetadata}}, Y{DimensionalData.Dimensions.Lookups.Categorical{Symbol, Vector{Symbol}, DimensionalData.Dimensions.Lookups.ForwardOrdered, DimensionalData.Dimensions.Lookups.NoMetadata}}}, Tuple{}, Matrix{ColorTypes.RGBA{Float32}}, DimensionalData.NoName, DimensionalData.Dimensions.Lookups.NoMetadata} to an object of type Union{ColorTypes.RGBA{Float32}, Matrix{ColorTypes.RGBA{Float32}}} Closest candidates are: convert(::Type{T}, ::T) where T @ Base Base.jl:84 Stacktrace: [1] numbers_to_colors(numbers::DimMatrix{Float32, Tuple{X{DimensionalData.Dimensions.Lookups.Sampled{Int64, StepRange{Int64, Int64}, DimensionalData.Dimensions.Lookups.ForwardOrdered, DimensionalData.Dimensions.Lookups.Regular{Int64}, DimensionalData.Dimensions.Lookups.Points, DimensionalData.Dimensions.Lookups.NoMetadata}}, Y{DimensionalData.Dimensions.Lookups.Categorical{Symbol, Vector{Symbol}, DimensionalData.Dimensions.Lookups.ForwardOrdered, DimensionalData.Dimensions.Lookups.NoMetadata}}}, Tuple{}, Matrix{Float32}, DimensionalData.NoName, DimensionalData.Dimensions.Lookups.NoMetadata}, colormap::Vector{ColorTypes.RGBA{Float32}}, colorscale::Function, colorrange::Vec{2, Float32}, lowclip::ColorTypes.RGBA{Float32}, highclip::ColorTypes.RGBA{Float32}, nan_color::ColorTypes.RGBA{Float32}) @ Makie ~/.julia/packages/Makie/We6MY/src/colorsampler.jl:163 [2] to_color(c::Makie.ColorMapping{2, DimMatrix{Float32, Tuple{X{DimensionalData.Dimensions.Lookups.Sampled{Int64, StepRange{Int64, Int64}, DimensionalData.Dimensions.Lookups.ForwardOrdered, DimensionalData.Dimensions.Lookups.Regular{Int64}, DimensionalData.Dimensions.Lookups.Points, DimensionalData.Dimensions.Lookups.NoMetadata}}, Y{DimensionalData.Dimensions.Lookups.Categorical{Symbol, Vector{Symbol}, DimensionalData.Dimensions.Lookups.ForwardOrdered, DimensionalData.Dimensions.Lookups.NoMetadata}}}, Tuple{}, Matrix{Float32}, DimensionalData.NoName, DimensionalData.Dimensions.Lookups.NoMetadata}, DimMatrix{Float32, Tuple{X{DimensionalData.Dimensions.Lookups.Sampled{Int64, StepRange{Int64, Int64}, DimensionalData.Dimensions.Lookups.ForwardOrdered, DimensionalData.Dimensions.Lookups.Regular{Int64}, DimensionalData.Dimensions.Lookups.Points, DimensionalData.Dimensions.Lookups.NoMetadata}}, Y{DimensionalData.Dimensions.Lookups.Categorical{Symbol, Vector{Symbol}, DimensionalData.Dimensions.Lookups.ForwardOrdered, DimensionalData.Dimensions.Lookups.NoMetadata}}}, Tuple{}, Matrix{Float32}, DimensionalData.NoName, DimensionalData.Dimensions.Lookups.NoMetadata}}) @ Makie ~/.julia/packages/Makie/We6MY/src/colorsampler.jl:346 [3] draw_atomic(scene::Scene, screen::CairoMakie.Screen{CairoMakie.IMAGE}, primitive::Union{Image, Heatmap}) @ CairoMakie ~/.julia/packages/CairoMakie/fZ4Q9/src/primitives.jl:849 [4] draw_plot(scene::Scene, screen::CairoMakie.Screen{CairoMakie.IMAGE}, primitive::Heatmap{Tuple{Vector{Float32}, Vector{Float32}, DimMatrix{Float32, Tuple{X{DimensionalData.Dimensions.Lookups.Sampled{Int64, StepRange{Int64, Int64}, DimensionalData.Dimensions.Lookups.ForwardOrdered, DimensionalData.Dimensions.Lookups.Regular{Int64}, DimensionalData.Dimensions.Lookups.Points, DimensionalData.Dimensions.Lookups.NoMetadata}}, Y{DimensionalData.Dimensions.Lookups.Categorical{Symbol, Vector{Symbol}, DimensionalData.Dimensions.Lookups.ForwardOrdered, DimensionalData.Dimensions.Lookups.NoMetadata}}}, Tuple{}, Matrix{Float32}, DimensionalData.NoName, DimensionalData.Dimensions.Lookups.NoMetadata}}}) @ CairoMakie ~/.julia/packages/CairoMakie/fZ4Q9/src/infrastructure.jl:129 [5] cairo_draw(screen::CairoMakie.Screen{CairoMakie.IMAGE}, scene::Scene) @ CairoMakie ~/.julia/packages/CairoMakie/fZ4Q9/src/infrastructure.jl:51 [6] backend_show(screen::CairoMakie.Screen{CairoMakie.IMAGE}, io::IOBuffer, ::MIME{Symbol("image/png")}, scene::Scene) @ CairoMakie ~/.julia/packages/CairoMakie/fZ4Q9/src/display.jl:104 [7] backend_show(screen::CairoMakie.Screen{CairoMakie.IMAGE}, io::IOBuffer, ::MIME{Symbol("juliavscode/html")}, scene::Scene) @ Makie ~/.julia/packages/Makie/We6MY/src/display.jl:490 [8] show(io::IOBuffer, m::MIME{Symbol("juliavscode/html")}, figlike::Figure) @ Makie ~/.julia/packages/Makie/We6MY/src/display.jl:259 [9] __binrepr(m::MIME{Symbol("juliavscode/html")}, x::Figure, context::Nothing) @ Base.Multimedia ./multimedia.jl:171 [10] _textrepr @ ./multimedia.jl:163 [inlined] [11] repr(m::MIME{Symbol("juliavscode/html")}, x::Figure) @ Base.Multimedia ./multimedia.jl:159 [12] display(d::VSCodeServer.InlineDisplay, m::MIME{Symbol("juliavscode/html")}, x::Any) @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.79.2/scripts/packages/VSCodeServer/src/display.jl:68 [13] display(d::VSCodeServer.InlineDisplay, mime::String, x::Any) @ Base.Multimedia ./multimedia.jl:228 [14] display(d::VSCodeServer.InlineDisplay, x::Any) @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.79.2/scripts/packages/VSCodeServer/src/display.jl:207 [15] display(x::Any) @ Base.Multimedia ./multimedia.jl:340 [16] display(figlike::Figure; backend::Module, inline::MakieCore.Automatic, update::Bool, screen_config::@Kwargs{}) @ Makie ~/.julia/packages/Makie/We6MY/src/display.jl:150 [17] display(figlike::Figure) @ Makie ~/.julia/packages/Makie/We6MY/src/display.jl:130 [18] #invokelatest#2 @ ./essentials.jl:892 [inlined] [19] invokelatest @ ./essentials.jl:889 [inlined] [20] (::VSCodeServer.var"#69#74"{Bool, Bool, Bool, Module, String, Int64, Int64, String, VSCodeServer.ReplRunCodeRequestParams})() @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.79.2/scripts/packages/VSCodeServer/src/eval.jl:237 [21] withpath(f::VSCodeServer.var"#69#74"{Bool, Bool, Bool, Module, String, Int64, Int64, String, VSCodeServer.ReplRunCodeRequestParams}, path::String) @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.79.2/scripts/packages/VSCodeServer/src/repl.jl:276 [22] (::VSCodeServer.var"#68#73"{Bool, Bool, Bool, Module, String, Int64, Int64, String, VSCodeServer.ReplRunCodeRequestParams})() @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.79.2/scripts/packages/VSCodeServer/src/eval.jl:179 [23] hideprompt(f::VSCodeServer.var"#68#73"{Bool, Bool, Bool, Module, String, Int64, Int64, String, VSCodeServer.ReplRunCodeRequestParams}) @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.79.2/scripts/packages/VSCodeServer/src/repl.jl:38 [24] (::VSCodeServer.var"#67#72"{Bool, Bool, Bool, Module, String, Int64, Int64, String, VSCodeServer.ReplRunCodeRequestParams})() @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.79.2/scripts/packages/VSCodeServer/src/eval.jl:150 [25] with_logstate(f::Function, logstate::Any) @ Base.CoreLogging ./logging.jl:515 [26] with_logger @ ./logging.jl:627 [inlined] [27] (::VSCodeServer.var"#66#71"{VSCodeServer.ReplRunCodeRequestParams})() @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.79.2/scripts/packages/VSCodeServer/src/eval.jl:263 [28] #invokelatest#2 @ ./essentials.jl:892 [inlined] [29] invokelatest(::Any) @ Base ./essentials.jl:889 [30] (::VSCodeServer.var"#64#65")() @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.79.2/scripts/packages/VSCodeServer/src/eval.jl:34 ``` ``` julia> using Pkg; Pkg.status() Status `~/src/sandbox/2024/bug_makie_dd/Project.toml` [13f3f980] CairoMakie v0.12.2 [0703355e] DimensionalData v0.27.3 [ee78f7c6] Makie v0.21.2 ```
rafaqz commented 5 months ago

No that syntax was never supported here, as Makie isn't really set up to do it yet.

Basically you always have to use the ax first argument:

Makie.plot!(ax, A; colormap=:inferno)

(if you can figure out how to fix it, go for it...)

xzackli commented 5 months ago

I'm a bit embarrassed; sorry for using up your time on this!

rafaqz commented 5 months ago

No worries, I make that mistake all the time too

rafaqz commented 5 months ago

And seriously, there must be a way to fix it, I just don't know the Makie internals for that well enough

asinghvi17 commented 5 months ago

Shouldnt defining plottype(::DimensionalArray}) just work?

rafaqz commented 5 months ago

No idea ;)

felixcremer commented 5 months ago

No that syntax was never supported here, as Makie isn't really set up to do it yet.

Basically you always have to use the ax first argument:

Makie.plot!(ax, A; colormap=:inferno)

(if you can figure out how to fix it, go for it...)

That is not true, it works in GLMakie as expected and I can reproduce the error in CairoMakie.

rafaqz commented 5 months ago

Hm it could partly work I guess just with the defaults

felixcremer commented 5 months ago

I also suspect, that this is something that changed with Makie 0.21 because with Makie 0.20 I get a different error about symbol handling with the provided DimArray, but when I change the Y axis to have integer values the plot! works without the axis.

asinghvi17 commented 5 months ago

Ah we probably need to define expand_dimensions then