Skip to content

Figure.histogram: Add the parameter 'weights' for weighting data - #4890

Merged
seisman merged 9 commits into
mainfrom
histogram/weights
Oct 8, 2026
Merged

seisman merged 9 commits into
mainfrom
histogram/weights

Conversation

@seisman

@seisman seisman commented Sep 15, 2026

Copy link
Copy Markdown
Member

In histogram, -Z+w means using the second data column as weights. This PR provides the weights parameter which is more Pythonic. It can cover three use cases:

  1. data is a file: weights=True means using the 2nd column as weights
  2. data is a 2-D array: weights=True means using the 2nd column as weights
  3. data is a 1-D array: weights must be a 1-D array as weights

For comparison, matplotlib.pyplot.hist also provides the weights parameter.

Here is an example showing that PyGMT and matplotlib produce the same plot:

data = [1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 4, 5, 6, 7, 8, 8, 8, 8, 8, 8]
weights = [0.1, 0.1, 0.2, 0.2, 0.3, 0.3, 0.4, 0.4, 0.5, 0.5, 0.6, 0.6, 0.7, 0.7, 0.8, 0.8, 0.9, 0.9, 1.0, 1.0]

import matplotlib.pyplot as plt
import numpy as np

plt.hist(data, weights=weights, bins=np.arange(0, 10, 1))
plt.show()


import pygmt
fig = pygmt.Figure()
fig.histogram(data=data, weights=weights, series=1, fill="lightblue", frame=True, region=[0, 10, 0, 0])
fig.show()

@seisman seisman added this to the 0.20.0 milestone Sep 15, 2026
@seisman seisman added enhancement Improving an existing feature needs review This PR has higher priority and needs review. labels Sep 15, 2026
@seisman
seisman requested a lite review from Copilot September 15, 2026 12:37

This comment was marked as low quality.

Comment thread pygmt/tests/test_histogram.py Outdated
@seisman seisman added final review call This PR requires final review and approval from a second reviewer and removed needs review This PR has higher priority and needs review. labels Oct 7, 2026
@seisman
seisman requested a review from a team October 7, 2026 13:02
Comment thread pygmt/src/histogram.py Outdated
Comment thread pygmt/src/histogram.py
Comment on lines 255 to 256
- W = pen
- c = panel

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.

Wondering if we need to add -Z and +w to this list:

Suggested change
- W = pen
- Z = histtype, **+w**: weights
- c = panel

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yes but will add it in #4912

seisman and others added 2 commits October 8, 2026 20:18
Co-authored-by: Yvonne Fröhlich <94163266+yvonnefroehlich@users.noreply.github.com>
Comment thread pygmt/src/histogram.py Outdated
Comment thread pygmt/src/histogram.py Outdated
Comment thread pygmt/src/histogram.py Outdated
seisman and others added 2 commits October 8, 2026 20:56
Co-authored-by: Yvonne Fröhlich <94163266+yvonnefroehlich@users.noreply.github.com>
Comment thread pygmt/src/histogram.py Outdated
Co-authored-by: Yvonne Fröhlich <94163266+yvonnefroehlich@users.noreply.github.com>
@seisman
seisman merged commit ebc2c8b into main Oct 8, 2026
21 of 22 checks passed
@seisman
seisman deleted the histogram/weights branch October 8, 2026 16:21
@seisman seisman removed the final review call This PR requires final review and approval from a second reviewer label Oct 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Improving an existing feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants