Skip to content

WIP: Improve and extend tutorial "Carthesian histograms" - #4988

Draft
yvonnefroehlich wants to merge 49 commits into
mainfrom
update-tut-histo
Draft

yvonnefroehlich wants to merge 49 commits into
mainfrom
update-tut-histo

Conversation

@yvonnefroehlich

@yvonnefroehlich yvonnefroehlich commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Description of proposed changes

This PR aims to improve the tutorial "Carthesian histograms" based on the aspects mentioned at #4905 (comment).

The new structure consistes of four main sections:

  • General setup: bin width via series and bar fill via fill and cmap
  • Histogram styles: bar orientation, stair-steps
  • Histogram types: count and frequency percent, cumulative values, distribution curve
  • Multi-dataset histograms: grouped, stacked, overlayed bars

As this tutorial is getting quite long we may want to think about splitting it into different smaller tutorials.

Fixes #4905
Patches #4965

Preview: https://pygmt-dev--4988.org.readthedocs.build/en/4988/tutorials/advanced/cartesian_histograms.html

Guidelines

Slash Commands

You can write slash commands (/command) in the first line of a comment to perform
specific operations. Supported slash command is:

  • /format: automatically format and lint the code

@yvonnefroehlich yvonnefroehlich added this to the 0.20.0 milestone Nov 21, 2026
@yvonnefroehlich yvonnefroehlich self-assigned this Nov 21, 2026
@yvonnefroehlich yvonnefroehlich added the documentation Improvements or additions to documentation label Nov 21, 2026
@yvonnefroehlich
yvonnefroehlich marked this pull request as draft November 21, 2026 15:38
@yvonnefroehlich

yvonnefroehlich commented Nov 21, 2026 •

Copy link
Copy Markdown
Member Author

I am confused about the behaviour the series parameter to set the bin width and wondering if it works correctly:

  • left: series=10
  • middle: series=[75, 120, 10]
    Why do the bars not begin at 75 and end at 120? The plot looks idential to the left one.
  • right: np.array([50, 75, 80, 90, 100, 110, 120, 140, 180])
image
import numpy as np
import pygmt
from pygmt.params import Axis, Frame


rng = np.random.default_rng(seed=100)
mean = 100
stddev = 20
data1 = rng.normal(loc=mean, scale=stddev, size=42)

args_histo = {
    "region": [0, 200, 0, 0],
    "projection": "X10c",
    "frame": Frame(
        axes="WStr",
        xaxis=Axis(annot=True, tick=10),
        yaxis=Axis(annot=1, tick=1, label="Counts"),
    ),
    "data": data1,
    "fill": "lightbrown",
    "pen": "0.5p,gray30",
}


fig = pygmt.Figure()

fig.histogram(**args_histo, series=10)

fig.shift_origin(xshift="w+2c")

fig.histogram(**args_histo, series=[75, 120, 10])

fig.shift_origin(xshift="w+2c")

fig.histogram(**args_histo, series=np.array([50, 75, 80, 90, 100, 110, 120, 140, 180]))

fig.show()

@yvonnefroehlich

yvonnefroehlich commented Nov 21, 2026 •

Copy link
Copy Markdown
Member Author

For me, using a colormap to fill the bars based on the position (mid-coordinate of the bin) or the bin count (or frequence) does not work by passing the colormap directly to the cmap parameter of Figure.histogram (first row), but I need to set up the colormap via pygmt.makecpt (second row) and use cmap=True.

Edit 2026/11/22: Issue report upstream GMT at GenericMappingTools/gmt#9314

image image
import numpy as np
import pygmt
from pygmt.params import Axis, Frame


rng = np.random.default_rng(seed=100)
mean = 100
stddev = 20
data1 = rng.normal(loc=mean, scale=stddev, size=42)

args_histo = {
    "region": [0, 200, 0, 0],
    "projection": "X10c",
    "frame": Frame(
        axes="WStr",
        xaxis=Axis(annot=True, tick=10),
        yaxis=Axis(annot=1, tick=1, label="Counts"),
    ),
    "data": data1,
    "pen": "0.5p,gray30",
    "series": 10,
}


fig = pygmt.Figure()

fig.histogram(**args_histo, fill="lightbrown")

fig.shift_origin(xshift="w+2c")

fig.histogram(**args_histo, fill="position", cmap="SCM/batlow")

fig.shift_origin(xshift="w+2c")

fig.histogram(**args_histo, fill="value", cmap="SCM/batlow")

fig.show()


fig = pygmt.Figure()

fig.histogram(**args_histo, fill="lightbrown")

fig.shift_origin(xshift="w+2c")

pygmt.makecpt(cmap="SCM/batlow", series=[0, 200, 10])
fig.colorbar(frame=True)
fig.histogram(**args_histo, fill="position", cmap=True)

fig.shift_origin(xshift="w+2c")

pygmt.makecpt(cmap="SCM/batlow", series=[0, 15, 1])
fig.colorbar(frame=True)
fig.histogram(**args_histo, fill="value", cmap=True)

fig.show()

@yvonnefroehlich yvonnefroehlich mentioned this pull request Nov 22, 2026
13 of 38 tasks
@yvonnefroehlich

yvonnefroehlich commented Nov 23, 2026 •

Copy link
Copy Markdown
Member Author

Before putting more efforts into polishing all the explanations and comments, I like to ask for comments and reviews on the general structure (and the length) of the tutorial.

The new structure is currently:

  • General setup
    • Bin width: series parameter
    • Bar fill: fill and cmap parameters
  • Histogram styles
    • Bar orientation
    • Stair-steps
  • Histogram types
    • Counts and frequency percent
    • Cumulative values
    • Distribution curve
  • Multi-dataset histograms
    • Grouped bars
    • Stacked bars
    • Overlayed bars

@yvonnefroehlich yvonnefroehlich added the needs review This PR has higher priority and needs review. label Nov 23, 2026
@seisman

seisman commented Nov 24, 2026

Copy link
Copy Markdown
Member

I am confused about the behaviour the series parameter to set the bin width and wondering if it works correctly:

  • left: series=10
  • middle: series=[75, 120, 10]
    Why do the bars not begin at 75 and end at 120? The plot looks idential to the left one.
  • right: np.array([50, 75, 80, 90, 100, 110, 120, 140, 180])
image import numpy as np import pygmt from pygmt.params import Axis, Frame

rng = np.random.default_rng(seed=100)
mean = 100
stddev = 20
data1 = rng.normal(loc=mean, scale=stddev, size=42)

args_histo = {
"region": [0, 200, 0, 0],
"projection": "X10c",
"frame": Frame(
axes="WStr",
xaxis=Axis(annot=True, tick=10),
yaxis=Axis(annot=1, tick=1, label="Counts"),
),
"data": data1,
"fill": "lightbrown",
"pen": "0.5p,gray30",
}

fig = pygmt.Figure()

fig.histogram(**args_histo, series=10)

fig.shift_origin(xshift="w+2c")

fig.histogram(**args_histo, series=[75, 120, 10])

fig.shift_origin(xshift="w+2c")

fig.histogram(**args_histo, series=np.array([50, 75, 80, 90, 100, 110, 120, 140, 180]))

fig.show()

the x-range is set by the region parameter

I am confused about the behaviour the series parameter to set the bin width and wondering if it works correctly:

  • left: series=10
  • middle: series=[75, 120, 10]
    Why do the bars not begin at 75 and end at 120? The plot looks idential to the left one.
  • right: np.array([50, 75, 80, 90, 100, 110, 120, 140, 180])
image import numpy as np import pygmt from pygmt.params import Axis, Frame

rng = np.random.default_rng(seed=100)
mean = 100
stddev = 20
data1 = rng.normal(loc=mean, scale=stddev, size=42)

args_histo = {
"region": [0, 200, 0, 0],
"projection": "X10c",
"frame": Frame(
axes="WStr",
xaxis=Axis(annot=True, tick=10),
yaxis=Axis(annot=1, tick=1, label="Counts"),
),
"data": data1,
"fill": "lightbrown",
"pen": "0.5p,gray30",
}

fig = pygmt.Figure()

fig.histogram(**args_histo, series=10)

fig.shift_origin(xshift="w+2c")

fig.histogram(**args_histo, series=[75, 120, 10])

fig.shift_origin(xshift="w+2c")

fig.histogram(**args_histo, series=np.array([50, 75, 80, 90, 100, 110, 120, 140, 180]))

fig.show()

this is expected. the x range is controlled by the region parameter

@yvonnefroehlich

Copy link
Copy Markdown
Member Author

I am confused about the behaviour the series parameter to set the bin width and wondering if it works correctly:

  • left: series=10
  • middle: series=[75, 120, 10]
    Why do the bars not begin at 75 and end at 120? The plot looks idential to the left one.
  • right: np.array([50, 75, 80, 90, 100, 110, 120, 140, 180])

the x-range is set by the region parameter

this is expected. the x range is controlled by the region parameter

Yes, the x-range of the plot, but regarding the bins the docs says:

image

For me this sounds like that for passing a list of three values, the minimum and maximum for the bins are taken from that list. But looking at the second plot above, the bars still start at 60, not at 75, and end at 150, not at 120. When using a sequence of any other length than three, the start and end of the bins are adjusted based on the first and last values of this sequence (third plot).

This branch has not been deployed

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

Labels

documentation Improvements or additions to documentation needs review This PR has higher priority and needs review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve the tutorial for Cartesian Histograms

2 participants