Yes, I think the difference is that it works as expected when using fig.shift_origin()
but not when using fig.subplot()
.
I believe that’s one of the most non-intuitive parts for PyGMT!
The example below is modified from @maxrjones’s example, without writing the CPT to a file. As mentioned by @maxrjones, it doesn’t work as expected. The two colorbars are the same.
### WRONG!!!!
import pygmt
subset_region=[-131.5, -128.5, 45, 47]
earth_relief_globe = pygmt.datasets.load_earth_relief(resolution="01d")
earth_relief_subset = pygmt.datasets.load_earth_relief("15s", region=subset_region)
fig = pygmt.Figure()
with fig.subplot(nrows=1, ncols=2, figsize=("15c", "15c"), margins="1c"):
pygmt.grd2cpt(cmap="inferno", grid=earth_relief_globe, continuous=True)
fig.grdimage(grid=earth_relief_globe, projection="R", region="g", frame=True, panel=0)
fig.colorbar(frame=["a", "x+lElevation", "y+lm"])
pygmt.grd2cpt(cmap="inferno",grid=earth_relief_subset, continuous=True)
fig.grdimage(grid=earth_relief_subset, projection="R", region=subset_region, frame=True, panel=1)
fig.colorbar(frame=["a", "x+lElevation", "y+lm"])
fig.show()
Here is the output:
To plot the correct figure using fig.subplot()
, you need to “activate” the panel (using fig.set_panel()
) before creating the CPT. Here is the modified script:
### CORRECT!!!
import pygmt
subset_region=[-131.5, -128.5, 45, 47]
earth_relief_globe = pygmt.datasets.load_earth_relief(resolution="01d")
earth_relief_subset = pygmt.datasets.load_earth_relief("15s", region=subset_region)
fig = pygmt.Figure()
with fig.subplot(nrows=1, ncols=2, figsize=("15c", "15c"), margins="1c"):
with fig.set_panel(panel=0):
pygmt.grd2cpt(cmap="inferno", grid=earth_relief_globe, continuous=True)
fig.grdimage(grid=earth_relief_globe, projection="R", region="g", frame=True)
fig.colorbar(frame=["a", "x+lElevation", "y+lm"])
with fig.set_panel(panel=1):
pygmt.grd2cpt(cmap="inferno",grid=earth_relief_subset, continuous=True)
fig.grdimage(grid=earth_relief_subset, projection="R", region=subset_region, frame=True)
fig.colorbar(frame=["a", "x+lElevation", "y+lm"])
fig.show()
Here is the output:
To understand why these two are different, you need to know more about GMT’s Hierarchical Levels. In short, “a subplot” and a “panel” can have their own “current” CPT file. When there is no panel-level CPT file, the subplot-level CPT file will be used.
In the “wrong” example:
- the first
grd2cpt
function creates the “current” CPT for the current subplot, and it’s used by the first grdimage
function in the first panel
- the 2nd
grd2cpt
creates a CPT file for the first panel (not the 2nd panel, because the 1st panel is currently activated!) Also, note that this CPT file is never used, as explained below.
- the 2nd
grdimage
is called in the 2nd panel. Because there is no panel-level CPT file for the 2nd panel, so the subplot-level CPT is used.
Thus both panels use the “subplot-level” CPT (created by the first grd2cpt
call) and the colorbars are the same.
In the correct example, we first activate the panel using fig.set_panel()
function, so grd2cpt
creates panel-level CPT files for each panel. That’s why it works.