Skip to content

Conversation

yvonnefroehlich
Copy link
Member

@yvonnefroehlich yvonnefroehlich commented Sep 29, 2025

Description of proposed changes

There are several reports and posts on GitHub and the GMT forum regarding the automatic scaling of the fontsize of the colorbar label up on GMT 6.5. Maybe adding a note to the documentation for Figure.colorbar makes this change more visible to users.

GMT docs dev: https://docs.generic-mapping-tools.org/dev/colorbar.html#description

Preview: https://pygmt-dev--4126.org.readthedocs.build/en/4126/api/generated/pygmt.Figure.colorbar.html

Reminders

  • Run make format and make check to make sure the code follows the style guide.
  • Add tests for new features or tests that would have caught the bug that you're fixing.
  • Add new public functions/methods/classes to doc/api/index.rst.
  • Write detailed docstrings for all functions/methods.
  • If wrapping a new module, open a 'Wrap new GMT module' issue and submit reasonably-sized PRs.
  • If adding new functionality, add an example to docstrings or tutorials.

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

Address #3041

@yvonnefroehlich yvonnefroehlich added this to the 0.17.0 milestone Sep 29, 2025
@yvonnefroehlich yvonnefroehlich self-assigned this Sep 29, 2025
@yvonnefroehlich yvonnefroehlich added the documentation Improvements or additions to documentation label Sep 29, 2025
@yvonnefroehlich yvonnefroehlich marked this pull request as draft September 29, 2025 14:20
@yvonnefroehlich yvonnefroehlich marked this pull request as ready for review September 29, 2025 18:26
@yvonnefroehlich yvonnefroehlich added the needs review This PR has higher priority and needs review. label Sep 30, 2025
@yvonnefroehlich yvonnefroehlich mentioned this pull request Sep 30, 2025
42 tasks
Comment on lines +54 to +56
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?

Co-authored-by: Wei Ji <[email protected]>
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.

2 participants