MakieOrg / AlgebraOfGraphics.jl

An algebraic spin on grammar-of-graphics data visualization in Julia. Powered by the Makie.jl plotting ecosystem.
https://aog.makie.org
MIT License
444 stars 45 forks source link

Cannot use alpha with numeric color mapping #371

Open knuesel opened 2 years ago

knuesel commented 2 years ago

The following works fine:

df = (x=1:3, y=1:3, c=1:3)

plt = data(df) * mapping(:x, :y, color=:c => nonnumeric) * visual(alpha=0.5)

draw(plt)

but if I remove the nonnumeric I get MethodError: no method matching color(::Vector{Int64}).

I guess this will be fixed at the same time as #225, but in the meantime it would be nice to have a workaround, so that alpha can be used with continous color scales...

Stack trace
MethodError: no method matching color(::Vector{Int64})
Closest candidates are:
  color(::ColorTypes.Color) at ~/.julia/packages/ColorTypes/6m8P7/src/traits.jl:108
  color(::ColorTypes.TransparentColor{C, T, 2} where T) where C at ~/.julia/packages/ColorTypes/6m8P7/src/traits.jl:109
  color(::ColorTypes.TransparentColor{C, T, 3} where T) where C at ~/.julia/packages/ColorTypes/6m8P7/src/traits.jl:110
  ...

Stacktrace:
  [1] convert_attribute(c::Tuple{Vector{Int64}, Float64}, k::MakieCore.Key{:color})
    @ Makie ~/.julia/packages/Makie/vwpRo/src/conversions.jl:771
  [2] get_attribute(dict::Scatter{Tuple{Vector{Point{2, Float32}}}}, key::Symbol)
    @ MakieCore ~/.julia/packages/MakieCore/A0hGm/src/attributes.jl:242
  [3] draw_atomic(scene::Scene, screen::CairoMakie.CairoScreen{Cairo.CairoSurfaceBase{UInt32}}, primitive::Scatter{Tuple{Vector{Point{2, Float32}}}})
    @ CairoMakie ~/.julia/packages/CairoMakie/JAJ5G/src/primitives.jl:176
  [4] draw_plot(scene::Scene, screen::CairoMakie.CairoScreen{Cairo.CairoSurfaceBase{UInt32}}, primitive::Scatter{Tuple{Vector{Point{2, Float32}}}})
    @ CairoMakie ~/.julia/packages/CairoMakie/JAJ5G/src/infrastructure.jl:230
  [5] cairo_draw(screen::CairoMakie.CairoScreen{Cairo.CairoSurfaceBase{UInt32}}, scene::Scene)
    @ CairoMakie ~/.julia/packages/CairoMakie/JAJ5G/src/infrastructure.jl:172
  [6] backend_show(x::CairoMakie.CairoBackend, io::IOContext{Base64.Base64EncodePipe}, #unused#::MIME{Symbol("image/png")}, scene::Scene)
    @ CairoMakie ~/.julia/packages/CairoMakie/JAJ5G/src/infrastructure.jl:335
  [7] show(io::IOContext{Base64.Base64EncodePipe}, m::MIME{Symbol("image/png")}, scene::Scene)
    @ Makie ~/.julia/packages/Makie/vwpRo/src/display.jl:109
  [8] #show#920
    @ ~/.julia/packages/Makie/vwpRo/src/display.jl:103 [inlined]
  [9] show
    @ ~/.julia/packages/Makie/vwpRo/src/display.jl:103 [inlined]
 [10] show
    @ ~/.julia/packages/AlgebraOfGraphics/8WZVt/src/entries.jl:89 [inlined]
 [11] limitstringmime(mime::MIME{Symbol("image/png")}, x::AlgebraOfGraphics.FigureGrid)
    @ IJulia ~/.julia/packages/IJulia/e8kqU/src/inline.jl:50
 [12] display_mimestring
    @ ~/.julia/packages/IJulia/e8kqU/src/display.jl:67 [inlined]
 [13] display_dict(f::AlgebraOfGraphics.FigureGrid)
    @ Main ./In[76]:2
 [14] #invokelatest#2
    @ ./essentials.jl:716 [inlined]
 [15] invokelatest
    @ ./essentials.jl:714 [inlined]
 [16] execute_request(socket::ZMQ.Socket, msg::IJulia.Msg)
    @ IJulia ~/.julia/packages/IJulia/e8kqU/src/execute_request.jl:112
 [17] #invokelatest#2
    @ ./essentials.jl:716 [inlined]
 [18] invokelatest
    @ ./essentials.jl:714 [inlined]
 [19] eventloop(socket::ZMQ.Socket)
    @ IJulia ~/.julia/packages/IJulia/e8kqU/src/eventloop.jl:8
 [20] (::IJulia.var"#15#18")()
    @ IJulia ./task.jl:423

Tested with AlgebraOfGraphics 0.6.5 and Makie 0.16.5.

piever commented 2 years ago

This will be slightly trickier to solve in a clean way. Makie support alpha transparency for continuous color directly in the colormap attribute, so visual(colormap=(:batlow, 0.5)) should work. It should be feasible to get this to work by default.

knuesel commented 2 years ago

Thanks for the workaround with colormap! Though it's annoying in my case that the colorbar is also affected:

df = (x=vec((1:3) .+ rand(200)'),
      y=vec((1:3) .+ rand(200)'),
      c=vec([1,2,5] .+ rand(200)'))

plt = data(df) * mapping(:x, :y, color=:c) * visual(colormap=(:viridis, 0.3))

draw(plt, axis=(width=300, height=200))

image

czimm79 commented 2 years ago

@knuesel Have you since figured out a way to prevent the colorbar from being affected? I'm trying a similar plot.

knuesel commented 2 years ago

@czimm79 no for now I'm just not using transparency in those plots with continuous color scale.

ericphanson commented 1 year ago

just to say the original example works for me now; I'm on

AlgebraOfGraphics v0.6.12
CairoMakie v0.9.4
Makie v0.18.4
knuesel commented 1 year ago

I still get an error on

AlgebraOfGraphics v0.6.16
CairoMakie v0.10.6
Makie v0.19.6

@ericphanson are you sure you removed nonnumeric from the example code?

ericphanson commented 1 year ago

Oh oops, no I just copied the example code as-is. I must've misread, sorry.