Skip to content
Merged
Changes from 15 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
13590f6
Add note regarding automatic fontsize scaling
yvonnefroehlich Sep 29, 2025
0c55a48
Add more information
yvonnefroehlich Sep 29, 2025
1fded9b
Use math mode
yvonnefroehlich Sep 29, 2025
f56ae84
Fix math synthax
yvonnefroehlich Sep 29, 2025
0e27f13
Remove underscore
yvonnefroehlich Sep 29, 2025
2e29446
Merge branch 'main' into add-note-fontsize-cb
yvonnefroehlich Sep 29, 2025
693be21
Add more information
yvonnefroehlich Sep 29, 2025
4e05d24
Merge remote-tracking branch 'origin/add-note-fontsize-cb' into add-n…
yvonnefroehlich Sep 29, 2025
8248cda
Merge branch 'main' into add-note-fontsize-cb
yvonnefroehlich Sep 29, 2025
c4066b8
Merge branch 'main' into add-note-fontsize-cb
yvonnefroehlich Sep 30, 2025
c9ea951
Merge branch 'main' into add-note-fontsize-cb
yvonnefroehlich Sep 30, 2025
19c3046
Merge branch 'main' into add-note-fontsize-cb
yvonnefroehlich Sep 30, 2025
b3c6ada
TEST: fix docs warning regarding pattern class
yvonnefroehlich Sep 30, 2025
35dad14
TEST: Use 'Parameters'
yvonnefroehlich Sep 30, 2025
7f05b32
Revert change
yvonnefroehlich Sep 30, 2025
d6bef9a
Improve formulation
yvonnefroehlich Sep 30, 2025
f514e26
Fix line length
yvonnefroehlich Sep 30, 2025
870be82
Improve formulation to account for all fontsizes and provide workaround
yvonnefroehlich Oct 1, 2025
3bdf1fd
Fix hilighting for code
yvonnefroehlich Oct 1, 2025
59c9518
Merge branch 'main' into add-note-fontsize-cb
yvonnefroehlich Oct 1, 2025
513840a
Improve formulation and grammar
yvonnefroehlich Oct 1, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pygmt/src/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ def colorbar(
linear scale, all be equal size, or by providing a file with individual
tile widths.

.. note::
Up on GMT 6.5.0, the fontsize of the colorbar label is scaled based on the
width of the colorbar (:math:`\sqrt{{colorbarwidth / 15}}`). Currently, in
modern mode, changing this fontsize via adjusting the GMT default parameter
:gmt-term:`FONT_LABEL` does not work as expected.
Copy link
Member

@weiji14 weiji14 Sep 30, 2025

Choose a reason for hiding this comment

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

Saw your post at https://forum.generic-mapping-tools.org/t/longitude-axis-starts-at-0-instead-of-my-region-bounds-when-using-mercator-projection-in-pygmt/6214/4, and the link to https://docs.generic-mapping-tools.org/dev/colorbar.html#description which suggests using --FONT_ANNOT_PRIMARY=....

Based on your example at #3041 (comment), is the recommendation to use:

fig.basemap(...)
with pygmt.config(FONT=...):
    fig.colorbar(cmap="...", ...)

Or can we maybe suggest using FONT_ANNOT_PRIMARY? I think the key is to:

  1. Only wrap the with pygmt.config(FONT_ANNOT_PRIMARY=...) statement around fig.colorbar, but keep the basemap and other fig.* calls outside.
  2. Apply the scaling factor using your scale_cb_font function in Colorbar annotation size does not change #3041 (comment)

Copy link
Member Author

@yvonnefroehlich yvonnefroehlich Sep 30, 2025

Choose a reason for hiding this comment

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

Hm. I think it's FONT_LABEL for the x-axis label. because as far as I understood it:

  • FONT_ANNOT_PRIMARY: fontsize of the x-axis annotations (numbers)
  • FONT_ANNOT_SECONDARY: fontsize of the y-axis label (-> unit)
  • FONT_LABEL: fontsize of the x-axis label (-> quantity)
  • FONT: fontsize of all

For me, FONT_ANNOT_PRIMARY has no effect on the x-axis label:

import pygmt

size = 5
args_bmap = {"region": [-1, 1, -size, size], "projection": f"X4c/{size}c", "frame": 1}
args_cb = {"cmap": "batlow", "position": "jMC", "frame": ["x+lquantity", "y+lunit"]}

fig = pygmt.Figure()

# -----------------------------------------------------------------------------
fig.basemap(**args_bmap)

fig.colorbar(**args_cb)

# -----------------------------------------------------------------------------
fig.shift_origin(xshift="+w+1c")
fig.basemap(**args_bmap)

with pygmt.config(
    FONT_ANNOT_PRIMARY="10p,blue",  # x-axis annotations (numbers)
    FONT_ANNOT_SECONDARY="20p,darkgreen",  # y-axis label (-> unit)
    FONT_LABEL="20p,orange",  # x-axis label (-> quantity)
):
    fig.colorbar(**args_cb)

# -----------------------------------------------------------------------------
fig.shift_origin(xshift="+w+1c")
fig.basemap(**args_bmap)

with pygmt.config(
    FONT_ANNOT_PRIMARY="25p,blue", 
    FONT_ANNOT_SECONDARY="20p,darkgreen",
    FONT_LABEL="20p,orange",
):
    fig.colorbar(**args_cb)

# -----------------------------------------------------------------------------
fig.shift_origin(xshift="+w+1c")
fig.basemap(**args_bmap)

with pygmt.config(
    FONT_ANNOT_PRIMARY="25p,blue", 
    FONT_ANNOT_SECONDARY="20p,darkgreen",
    # FONT_LABEL="20p,orange",
):
    fig.colorbar(**args_cb)

fig.show()
colorbar_width_label_fontsize

I am fine with including an explicit workaround. Yes, the user has to adjust the desired font size based on the scale factor (previously calculated using the colorbar width) and pass this value to FONT_LABEL (or FONT). The automatic font size scaling reverts this adjustment, and the desired font size is applied. This has to be done locally (with config(...): ) to avoid affecting the font sizes related to the figure frame (basemap) because here no automatic font size scaling is applied.

Copy link
Member

Choose a reason for hiding this comment

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

From Leo's code at #3041 (comment), it seems like it is not just the colorbar label (set by FONT_LABEL) which is affected by the scaling factor, but also the x- and y- axis annotations (set by FONT_ANNOT_PRIMARY and FONT_ANNOT_SECONDARY respectively). So probably should mention all of these. Could you update the text to reflect that both the label and annotations are affected?

Copy link
Member Author

@yvonnefroehlich yvonnefroehlich Oct 1, 2025

Choose a reason for hiding this comment

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

Yes, the fontsizes of x-label, x-axis annotations and y-label are affect, and it looks like they all use the same scaling, see code and figure below. I have extended the text in 870be82.


import pygmt
import numpy as np

# -----------------------------------------------------------------------------
def scale_cb_font(cb_width):
    scale_factor = 1 / 15  # scale factor used in GMT 6.5
    font_scaling = np.sqrt(cb_width * scale_factor)
    return font_scaling
# -----------------------------------------------------------------------------

font_size = 18

plot_size = 5
args_bmap = {
    "region": [-1, 1, -plot_size, plot_size], "projection": f"X4c/{plot_size}c", "frame": 1}

cb_width = 4
args_cb = {
    "cmap": "batlow", "position": f"jMC+w{cb_width}c", "frame": ["x+lquantity", "y+lunit"]
}
font_scaling = scale_cb_font(cb_width)

# -----------------------------------------------------------------------------
fig = pygmt.Figure()

# Left
fig.basemap(**args_bmap)

fig.colorbar(**args_cb)

# Middle
fig.shift_origin(xshift="+w+1c")

with pygmt.config(
    FONT_ANNOT_PRIMARY=f"{font_size}p",  # x-axis annotations (-> numbers)
    FONT_ANNOT_SECONDARY=f"{font_size}p",  # y-axis label (-> unit)
    FONT_LABEL=f"{font_size}p",  # x-axis label (-> quantity)
    # FONT=f"{font_size}p"  # all
):
    fig.basemap(**args_bmap)

    fig.colorbar(**args_cb)

# Right
fig.shift_origin(xshift="+w+1c")

with pygmt.config(FONT=font_size):
    fig.basemap(**args_bmap)

with pygmt.config(
    FONT_ANNOT_PRIMARY=f"{font_size / font_scaling}p",
    FONT_ANNOT_SECONDARY=f"{font_size / font_scaling}p",
    FONT_LABEL=f"{font_size / font_scaling}p",
):
    fig.colorbar(**args_cb)

fig.show()
cb_font_scaling_02


Full GMT docs at :gmt-docs:`colorbar.html`.

{aliases}
Expand Down
Loading