-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from legend-exp/singlefits
Add `residualplot!` and `lplot!` for histogram single fits
- Loading branch information
Showing
7 changed files
with
104 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# This file is a part of LegendMakie.jl, licensed under the MIT License (MIT). | ||
|
||
|
||
function round_wo_units(x::Unitful.RealOrRealQuantity; digits::Int=2) | ||
Unitful.unit(x) == Unitful.NoUnits ? round(x; digits) : round(Unitful.unit(x), x; digits) | ||
end | ||
|
||
function LegendMakie.lplot!( | ||
report::NamedTuple{(:f_fit, :h, :μ, :σ, :gof)}; | ||
title::AbstractString = "", show_residuals::Bool = true, | ||
xlabel = "", xticks = -4:2:4, xlims = (-5,5), ylims = (0,nothing), | ||
legend_position = :lt, watermark::Bool = true, kwargs... | ||
) | ||
|
||
fig = Makie.current_figure() | ||
|
||
g = Makie.GridLayout(fig[1,1]) | ||
ax = Makie.Axis(g[1,1], | ||
title = title, titlefont = :bold, titlesize = 16pt, xlabel = xlabel, | ||
xticks = xticks, limits = (xlims, ylims), ylabel = "Normalized Counts", | ||
) | ||
|
||
# Create histogram | ||
Makie.plot!(ax, report.h, label = "Data") | ||
|
||
_x = range(minimum(xlims), stop = maximum(xlims), length = 1000) | ||
Makie.lines!(_x, report.f_fit.(_x), color = :red, | ||
label = "Normal Fit\nμ = $(round_wo_units(report.μ, digits=2))\nσ = $(round_wo_units(report.σ, digits=2))") | ||
|
||
Makie.axislegend(ax, position = legend_position) | ||
|
||
if !isempty(report.gof) && show_residuals | ||
|
||
ax.xticklabelsize = 0 | ||
ax.xticksize = 0 | ||
ax.xlabel = "" | ||
|
||
ax2 = Makie.Axis(g[2,1], xticks = xticks, yticks = -3:3:3, limits = (xlims,(-5,5)), xlabel = xlabel, ylabel = "Residuals (σ)") | ||
LegendMakie.residualplot!(ax2, (x = report.gof.bin_centers, residuals_norm = [ifelse(abs(r) < 1e-6, 0.0, r) for r in report.gof.residuals_norm])) | ||
|
||
# link axis and put plots together | ||
Makie.linkxaxes!(ax, ax2) | ||
Makie.rowgap!(g, 0) | ||
Makie.rowsize!(g, 1, Makie.Auto(3)) | ||
|
||
# align ylabels | ||
yspace = maximum(Makie.tight_yticklabel_spacing!, (ax, ax2)) | ||
ax.yticklabelspace = yspace | ||
ax2.yticklabelspace = yspace | ||
end | ||
|
||
all = Makie.Axis(g[:,:]) | ||
Makie.hidedecorations!(all) | ||
Makie.hidespines!(all) | ||
Makie.current_axis!(all) | ||
|
||
watermark && LegendMakie.add_watermarks!(; kwargs...) | ||
|
||
fig | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# This file is a part of LegendMakie.jl, licensed under the MIT License (MIT). | ||
|
||
import LegendMakie: residualplot! | ||
|
||
Makie.@recipe(ResidualPlot, report) do scene | ||
Makie.Attributes( | ||
color_1σ = :darkgrey, | ||
color_3σ = :lightgrey, | ||
color = :black | ||
) | ||
end | ||
|
||
function Makie.plot!(p::ResidualPlot{<:Tuple{NamedTuple{(:x, :residuals_norm)}}}) | ||
report = p.report[] | ||
xvalues = report.x | ||
res = report.residuals_norm | ||
Makie.hspan!(p, [-3], [3], color = p.color_3σ) | ||
Makie.hspan!(p, [-1], [1], color = p.color_1σ) | ||
Makie.scatter!(p, xvalues, res, color = p.color) | ||
p | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
[deps] | ||
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" | ||
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" | ||
LegendSpecFits = "18221496-77af-46cf-bab8-820da09f7f97" | ||
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a" | ||
RadiationDetectorSignals = "bf2c0563-65cf-5db2-a620-ceb7de82658c" | ||
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" | ||
|
||
[compat] | ||
Documenter = "1" | ||
LegendSpecFits = "0.3" | ||
Makie = "0.22" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters