Skip to content

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

Open
seisman wants to merge 1 commit into
mainfrom
histogram/weights
Open

seisman wants to merge 1 commit 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Improving an existing feature needs review This PR has higher priority and needs review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants