Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Specifying 'errorbar' with asymmetric error bar data with 'Unitful' values returns DimensionError #4957

Open
jmtlawrie opened this issue Jul 6, 2024 · 0 comments

Comments

@jmtlawrie
Copy link

Details

Hello!

Plotting data with Unitful.jl units and specifiying to plot asymmetrical error bars for these data, giving the errorbar data also as data with Unitful.jl units results in a DimensionError due to attempting to subtract incompatible Unitful.Quantity from each other.

My best guess (so far) is that something occurs (or doesn't occur, but it should) within UnitfulExt.jl. I looked specifically at when the attributes are fixed by the function fixaxis!, but unfortunately I couldn't quite understand why it is only asymmetrical error bar data which seem to cause an issue.

A workaround is to pre-process the error data to ensure that the data are converted to the same unit as the axis, and that the Unitful.jl units are removed before plot() is called.

Thanks!

J

Minimum working example

using Plots
using Unitful

x = [1.0, 2.0]
y = x .* u"m"
ϵₛ = y         # Symmetric error bar data
ϵₗ = y          # Lower error bound data
ϵₕ = ϵₗ ./ 2     # Upper error bound data

# Symmetric error bar data with Unitful units -> no problem
plot(x, y; yerror = ϵₛ)

# Asymmetric error bar data with Unitful units -> returns 'DimensionError' attempting to subtract unitless value from value with units
plot(x, y; yerror = (ϵₗ, ϵₕ)) 

# Asymmetric error bar data without Unitful units -> plots successfully, but user must ensure the error bar values match the axis units 
plot(x, y; yerror = map(t -> ustrip.(t), (ϵₗ, ϵₕ)))

Full error message:

ERROR: DimensionError: 1.0 and 1.0 m are not dimensionally compatible.
Stacktrace:
  [1] -(x::Quantity{Float64, NoDims, Unitful.FreeUnits{…}}, y::Quantity{Float64, 𝐋, Unitful.FreeUnits{…}})
    @ Unitful ~/.julia/packages/Unitful/6r8Hq/src/quantities.jl:137
  [2] -(x::Float64, y::Quantity{Float64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}})
    @ Base ./promotion.jl:424
  [3] error_coords(errorbar::Vector{Tuple{Quantity{…}, Quantity{…}}}, errordata::Vector{Float64}, otherdata::Vector{Float64})
    @ Plots ~/.julia/packages/Plots/du2dt/src/recipes.jl:1132
  [4] macro expansion
    @ ~/.julia/packages/Plots/du2dt/src/recipes.jl:1167 [inlined]
  [5] apply_recipe(plotattributes::AbstractDict{Symbol, Any}, ::Type{Val{:yerror}}, x::Any, y::Any, z::Any)
    @ Plots ~/.julia/packages/RecipesBase/BRe07/src/RecipesBase.jl:300
  [6] _process_seriesrecipe(plt::Any, plotattributes::Any)
    @ RecipesPipeline ~/.julia/packages/RecipesPipeline/BGM3l/src/series_recipe.jl:50
  [7] _process_seriesrecipes!(plt::Any, kw_list::Any)
    @ RecipesPipeline ~/.julia/packages/RecipesPipeline/BGM3l/src/series_recipe.jl:27
  [8] recipe_pipeline!(plt::Any, plotattributes::Any, args::Any)
    @ RecipesPipeline ~/.julia/packages/RecipesPipeline/BGM3l/src/RecipesPipeline.jl:99
  [9] _plot!(plt::Plots.Plot, plotattributes::Any, args::Any)
    @ Plots ~/.julia/packages/Plots/du2dt/src/plot.jl:223
 [10] plot(::Any, ::Vararg{Any}; kw...)
    @ Plots ~/.julia/packages/Plots/du2dt/src/plot.jl:102
 [11] top-level scope
    @ ~/Desktop/Projects/cuddly-journey/UnitfulPlots_test.jl:12
Some type information was truncated. Use `show(err)` to see complete types.

Backends

This bug occurs on ( insert x below )

Backend yes no untested
gr (default) x
pythonplot x
plotlyjs x
pgfplotsx x
unicodeplots x
inspectdr x
gaston x

Versions

Plots.jl version:

[91a5bcdd] Plots v1.40.5

Backend version (]st -m <backend(s)>):

[28b8d3ca] GR v0.73.6

Output of versioninfo():

julia> versioninfo()
Julia Version 1.10.3
Commit 0b4590a5507 (2024-04-30 10:59 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: macOS (x86_64-apple-darwin22.4.0)
  CPU: 4 × Intel(R) Core(TM) i5-5250U CPU @ 1.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, broadwell)
Threads: 1 default, 0 interactive, 1 GC (on 4 virtual cores)
Environment:
  JULIA_EDITOR = code
  JULIA_NUM_THREADS = 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants