jw3126 / UnitfulRecipes.jl

Plots.jl recipes for Unitful.jl arrays
MIT License
37 stars 10 forks source link

unitful yerr seems to break #42

Closed jstrube closed 3 years ago

jstrube commented 3 years ago

great package! Unfortunately, I couldn't get this to work with a unitful yerr. I'm getting an error message of "incompatible units" if I try to put a unitful yerr on a unitful value. The error message actually reports the y value without units, and the yerr with units. For example:

momenta = [p*1u"GeV/c" for p in 1.0:0.1:10]
masses = [139.57039u"MeV/c^2"]
piMass = ones(length(momenta))*masses[1]
piErr=[10.882833439073304u"MeV/c^2" for p in momenta]
yerr=piErr
plot(momenta, [piMass], yerr=yerr, label=["pion"])

gives me:

DimensionError: 139.57039 and 10.882833439073304 MeV c^-2 are not dimensionally compatible.

Stacktrace:
  [1] -(x::Quantity{Float64, NoDims, Unitful.FreeUnits{(), NoDims, nothing}}, y::Quantity{Float64, 𝐌, Unitful.FreeUnits{(c^-2, MeV), 𝐌, nothing}})
    @ Unitful ~/.julia/packages/Unitful/JwSBO/src/quantities.jl:137
  [2] -(x::Float64, y::Quantity{Float64, 𝐌, Unitful.FreeUnits{(c^-2, MeV), 𝐌, nothing}})
    @ Base ./promotion.jl:323
  [3] error_coords(errorbar::Vector{Quantity{Float64, 𝐌, Unitful.FreeUnits{(c^-2, MeV), 𝐌, nothing}}}, errordata::Vector{Float64}, otherdata::Vector{Float64})
    @ Plots ~/.julia/packages/Plots/SjqWU/src/recipes.jl:1121
  [4] macro expansion
    @ ~/.julia/packages/Plots/SjqWU/src/recipes.jl:1159 [inlined]
  [5] apply_recipe(plotattributes::AbstractDict{Symbol, Any}, #unused#::Type{Val{:yerror}}, x::Any, y::Any, z::Any)
    @ Plots ~/.julia/packages/RecipesBase/92zOw/src/RecipesBase.jl:282
  [6] _process_seriesrecipe(plt::Any, plotattributes::Any)
    @ RecipesPipeline ~/.julia/packages/RecipesPipeline/VEk89/src/series_recipe.jl:50
  [7] _process_seriesrecipes!(plt::Any, kw_list::Any)
    @ RecipesPipeline ~/.julia/packages/RecipesPipeline/VEk89/src/series_recipe.jl:27
  [8] recipe_pipeline!(plt::Any, plotattributes::Any, args::Any)
    @ RecipesPipeline ~/.julia/packages/RecipesPipeline/VEk89/src/RecipesPipeline.jl:97
  [9] _plot!(plt::Plots.Plot, plotattributes::Any, args::Any)
    @ Plots ~/.julia/packages/Plots/SjqWU/src/plot.jl:172
 [10] #plot#129
    @ ~/.julia/packages/Plots/SjqWU/src/plot.jl:58 [inlined]
 [11] top-level scope
    @ In[31]:11
 [12] eval
    @ ./boot.jl:360 [inlined]
 [13] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
    @ Base ./loading.jl:1090
briochemc commented 3 years ago

Yep, this is not supported. You might the first one to try it! I think we could add a recipe for that. I'll take a look.

jstrube commented 3 years ago

great, thank you!

briochemc commented 3 years ago

@jw3126, I think #43 will fix this issue but I would like to add an example similar to @jstrube's example, albeit with a bit prettier output. What about (edited the code)

using UnitfulRecipes, Plots, Unitful
using Unitful: GeV, MeV, c
x = (1.0:0.1:10) * GeV/c
y = @. (2 + sin(x / (GeV/c))) * 139.6MeV/c^2 # a sine to make it pretty
yerror = 10.9MeV/c^2 * exp.(randn(length(x))) # some noise for pretty again
plot(x, y; yerror, title="My unitful data with yerror bars", lab="")

which gives

Screen Shot 2021-03-02 at 10 56 42 am

Otherwise, @jstrube, maybe you have a more realistic example that we can use instead? Fee free to suggest anything! πŸ˜„


Edited the code example

jstrube commented 3 years ago

I think the example looks fine. I just wouldn't call the variable piMassπŸ˜‰. Thanks for fixing that so quickly.

briochemc commented 3 years ago

Added the example that you should now be able to see in https://jw3126.github.io/UnitfulRecipes.jl/previews/PR43/examples/1_Examples/#Error-bars-1

briochemc commented 3 years ago

@jstrube give it a few minutes (hours) for the new version to be released and you should be able to pkg> up and get going with the unitful errorbars!