Skip to content

Figure.histogram: Improved syntax for filling bars by position/value - #4886

Open
seisman wants to merge 3 commits into
mainfrom
histogram/fillby
Open

Figure.histogram: Improved syntax for filling bars by position/value#4886
seisman wants to merge 3 commits into
mainfrom
histogram/fillby

Conversation

@seisman

@seisman seisman commented Sep 9, 2026

Copy link
Copy Markdown
Member

In histogram, bars can be filled by a constant color/pattern, or by the bin position or value (count, frequency). This is achieved by the -C option, with a syntax like -C<cmap>+b.

This PR implements a Pythonic syntax for such features by extending the existing parameter fill (alias of -G). In addition to color or pattern, now fill can accept two special values: "position" and "value".

GMT CLI and PyGMT comparison

Here is a comparison:

GMT CLI PyGMT Notes
-Gred fill="red" Fill by a constant color
-Gp5 fill=Pattern(5) Fill by a pattern
-C fill="position" Fill based on the current CPT and the mid-coordinate of each bar
-Ctest.cpt fill="position", cmap="test.cpt" Same as above but use test.cpt
-C+b fill="value" Fill based on the current CPT and the bin value (count or frequency, i.e., the bin height)
-Ctest.cpt+b fill="value", cmap="test.cpt" Same as above but use test.cpt

Examples

import numpy as np
import pygmt
from pygmt.params import Frame, Pattern

rng = np.random.default_rng(seed=100)
mean = 100  # mean of distribution
stddev = 25  # standard deviation of distribution
data = rng.normal(loc=mean, scale=stddev, size=521)

fig = pygmt.Figure()
fig.histogram(data=data, frame=Frame(axes="WSen", title="Fill by color"), series=10, fill="green")

fig.shift_origin(xshift="w+1")
fig.histogram(data=data, frame=Frame(axes="WSen", title="Fill by pattern"), series=10, fill=Pattern(10, dpi=50))

fig.shift_origin(xshift="w+1")
pygmt.makecpt(cmap="turbo", series=(0, 200, 25))
fig.histogram(data=data, frame=Frame(axes="WSen", title="Fill by position"), series=10, pen="1p", fill="position")
fig.colorbar()

fig.shift_origin(xshift="w+4")
pygmt.makecpt(cmap="turbo", series=(0, 100, 25))
fig.histogram(data=data, frame=Frame(axes="WSen", title="Fill by value"), series=10, pen="1p", fill="value")
fig.colorbar()

fig.show()
histogram

Alternative implementation

An alternative implementation is to keep fill unchanged, but alias -C to two separate parameters, cmap and fill_by_value (or any other names). The cons are:

  • User may be confused by the two similar parameters fill and fill_by_value
  • -G can't be used with -C, so we need to deal with conflicts between fill and cmap/fill_by_value carefully

Address #4251

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The updated fill docstring currently contradicts the implemented/tested behavior for CPT-based fills and should be corrected before approval.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR updates Figure.histogram to support a more Pythonic way to request CPT-based bar filling by extending the existing fill parameter to accept special values ("position" and "value"), while keeping the legacy cmap (-C) syntax working.

Changes:

  • Extend histogram(fill=...) to accept "position"/"value" and translate them into the appropriate -C / -C+b CLI behavior.
  • Add image-based regression tests for the new fill syntax and the legacy cmap syntax.
  • Add a new baseline image for the histogram fill behavior.
File summaries
File Description
pygmt/src/histogram.py Implements the new fill="position"/fill="value" syntax and maps it onto -C / -C+b.
pygmt/tests/test_histogram.py Adds pytest-mpl image tests covering both new and legacy CPT fill syntaxes, plus an error case.
pygmt/tests/baseline/test_histogram_fill.png Adds the new baseline image used by the pytest-mpl tests.
Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread pygmt/src/histogram.py Outdated
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