MakieOrg / Makie.jl

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

supplying a colorrange as a steprange fails #4097

Open tiemvanderdeure opened 2 months ago

tiemvanderdeure commented 2 months ago

Supplying a color and a colorrange errors sometimes, seemingly only if the color is an integer above 2.

To reproduce, simply:

using CairoMakie
plot(rand(10), color = 3, colorrange = 1:10)

All of the following code works as expected:

plot(rand(10), color = 2, colorrange = 1:10)
plot(rand(10), color = fill(3, 10), colorrange = 1:10)

stacktrace:

ERROR: MethodError: Cannot `convert` an object of type 
  Vector{MakieCore.Automatic} to an object of type 
  Union{ColorTypes.RGBA{Float32}, Vector{ColorTypes.RGBA{Float32}}}

Closest candidates are:
  convert(::Type{T}, ::T) where T
   @ Base Base.jl:84

Stacktrace:
  [1] numbers_to_colors(numbers::Vector{…}, colormap::Vector{…}, colorscale::Function, colorrange::Vec{…}, lowclip::MakieCore.Automatic, highclip::MakieCore.Automatic, nan_color::ColorTypes.RGBA{…})
    @ Makie ~/.julia/packages/Makie/rEu75/src/colorsampler.jl:163
  [2] (::Makie.var"#116#118")(::Vector{Float32}, ::Vararg{Any})
    @ Makie ~/.julia/packages/Makie/rEu75/src/colorsampler.jl:368
  [3] map(::Makie.var"#116#118", ::Observable{…}, ::Observable{…}, ::Vararg{…}; ignore_equal_values::Bool, priority::Int64)
    @ Observables ~/.julia/packages/Observables/YdEbO/src/Observables.jl:570
  [4] map(::Makie.var"#116#118", ::Observable{Vector{…}}, ::Observable{Vector{…}}, ::Function, ::Vararg{Any})
    @ Observables ~/.julia/packages/Observables/YdEbO/src/Observables.jl:568
  [5] assemble_colors(::Int64, color::Observable{Any}, plot::Scatter{Tuple{Vector{Point{2, Float64}}}})
    @ Makie ~/.julia/packages/Makie/rEu75/src/colorsampler.jl:368
  [6] color_and_colormap!(plot::Scatter{Tuple{Vector{Point{2, Float64}}}}, colors::Observable{Any})
    @ Makie ~/.julia/packages/Makie/rEu75/src/interfaces.jl:14
  [7] color_and_colormap!(plot::Scatter{Tuple{Vector{Point{2, Float64}}}})
    @ Makie ~/.julia/packages/Makie/rEu75/src/interfaces.jl:10
  [8] calculated_attributes!
    @ ~/.julia/packages/Makie/rEu75/src/interfaces.jl:64 [inlined]
  [9] connect_plot!(parent::Scene, plot::Scatter{Tuple{Vector{Point{2, Float64}}}})
    @ Makie ~/.julia/packages/Makie/rEu75/src/interfaces.jl:378
 [10] plot!
    @ ~/.julia/packages/Makie/rEu75/src/interfaces.jl:389 [inlined]
 [11] plot!(ax::Axis, plot::Scatter{Tuple{Vector{Point{2, Float64}}}})
    @ Makie ~/.julia/packages/Makie/rEu75/src/figureplotting.jl:412
 [12] plot!(fa::Makie.FigureAxis, plot::Scatter{Tuple{Vector{Point{2, Float64}}}})
    @ Makie ~/.julia/packages/Makie/rEu75/src/figureplotting.jl:407
 [13] _create_plot(F::Function, attributes::Dict{Symbol, Any}, args::Vector{Float64})
    @ Makie ~/.julia/packages/Makie/rEu75/src/figureplotting.jl:318
 [14] #plot#10
    @ ~/.julia/packages/MakieCore/f3yyf/src/recipes.jl:39 [inlined]
 [15] top-level scope
    @ REPL[7]:1
asinghvi17 commented 2 months ago

The issue here is that the colorrange has to be a tuple - we should have a better error though.

plot(rand(10), color = 3, colorrange = (1, 10))

should work.

I guess we could also have a convert for abstract vectors which just takes extrema for colorrange...

tiemvanderdeure commented 2 months ago

Ah of course, I thought it was weird that no one would have run into this before. ;)

I got thrown of by the fact that it worked for color = 1 and color = 2 - and still don't get why those other examples don't error?

I think it's reasonable to require colorrange to be a tuple, allowing vectors and calling extrema opens a whole lot of edge cases and users can just call extrema themselves. Maybe this should just have a check and a more straightforward error message? Passing a range as the colorrange is an easy mistake to make.