Skip to content

histogram: Fix bug with the -N normal curve for reverse cumulative histograms (-Qr) - #9218

Merged
Esteban82 merged 1 commit into
masterfrom
fix-histogram-qr-normal-curve
Sep 21, 2026
Merged

Esteban82 merged 1 commit into
masterfrom
fix-histogram-qr-normal-curve

Conversation

@Esteban82

Copy link
Copy Markdown
Member

This fails:

gmt histogram @v3206_06.txt -T-7000/0/250 -Z0 -JX12c/8c -Baf -Ggray -W0.5p -N0+p2p,red -Qr -png lixo

Output with bug:

lixo_bug

Output with this fix:

lixo_fix

Done with Claude Sonnet 5 and review by Opus 5.

This PR also adds a test asserting that the reported area equals the number of input points for both -Q and -Qr; it fails on the previous code for -Qr.

Two separate mistakes made the -N overlay unusable together with -Qr,
while -Q was fine:

- pshistogram_plot_boxes derived "area" (the grand total, used to scale
  the -N curve) by picking up the last bin, on the grounds that it "has
  the entire sum". That holds for -Q, where the bars count up, but under
  -Qr they count down and the last bin is ~0. On a 6521-point sample the
  reported area came out as 21 instead of 6521, so the curve was drawn
  roughly 300x too small -- flat against the axis. With -S (stairs) the
  last bin is exactly 0, so the curve vanished altogether. Use F->sum_w,
  which is the same value the last bin gives under -Q and is correct
  either way, and hoist it out of the bar loop since it no longer
  depends on the bin.

- The reverse curve was computed as f - yp, but f is only half the grand
  total (it is set to 0.5 and later multiplied by area), while the
  forward curve yp spans 0 to 2f. Subtracting f therefore started the
  curve at half height and drove it negative past the mean instead of
  running from the total down to zero. Use 2f - yp.

With both fixed, the -Qr curve is the mirror image of the -Q one and
tracks the bars, for every -Z mode and not just counts.

Adds a test asserting that the reported area equals the number of input
points for both -Q and -Qr; it fails on the previous code for -Qr.

No baseline image changes: no existing test plots -Qr, and the -Q value
is unchanged, so all eight pshistogram baselines still compare at RMS
0.0000, including distributions.sh which combines -Q with -N.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@Esteban82 Esteban82 added add-changelog Add PR to the changelog bug Something isn't working labels Sep 21, 2026

@joa-quim joa-quim left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

@Esteban82
Esteban82 merged commit 4935487 into master Sep 21, 2026
17 of 20 checks passed
@Esteban82
Esteban82 deleted the fix-histogram-qr-normal-curve branch September 21, 2026 19:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

add-changelog Add PR to the changelog bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants