From 4668e60a4526edc2fe10933d48fe182ec2e447a6 Mon Sep 17 00:00:00 2001 From: Kipton Barros Date: Sun, 8 Sep 2024 09:10:20 -0400 Subject: [PATCH 1/3] Make Colorbar more reliable --- ext/PlottingExt.jl | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/ext/PlottingExt.jl b/ext/PlottingExt.jl index 0d4f30715..1ba1b3db7 100644 --- a/ext/PlottingExt.jl +++ b/ext/PlottingExt.jl @@ -1013,11 +1013,25 @@ function colorrange_from_data(; data, saturation, sensitivity, allpositive) cmax = Statistics.quantile(vec(maximum(data; dims=1)), saturation) cmin = Statistics.quantile(vec(minimum(data; dims=1)), 1 - saturation) + # The returned (lo, hi) should satisfy lo < hi in all cases to allow for a + # meaningful Makie.Colorbar. if allpositive - return (sensitivity, 1) .* cmax + # Intensities are supposed to be non-negative in this branch, but might + # not be due to any of: User error, very small roundative around + # negative zero, or Gibbs ringing in a KPM calculation. + @assert 0 <= sensitivity < 1 + if iszero(abs(cmax)) + return (0, 1e-15) + else + (sensitivity, 1) .* abs(cmax) + end else - cabsmax = max(abs(cmax), abs(cmin)) - return (-cabsmax, cabsmax) + cscale = max(abs(cmax), abs(cmin)) + if iszero(cscale) + return (-1e-15, 1e-15) + else + return (-cscale, cscale) + end end end @@ -1113,7 +1127,7 @@ function Sunny.plot_intensities!(panel, res::Sunny.Intensities{Float64}; colorma xticklabelrotation = maximum(length.(qpts.xticks[2])) > 3 ? π/6 : 0.0 ax = Makie.Axis(panel[1, 1]; xlabel="Momentum (r.l.u.)", ylabel, qpts.xticks, xticklabelrotation, axisopts...) hm = Makie.heatmap!(ax, axes(data, 2), collect(energies/unit_energy), data'; colormap, colorrange) - allequal(colorrange) || Makie.Colorbar(panel[1, 2], hm) + Makie.Colorbar(panel[1, 2], hm) return ax elseif qpts isa Sunny.QGrid{2} if isone(length(energies)) @@ -1122,7 +1136,7 @@ function Sunny.plot_intensities!(panel, res::Sunny.Intensities{Float64}; colorma (xs, ys) = map(range, qpts.coefs_lo, qpts.coefs_hi, size(qpts.qs)) ax = Makie.Axis(panel[1, 1]; xlabel, ylabel, aspect, axisopts...) hm = Makie.heatmap!(ax, xs, ys, dropdims(data; dims=1); colormap, colorrange) - allequal(colorrange) || Makie.Colorbar(panel[1, 2], hm) + Makie.Colorbar(panel[1, 2], hm) return ax else error("Cannot yet plot $(typeof(res))") @@ -1139,14 +1153,14 @@ function Sunny.plot_intensities!(panel, res::Sunny.StaticIntensities{Float64}; c colorrange_suggest = colorrange_from_data(; data=reshape(data, 1, size(data)...), saturation, sensitivity=0, allpositive) colormap = @something colormap (allpositive ? :gnuplot2 : :bwr) colorrange = @something colorrange colorrange_suggest - + if qpts isa Sunny.QGrid{2} aspect = grid_aspect_ratio(crystal, qpts) xlabel, ylabel = suggest_labels_for_grid(qpts) (xs, ys) = map(range, qpts.coefs_lo, qpts.coefs_hi, size(qpts.qs)) ax = Makie.Axis(panel[1, 1]; xlabel, ylabel, aspect, axisopts...) hm = Makie.heatmap!(ax, xs, ys, data; colormap, colorrange) - allequal(colorrange) || Makie.Colorbar(panel[1, 2], hm) + Makie.Colorbar(panel[1, 2], hm) return ax elseif qpts isa Sunny.QPath xticklabelrotation = maximum(length.(qpts.xticks[2])) > 3 ? π/6 : 0.0 @@ -1170,7 +1184,7 @@ function Sunny.plot_intensities!(panel, res::Sunny.PowderIntensities{Float64}; c ax = Makie.Axis(panel[1, 1]; xlabel, ylabel, axisopts...) hm = Makie.heatmap!(ax, res.radii, collect(res.energies/unit_energy), res.data'; colormap, colorrange) - allequal(colorrange) || Makie.Colorbar(panel[1, 2], hm) + Makie.Colorbar(panel[1, 2], hm) return ax end From a9d8332bd7334214e3c637ee21930e5f308085a9 Mon Sep 17 00:00:00 2001 From: Kipton Barros Date: Sun, 8 Sep 2024 09:20:51 -0400 Subject: [PATCH 2/3] Document fix --- docs/src/versions.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/src/versions.md b/docs/src/versions.md index fa4cdf001..443aea1cf 100644 --- a/docs/src/versions.md +++ b/docs/src/versions.md @@ -4,12 +4,14 @@ (In development) * Fix error in `SampledCorrelations` with a coarse ``𝐪``-grid. ([PR - #314](https://github.com/SunnySuite/Sunny.jl/pull/314)) + #314](https://github.com/SunnySuite/Sunny.jl/pull/314)). +* Fix colorbar in `plot_intensities!` when all data is uniform ([PR + #315](https://github.com/SunnySuite/Sunny.jl/pull/315)). + ## v0.7.1 (Sep 3, 2024) -* Fix crash in `plot_intensities!` when all data is uniform. * Correctness fix for scalar biquadratic interactions specified with option `biquad` to [`set_exchange!`](@ref). * Prototype implementation of entangled units. From 97e90b3e797d8b308c281695db961a4bea64be42 Mon Sep 17 00:00:00 2001 From: Kipton Barros Date: Sun, 8 Sep 2024 09:24:13 -0400 Subject: [PATCH 3/3] Formatting --- docs/src/versions.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/src/versions.md b/docs/src/versions.md index 443aea1cf..6cb6d74c2 100644 --- a/docs/src/versions.md +++ b/docs/src/versions.md @@ -8,7 +8,6 @@ * Fix colorbar in `plot_intensities!` when all data is uniform ([PR #315](https://github.com/SunnySuite/Sunny.jl/pull/315)). - ## v0.7.1 (Sep 3, 2024)