JuliaPhysics / Measurements.jl

Error propagation calculator and library for physical measurements. It supports real and complex numbers with uncertainty, arbitrary precision calculations, operations with arrays, and numerical integration.

Home Page:https://juliaphysics.github.io/Measurements.jl/stable/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[FR] Plot recipe: ribbon plots option beside error bars

kapple19 opened this issue · comments

I'm happy to submit a pull request on this, I just want to confirm that the package devs are happy with this suggestion: add a ribbon option for the plot output. Motivation: Plots.jl and Measurements.jl don't have a simple "switch" to flick to use the readily-available info in a Measurement instance of the ± information.

I'm not sure if I would (1) add a new recipe to plot-recipes.jl or (2) alter an existing recipe.

The recipe I could alter would be something like

@recipe function plot(x::AbstractVector{<:Real}, y::AbstractVector{<:Measurement}, err::Symbol = :bar)
	if err == :ribbon
		ribbon := uncertainty.(y)
	elseif err == :bar
		yerror := uncertainty.(y)
	else
		error("Unrecognized error bar display option.")
	end
	x, value.(y)
end

Please advise, this would be my first ever pull request, and I'm still learning about Plots.jl recipes. Thanks for the great package!

Hi, thanks for the interest! I agree that having the ability to choose between a ribbon and error bars would be cool, but I have two concerns:

  • does the ribbon make sense if you plot vectors? Or is it better to use it only for something smooth, like a function (plot(f, x))? Probably the distinction vectors vs function as input is moot, so maybe this concern is not too relevant, not sure
  • you suggest to add a keyword argument (I don't like err very much though, a bit cryptic and confusing), but how does that work in practice? Does it compose with other keyword arguments defined by other recipes? I never explored this possibility, I really don't know how this works

If you can provide answers to these questions, I'd very much appreciate a pull request

does the ribbon make sense if you plot vectors? Or is it better to use it only for something smooth, like a function (plot(f, x))? Probably the distinction vectors vs function as input is moot, so maybe this concern is not too relevant, not sure

I agree, it's better for a function.

I don't like err very much

Yeah me too, I can't think of a name.

Does it compose with other keyword arguments defined by other recipes?

Yes, it does! I did test this one out. It adds err (or whatever we might choose) to the optional arguments. I might test it a bit more to see how it behaves in different order, if it's better as a keyword argument, etc.

I'd very much appreciate a pull request

Okay cool. As I was writing this issue, I realised that I would need to do more experimenting and digging into the bigger picture of Measurements.jl and RecipesBase.jl to understand the best way to do this. I'll let you know once I've rigorously and thoroughly thought out some potential solution(s). Thanks @giordano!

Hello! :) Is there something new to this? I would use this quite often for model simulations where ribbons are way more effective than error bars.

My workaround right now is something like this:

y1 = Measurements.value.(y_meas)
y1u = Measurements.uncertainty.(y_meas)
# creating the ribbon
p = plot(t, y1.-y1u, fillranges = y1.+y1u, fillalpha = 0.25, linewidth=0, label = "" , fillcolor = 1)
# plotting the mean value on top
p = plot!(t, y1, label = "model", color=1)

Is there something new to this?

I already explained I don't know how to do this myself, if someone has a clear view of how to best implement this, they're welcome to submit a pull request. I don't think that has happened yet.

Hello! :) Is there something new to this? I would use this quite often for model simulations where ribbons are way more effective than error bars.

My apologies, I never followed through on this. I'll action it next chance I get.

I have some changes on a local branch in a local clone, but I don't have permission to push. @giordano how would you like me to submit a pull request?

@dfabianus here's a preview =)

x = range(0, 2π, 301)
yval = cospi.(x)
yunc = (5 .+ 2sinpi.(5x))/10
y = measurement.(yval, yunc)
plot(x, y,
    uncertainty_plot = :ribbon,
    label = "model",
    fillalpha = 0.25,
    color = 1
)

dfabianus

The defaults are inherited from Plots.jl of course:

plot(x, y, uncertainty_plot = :ribbon)

dfabianus_newer

Edit: Noticed the double pi in my definitions haha.

Yes, please, open a PR!

@giordano how do I get permission to push to JuliaPhysics/Measurements.jl? I can't open a pull request because I don't have permission to publish and push my branch to this repository.

Looks good @kapple19 ! Did you make a fork? To open a pull request usually you have to fork the repository first.
Have a look here:
https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request-from-a-fork

Ah, thanks heaps! I didn't know I had to fork it.

Edit: I should add some unit tests actually.