Skip to content

Add the Perspective class for setting perspective view #4176

Open

Description

@seisman

GMT’s -p option enables perspective view by setting the azimuth and elevation of the viewpoint. The full documentation is available at:
https://docs.generic-mapping-tools.org/6.6/gmt.html#perspective-full

The GMT command-line syntax is rather complex:

-p[x|y|z]azim[/elev[/zlevel]][+wlon0/lat0[/z0]][+vx0/y0]

At present, we must pass the raw string directly to the perspective parameter in the first plotting command, and then specify perspective=True in subsequent commands. This approach feels un-Pythonic.

One possible improvement is to define an auxiliary class that can be passed to the perspective parameter, for example:

perspective = Perspective(azimuth, elevation, zlevel, ...)

However, a more Pythonic design might be to provide a method—similar to Matplotlib’s [view_init](https://matplotlib.org/stable/api/_as_gen/mpl_toolkits.mplot3d.axes3d.Axes3D.view_init.html)—that allows setting the perspective once and reusing it, like so:

import pygmt

fig = pygmt.Figure()
fig.set_perspective(azimuth=xxx, elevation=xxx, zlevel=xxx)
fig.basemap(region=xxx, projection=xxx, perspective=True)
fig.coast(..., perspective=True)
fig.text(...)  # No perspective parameter needed

With this approach, the perspective parameter can simply take a boolean value—True to apply the perspective settings defined by fig.set_perspective, and False to disable them. This would also simplify the docstring for the perspective parameter.

This is still an early idea, and I’d appreciate any feedback before proceeding with implementation.

Activity

  1. added
    discussions
    on Oct 23, 2025
  2. changed the title [-]POC: Pythonic way to set perspective view[/-] [+]RFC: Pythonic way to set perspective view[/+] on Oct 23, 2025
  3. weiji14 commented on Oct 23, 2025

    @weiji14
    Member

    I'm ok with a pygmt.param.Perspective class, but slightly against implementing fig.set_perspective, mostly because unlike fig.set_panel, there's no easy way to translate that to GMT syntax. We would still have to support the old method of passing perspective through e.g. basemap on the first call, and having the new set_perspective method would go against the 'There should be one-- and preferably only one --obvious way to do it'. It is possible to switch to the new syntax only, but then there would be a tricky deprecation cycle.

    Implementation-wise, it might require #2388 (make PyGMT stateful), at least having state in the Figure class instance.

    Personally, I would encourage a syntax that's more like:

    import pygmt
    
    fig = pygmt.Figure()
    pp = pygmt.param.Perspective(azimuth=xxx, elevation=xxx, zlevel=xxx)
    fig.basemap(region=xxx, projection=xxx, perspective=pp)
    fig.coast(..., perspective=pp)  # can also use perspective=True
    fig.text(...)  # No perspective parameter needed

    this same style of instantiating a param class first, and then using it in the Figure methods can be learned more easily across the different Alias classes (Box/Pattern/Pen/Projection/etc) that are tracked in #4122.

  4. seisman commented on Nov 12, 2025

    @seisman
    MemberAuthor
  5. seisman commented on Nov 12, 2025

    @seisman
    MemberAuthor

    Currently, the perspective parameter supports the following input forms:

    • A single value: azimuth
    • A sequence of two values: (azimuth, elevation)
    • A sequence of three values: (azimuth, elevation, zlevel)
    • True, to reuse the previous perspective setting
    • A string (e.g., "x150/30+w30/30") to use the full GMT -p syntax with more controls

    Apparently, option 5 is not Pythonic, but it's not commonly used. The Perspective class will supersede it once implemented. Even after implementing Perspective, for backward compatibility, we still need to support the above forms, and more importantly, perspective=30 is simpler than perspective=Perspective(azimuth=30). Therefore, the Perspective class is not a high-priority feature and does not need to be completed before submitting the paper.

    PS: I've started PR #4206 to migrate the perspective parameter to the new alias system.

  6. changed the title [-]RFC: Pythonic way to set perspective view[/-] [+]Pythonic way to set perspective view[/+] on Jun 25, 2026
  7. changed the title [-]Pythonic way to set perspective view[/-] [+]Add the Perspective class for setting perspective view[/+] on Jun 25, 2026
  8. added
    feature
    and removed
    discussions
    on Jun 30, 2026
  9. moved this from To do to In Progress in Pythonic GMT argumentson Jun 30, 2026
  10. added
    feature request
    and removed
    feature
    on Jul 25, 2026
  11. added this to the 0.20.0 milestone on Aug 13, 2026
  12. seisman commented on Aug 23, 2026

    @seisman
    MemberAuthor

    I find the GMT documentation about +w/+v confusing (https://docs.generic-mapping-tools.org/6.7/gmt.html#perspective-full):

    For frames used for animation, we fix the center of your data domain. Specify another center using one of the following modifiers:

    +w - Project lon0/lat0 (and z0 if applicable) to the center of the page size.
    +v - Specify the coordinates of the projected 2-D view point as x0/y0.

    while the command usage (https://github.com/GenericMappingTools/gmt/blob/cb23d51e997641381b17308f6127a29555b7423e/src/gmt_init.c#L8370-L8377) is clearer:

    For a plain rotation about the z-axis, give rotation angle only and optionally use modifiers
    +w or +v to select location of axis:
    +w Specify a fixed coordinate point [region center].
    +v Set a fixed projected point [panel center].

    Here is a script to understand the behavior:

    gmt begin map
        gmt basemap -R-10/10/-10/10 -JX10c -Ba2g2
        gmt basemap -R-10/10/-10/10 -JX10 -Ba2g2 -p30
    
        gmt basemap -R-10/10/-10/10 -JX10c -Ba2g2 -Xw+5c
        gmt basemap -R-10/10/-10/10 -JX10c -Ba2g2 -p30+w4/4
        gmt basemap -R-10/10/-10/10 -JX10c -Ba2g2 -p60+w4/4
        gmt basemap -R-10/10/-10/10 -JX10c -Ba2g2 -p120+w4/4
    
        gmt basemap -R-10/10/-10/10 -JX10c -Ba2g2 -Xw+10c
        gmt basemap -R-10/10/-10/10 -JX10c -Ba2g2 -p30+v4/4
        gmt basemap -R-10/10/-10/10 -JX10c -Ba2g2 -p60+v4/4
        gmt basemap -R-10/10/-10/10 -JX10c -Ba2g2 -p120+v4/4
    
    gmt end show
    
    Image As can be seen, `-p` without `+w`/`+v` rotates about the plot origin; `+w4/4` rotates about the user data coordinates (4, 4); `+v4/4` rotates about the plot coordinates ("4c", "4c").

    From the source code, it's also clear that +w/+v are exclusive (when used together, +v has a higher priority).

    As mentioned above, GMT recommends long-form names view/world for +v/+w, respectively, which I feel difficult to understand. What about using the same terminology as the Position class, i.e.,

    • refpoint: specify the fixed reference point for the rotation
    • cstype: specify the coordinate system type for the reference point, which can be mapcoords and plotcoords, e.g.,
      • refpoint=(4, 4), cstype="mapcoords" is mapped to +w4/4
      • refpoint=(4, 4), cstype="plotcoords" is mapped to +v4/4
  13. pinned this issue on Aug 23, 2026
  14. removed this from the 0.20.0 milestone on Oct 22, 2026
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

    feature request

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions