You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add the Perspective class for setting perspective view #4176
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:
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.
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:
importpygmtfig=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=Truefig.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.
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.
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].
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
Activity
[-]POC: Pythonic way to set perspective view[/-][+]RFC: Pythonic way to set perspective view[/+]weiji14 commentedon Oct 23, 2025
I'm ok with a
pygmt.param.Perspectiveclass, but slightly against implementingfig.set_perspective, mostly because unlikefig.set_panel, there's no easy way to translate that to GMT syntax. We would still have to support the old method of passingperspectivethrough e.g.basemapon the first call, and having the newset_perspectivemethod 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
Figureclass instance.Personally, I would encourage a syntax that's more like:
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.
seisman commentedon Nov 12, 2025
OK. I've implemented a
Perspectiveclass in PR #4201. The implementation is simple, and we need to choose suitable parameter names.-p[x|y|z]azim[/elev[/zlevel]][+wlon0/lat0[/z0]][+vx0/y0]:x|y|z:planeImplement the Perspective class for specifying viewpoint #4201azim:azimuthImplement the Perspective class for specifying viewpoint #4201elev:elevationImplement the Perspective class for specifying viewpoint #4201zlevel:levelImplement the Perspective class for specifying viewpoint #4201+w:center[GMT usesworld]+v:viewpoint[GMT usesview]Reference:
seisman commentedon Nov 12, 2025
Currently, the
perspectiveparameter supports the following input forms:True, to reuse the previous perspective setting-psyntax with more controlsApparently, option 5 is not Pythonic, but it's not commonly used. The
Perspectiveclass will supersede it once implemented. Even after implementingPerspective, for backward compatibility, we still need to support the above forms, and more importantly,perspective=30is simpler thanperspective=Perspective(azimuth=30). Therefore, thePerspectiveclass 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
perspectiveparameter to the new alias system.[-]RFC: Pythonic way to set perspective view[/-][+]Pythonic way to set perspective view[/+][-]Pythonic way to set perspective view[/-][+]Add the Perspective class for setting perspective view[/+]seisman commentedon Aug 23, 2026
I find the GMT documentation about
+w/+vconfusing (https://docs.generic-mapping-tools.org/6.7/gmt.html#perspective-full):while the command usage (https://github.com/GenericMappingTools/gmt/blob/cb23d51e997641381b17308f6127a29555b7423e/src/gmt_init.c#L8370-L8377) is clearer:
Here is a script to understand the behavior:
From the source code, it's also clear that
+w/+vare exclusive (when used together,+vhas a higher priority).As mentioned above, GMT recommends long-form names
view/worldfor+v/+w, respectively, which I feel difficult to understand. What about using the same terminology as thePositionclass, i.e.,refpoint: specify the fixed reference point for the rotationcstype: specify the coordinate system type for the reference point, which can bemapcoordsandplotcoords, e.g.,refpoint=(4, 4), cstype="mapcoords"is mapped to+w4/4refpoint=(4, 4), cstype="plotcoords"is mapped to+v4/4