MakieOrg / Makie.jl

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

text! with relative space does not work with DateTime x-axis #4324

Open rbeeli opened 2 months ago

rbeeli commented 2 months ago

When using the text! function and space=:relative, it does not work when the x-axis is of DateTime type. In my understanding, if space=:relative is set, it should always accept floating point x, y coordinates, independent of the axis data types, since it's relative to the plot area, not the data space?

Self-contained, reproducible example:

using Makie, Dates

f = Figure();
ax = Axis(f[1, 1]);
x = [DateTime(2020, 1, 1), DateTime(2020, 1, 2), DateTime(2020, 1, 3)];
y = [1.0, 2.0, 1.5];
Makie.lines!(ax, x, y, color=:yellow)

text!(ax, 0.0, 1.0,
    text="Title",
    font=:bold,
    align=(:left, :top),
    offset=(8, -4),
    space=:relative)

display(f);

Output:

julia> text!(ax, 0.0, 1.0,
           text="Title",
           font=:bold,
           align=(:left, :top),
           offset=(8, -4),
           space=:relative)
ERROR: Plotting unit Float64 into axis with type DateTime not supported.
Stacktrace:
  [1] error(s::String)
    @ Base ./error.jl:35
  [2] convert_dim_observable(conversion::Makie.DateTimeConversion, values::Observable{…}, deregister::Vector{…})
    @ Makie ~/.julia/packages/Makie/8h0bl/src/dim-converts/dates-integration.jl:79
  [3] convert_dim_observable(conversions::Makie.DimConversions, dim::Int64, value::Observable{…}, deregister::Vector{…})
    @ Makie ~/.julia/packages/Makie/8h0bl/src/dim-converts/dim-converts.jl:198
  [4] (::Makie.var"#209#211"{UnionAll, NoConversion, Tuple{…}, Vector{…}, Makie.DimConversions})(i::Int64)
    @ Makie ~/.julia/packages/Makie/8h0bl/src/dim-converts/dim-converts.jl:189
  [5] ntuple
    @ ./ntuple.jl:19 [inlined]
  [6] try_dim_convert(P::Type{…}, PTrait::NoConversion, user_attributes::Dict{…}, args_obs::Tuple{…}, deregister::Vector{…})
    @ Makie ~/.julia/packages/Makie/8h0bl/src/dim-converts/dim-converts.jl:183
  [7] conversion_pipeline(P::Type{…}, used_attrs::Tuple{}, args::Tuple{…}, args_obs::Tuple{…}, user_attributes::Dict{…}, deregister::Vector{…}, recursion::Int64)
    @ Makie ~/.julia/packages/Makie/8h0bl/src/interfaces.jl:221
  [8] conversion_pipeline(P::Type{…}, used_attrs::Tuple{}, args::Tuple{…}, args_obs::Tuple{…}, user_attributes::Dict{…}, deregister::Vector{…})
    @ Makie ~/.julia/packages/Makie/8h0bl/src/interfaces.jl:214
  [9] (MakieCore.Text)(user_args::Tuple{Float64, Float64}, user_attributes::Dict{Symbol, Any})
    @ Makie ~/.julia/packages/Makie/8h0bl/src/interfaces.jl:272
 [10] _create_plot!(::Function, ::Dict{Symbol, Any}, ::Axis, ::Float64, ::Vararg{Float64})
    @ Makie ~/.julia/packages/Makie/8h0bl/src/figureplotting.jl:377
 [11] text!(::Axis, ::Vararg{Any}; kw::@Kwargs{text::String, font::Symbol, align::Tuple{…}, offset::Tuple{…}, space::Symbol})
    @ MakieCore ~/.julia/packages/MakieCore/hXATT/src/recipes.jl:440
 [12] top-level scope
    @ REPL[23]:1
asinghvi17 commented 2 months ago

I think if space != :data, we should always allow numbers (and disallow units) by skipping the dim conversion?