Skip to content

Colorbar positioning is problematic in subplot mode #2426

Closed

Description

@seisman

Initially found in #2354 (comment).

Here is a minimal example to reproduce the issue:

import pygmt
fig = pygmt.Figure()
with fig.subplot(nrows=1, ncols=2, figsize=(10, 5)):
    fig.basemap(region=[0, 10, 0, 10], projection="X?", frame=True, panel=True)
    fig.basemap(region=[0, 10, 0, 10], projection="X?", frame=True, panel=True)
fig.colorbar(position="JBC+w5c+h", cmap="turbo", frame=True)
fig.show()

The "equivalent" GMT bash script is:

gmt begin
    gmt figure map
	gmt subplot begin 1x2 -Ff10c/5c
		gmt basemap -R0/10/0/10 -JX? -B -c
		gmt basemap -R0/10/0/10 -JX? -B -c
	gmt subplot end
	gmt colorbar -DJBC+w5c+h -Cturbo -B
gmt end show

PyGMT and GMT CLI produce different outputs, and the GMT CLI one is correct:

PyGMT GMT CLI
pygmt-colorbar bash-colorbar

Activity

  1. added
    bug
    on Mar 15, 2023
  2. seisman commented on Mar 15, 2023

    @seisman
    MemberAuthor

    After adding verbose="d" to the Figure.colobar() call in the PyGMT script, I see debug messages like:

    pygmt-session [DEBUG]: Use PS filename /Users/seisman/.gmt/sessions/gmt_session.6653/gmt_1.ps-
    pygmt-session [DEBUG]: gmtinit_get_current_panel: No current panel selected so not in subplot mode
    pygmt-session [DEBUG]: Modern mode: Added -R to options since history is available [0/3.93701/0/1.9685].
    pygmt-session [DEBUG]: Modern: Adding -JX15c/15c to options since there is no history available.
    pygmt-session [DEBUG]: Revised options: -B -Cturbo -DJBC+w5c+h -Vd -R -JX15c/15c
    colorbar [DEBUG]: History: Process -R
    colorbar [DEBUG]: History: Process -JX15c/15c
    

    For comparison, adding -Vd to the colorbar command in the bash script gives:

    gmt [DEBUG]: Use PS filename /Users/seisman/.gmt/sessions/gmt_session.6943/gmt_0.ps-
    gmt [DEBUG]: gmtinit_get_current_panel: No current panel selected so not in subplot mode
    gmt [DEBUG]: Modern mode: Added -R to options since history is available [0/3.93701/0/1.9685].
    gmt [DEBUG]: Modern: Adding -J to options since history is available [x1i].
    gmt [DEBUG]: Revised options: -DJBC+w5c+h -Cturbo -B -Vd -R -J
    colorbar [DEBUG]: History: Process -R
    colorbar [DEBUG]: History: Process -J
    

    So, for unknown reasons, PyGMT fails to get the history information for -J option and use the default -JX15c/15c instead, which explains the incorrect positioning of colorbars.

  3. seisman commented on Mar 15, 2023

    @seisman
    MemberAuthor

    Ping @PaulWessel. Need your help here.

  4. PaulWessel commented on Mar 15, 2023

    @PaulWessel

    Hm, got to be something different in the wrapper when it calls GMT compared to the CLI. I suggest you first do this

    1. gmt clear sessions
    2. Run each of the CLI commands and examine what is created in ~/.gmt/sessions/gmt_session.* Take a note of files being created and destroyed after each command
    3. Repeat 1
    4. Do the same on command at the time via PyGMT and compare to see what and when things go wrong.
  5. seisman commented on Mar 15, 2023

    @seisman
    MemberAuthor

    Hm, got to be something different in the wrapper when it calls GMT compared to the CLI. I suggest you first do this

    1. gmt clear sessions
    2. Run each of the CLI commands and examine what is created in ~/.gmt/sessions/gmt_session.* Take a note of files being created and destroyed after each command
    3. Repeat 1
    4. Do the same on command at the time via PyGMT and compare to see what and when things go wrong.

    Thanks. It helps me find the culprit. It's NOT a GMT bug. It's a bug on the PyGMT side.

  6. added this to the 0.9.0 milestone on Mar 15, 2023
  7. tiannh7 commented on Sep 23, 2025

    @tiannh7

    Hi @seisman @PaulWessel

    I encountered a similar problem with subplot:subsize in current pygmt version:

    fig = pygmt.Figure()
    pygmt.config(FONT="10p", FONT_TAG="10p,Helvetica-Bold,black")
    with fig.subplot(
        nrows=1, ncols=2, subsize=("10c", "8c"),
        margins=["0.3c","0.5c"], sharey='l', frame=['WSne', 'xaf', 'yaf']
    ):
        
        max_abs_stress = 100
        pygmt.makecpt(cmap="../cpt/stress_bam.cpt", series=[-max_abs_stress, max_abs_stress, 0.1], 
                      continuous=True, output='../cpt/stress_bam-k2.cpt')
        stress_cpt='../cpt/stress_bam-k2.cpt'
        
        #-----------------------------------------------------------------------#
        with fig.set_panel(panel=0):
            fig.basemap(region=region, projection="Q?")
    
        #-----------------------------------------------------------------------#
        with fig.set_panel(panel=1):
            fig.basemap(region=region, projection="Q?")
    
    colorbar_label = r'stress [MPa]'
    fig.colorbar(cmap=stress_cpt,frame=[f"xaf+l{colorbar_label}", "y"], position="JBC+w10c/0.4c+o0c/0.7c+h+e")
    
    if show_me:
        fig.show(dpi=300)
    Image
    PyGMT information:
      version: v0.16.0
    System information:
      python: 3.13.3 | packaged by conda-forge | (main, Apr 14 2025, 20:44:03) [GCC 13.3.0]
      executable: /share/anaconda3/envs/pygmt/bin/python
      machine: Linux-6.8.0-60-generic-x86_64-with-glibc2.35
    Dependency information:
      numpy: 2.2.6
      pandas: 2.3.0
      xarray: 2025.4.0
      packaging: 25.0
      contextily: None
      geopandas: None
      IPython: 9.3.0
      pyarrow: None
      rioxarray: None
      gdal: None
      ghostscript: 10.04.0
    GMT library information:
      version: 6.5.0
      padding: 2
      share dir: /share/anaconda3/envs/pygmt/share/gmt
      plugin dir: /share/anaconda3/envs/pygmt/lib/gmt/plugins
      library path: /share/anaconda3/envs/pygmt/lib/libgmt.so
      cores: 256
      grid layout: rows
      image layout: 
      binary version: 6.5.0
  8. seisman commented on Sep 23, 2025

    @seisman
    MemberAuthor

    I can't reproduce your issue. Please provide a minimum working example.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions