Skip to content

Conversation

@seisman
Copy link
Member

@seisman seisman commented Dec 2, 2025

This PR improves grdview's -Q option in a more Pythonic way by splitting it into multiple parameters:

  • surftype
  • dpi
  • nan_transparent
  • monochrome
  • mesh_fill

Address #4208 (comment).

Preview at https://pygmt-dev--4234.org.readthedocs.build/en/4234/api/generated/pygmt.Figure.grdview.html

Example

import pygmt

grd_relief = pygmt.datasets.load_earth_relief(resolution="15m", region=[-25, -13, 63.2, 66.7])

args = dict(
    region=[-25, -13, 63.2, 66.7], 
    projection="M?", 
    perspective=(-150, 25),
    cmap="SCM/oleron",
    zsize="1.5c",
    plane="+ggray",
    panel=True,
)

fig = pygmt.Figure()
with fig.subplot(nrows=5, ncols=3, subsize=(5, 5), margins=(1, 0.2)):
    for surftype in ("surface", "mesh", "surface+mesh", "image", "waterfall_x", "waterfall_y"):
        fig.grdview(
            grid=grd_relief,
            frame=["xaf", "yaf", f"WSenZ+t{surftype}"],
            surftype=surftype,
            **args,
        )
    for surftype in ("surface", "mesh", "surface+mesh", "image", "waterfall_x", "waterfall_y"):
        fig.grdview(
            grid=grd_relief,
            frame=["xaf", "yaf", f"WSenZ+t{surftype}"],
            surftype=surftype,
            monochrome=True,
            **args,
        )
    for surtype in ("mesh", "waterfall_x", "waterfall_y"):
        fig.grdview(
            grid=grd_relief,
            frame=["xaf", "yaf", f"WSenZ+t{surftype}"],
            surftype=surftype,
            meshfill="red@50",
            **args,
        )
fig.show()
map

),
Alias(dpi, name="dpi"),
Alias(meshfill, name="meshfill"),
Alias(monochrome, name="monochrome", prefix="+m"),
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't +m be a suffix?

Copy link
Member Author

Choose a reason for hiding this comment

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

The full syntax is -Qc|i|m[x|y]|s[m][color][+m]. It's a suffix for the -Q option, but for monochrome, it's a prefix. Since monochrome is a boolean, monochrome would be +m, so here it makes no difference if +m is a prefix or a suffix.

Copy link
Member

Choose a reason for hiding this comment

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

Ah right, got it now.

Copy link
Member

@weiji14 weiji14 Jan 22, 2026

Choose a reason for hiding this comment

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

Just a note that after the suffix feature is implemented in #4259, this +m does need to be a prefix, otherwise a list of values will be returned, xref #4259 (comment).

Comment on lines 194 to 200
"image": "c" if nan_transparent is True else "i",
"waterfallx": "mx",
"waterfally": "my",
}

# surftype was aliased to Q previously.
_old_surftype_syntax = surftype not in _surtype_mapping and surftype is not None
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a unit test to ensure that backwards compatibility is preserved when a user passes surftype="c", but did not set nan_transparent=True (which is added in this PR). The output grdview plot should still have NaN transparency.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added in aa9eb73.

@seisman seisman removed this from the 0.18.0 milestone Dec 25, 2025
@seisman seisman changed the title Figure.grdview: Pythonic syntax for surftype parameter Figure.grdview: Add parameters surftype/dpi/mesh_fill/nan_transparent/monochrome to control surfure types Jan 11, 2026
@seisman seisman marked this pull request as ready for review January 23, 2026 12:31
@seisman seisman added the needs review This PR has higher priority and needs review. label Jan 23, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors PyGMT’s Figure.grdview -Q option into explicit, Pythonic parameters (surftype, dpi, mesh_fill, nan_transparent, monochrome) and updates tests/examples accordingly.

Changes:

  • Add a dedicated -Q alias builder and expose new grdview parameters to control surface rendering modes.
  • Expand/refresh grdview tests (including image baselines) to cover the new parameterization and validation behavior.
  • Update gallery/tutorial examples to use the new long-form surftype values.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
pygmt/src/grdview.py Introduces _alias_option_Q, adds new grdview parameters, updates docs, and validates parameter combinations.
pygmt/tests/test_grdview.py Adds unit + image tests for new -Q parameterization; updates existing tests to new surftype values; updates validation tests.
pygmt/tests/baseline/test_grdview_with_cmap_for_surface_monochrome_plot.png.dvc Removes baseline for deleted test.
pygmt/tests/baseline/test_grdview_with_cmap_for_perspective_surface_plot.png.dvc Removes baseline for deleted test.
pygmt/tests/baseline/test_grdview_with_cmap_for_image_plot.png.dvc Removes baseline for deleted test.
pygmt/tests/baseline/test_grdview_surftype.png.dvc Adds baseline for new surftype coverage image test.
pygmt/tests/baseline/test_grdview_monochrome.png.dvc Adds baseline for new monochrome image test.
pygmt/tests/baseline/test_grdview_mesh_pen_and_mesh_fill.png.dvc Adds baseline for new mesh pen/fill image test.
pygmt/tests/baseline/test_grdview_image_dpi.png.dvc Adds baseline for new image DPI test.
examples/tutorials/advanced/draping_on_3d_surface.py Updates surftype usage to new long-form values.
examples/tutorials/advanced/3d_perspective_image.py Updates surftype usage to new long-form values and adjusts inline comments.
examples/gallery/3d_plots/grdview_surface.py Updates example code to use surftype="surface".

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines -101 to -108
@pytest.mark.mpl_image_compare
def test_grdview_with_cmap_for_image_plot(xrgrid):
"""
Run grdview by passing in a grid and setting a colormap for producing an image plot.
"""
fig = Figure()
fig.grdview(grid=xrgrid, cmap="SCM/oleron", surftype="i")
return fig
Copy link
Member Author

Choose a reason for hiding this comment

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

Superseded by test_grdview_surftype



@pytest.mark.mpl_image_compare
def test_grdview_with_cmap_for_surface_monochrome_plot(xrgrid):
Copy link
Member Author

Choose a reason for hiding this comment

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

Superseded by test_grdview_monochrome

return fig


@pytest.mark.mpl_image_compare
Copy link
Member Author

Choose a reason for hiding this comment

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

Superseded by test_grdview_surftype

@seisman seisman added final review call This PR requires final review and approval from a second reviewer and removed needs review This PR has higher priority and needs review. labels Jan 26, 2026
@seisman seisman requested a review from a team January 27, 2026 03:44
Comment on lines +150 to +152
- ``"surface"``: surface plot.
- ``"surface+mesh"``: surface plot with mesh lines drawn on top of the surface.
- ``"image"``: image plot.
Copy link
Member

@weiji14 weiji14 Jan 27, 2026

Choose a reason for hiding this comment

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

I've used surftype="sim" before here. It's been a few years so I can't remember, but maybe check if -Qsim is equal to -Qsm, or if we need to add a surface+image+mesh option 😂

Copy link
Member Author

@seisman seisman Jan 28, 2026

Choose a reason for hiding this comment

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

surftype="sim" is identical to surftype="s":

import pygmt

grd_relief = pygmt.datasets.load_earth_relief(
    resolution="15m", region=[-25, -13, 63.2, 66.7]
)

args = dict(
    region=[-25, -13, 63.2, 66.7],
    projection="M10c",
    perspective=(-150, 25),
    cmap="SCM/oleron",
    frame=True,
    zsize="1.5c",
    plane="+ggray",
)

fig = pygmt.Figure()
fig.grdview(grid=grd_relief, surftype="sim", **args)
fig.shift_origin(xshift="w+2c")
fig.grdview(grid=grd_relief, surftype="surface", **args)
fig.show()
map

-Qsim is taken as -Qs based on the GMT source code https://github.com/GenericMappingTools/gmt/blob/a091f7542cfded0ab0cc7f7fe1f899790b5eed56/src/grdview.c#L773-L777.

Copy link
Member

Choose a reason for hiding this comment

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

Nice, glad to know we don't need to add more combinations!

Comment on lines +150 to +152
- ``"surface"``: surface plot.
- ``"surface+mesh"``: surface plot with mesh lines drawn on top of the surface.
- ``"image"``: image plot.
Copy link
Member

Choose a reason for hiding this comment

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

Nice, glad to know we don't need to add more combinations!

surftype
Specify surface type for the grid. Valid values are:

- ``"mesh"``: mesh plot [Default].
Copy link
Member

Choose a reason for hiding this comment

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

Default is technically None if no argument is passed to surftype. Should we remove the [Default] then, or allow for surftype=True to mean surftype="m"?

Copy link
Member Author

Choose a reason for hiding this comment

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

I feel surftype=True is not intuitive, mainly because:

  • surftype=False is meaningless in this context (False usually means "disable")
  • surftype=True is mapped to -Q but -Q without any arguments is invalid in GMT.

Copy link
Member

@weiji14 weiji14 Jan 28, 2026

Choose a reason for hiding this comment

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

Agree, so let's just remove the [Default] to only allow string literals

Suggested change
- ``"mesh"``: mesh plot [Default].
- ``"mesh"``: mesh plot.

and probably need to update the docs to remove [Default] too at https://docs.generic-mapping-tools.org/6.6/grdview.html#q if -Q by itself is invalid. Unless I'm missing some context?

Copy link
Member Author

Choose a reason for hiding this comment

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

"Default" actually means -Q is not required, and if not given, -Qm is used.

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

Labels

enhancement Improving an existing feature final review call This PR requires final review and approval from a second reviewer

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants