Skip to content

Allow export of panel size #4204

Open
Open

Description

@JackDMcGrath

If the projections that you are using are not initially defined by a given height and width, then it is difficult to extract the dimensions of the plot in cm. This is an issue, for example, if panel A is an oblique mercator projection (with a projection defined by a central point and a rotation, and a region given as a top left and bottom right coordinate) and you would like panel B to be the same height (where the projection is f'X{width}c/{height}c').

As fig.shift_origin() has included an option to get the plot size in inches since v15.0.0 :

    with Session() as lib:
        lib.call_module(module="plot", args=build_arg_list(kwdict))
        _xshift = lib.get_common("X")  # False or xshift in inches
        _yshift = lib.get_common("Y")  # False or yshift in inches

it feels like it should be possible to include a function where _xshift and _yshift can be exported as variables.

Example use:

proj =  f"Oa{center_lon}/{center_lat}/55/{width}c+v"   ##### PANEL HEIGHT IN CM NOT DEFINED
region = f"{tl_lon}/{tl_lat}/{br_lon}/{br_lat}+r"
fig = pygmt.Figure()
fig.coast(region=region, projection=proj, land="gray80", water="steelblue", area_thresh=1000)
panelA_width, panelA_height = fig.panel_dimensions('c')     ##### RETURN PANEL SIZE IN CM
fig.shift_origin(width='w')
fig.basemap(region=[0, xmax, 0, ymax], projection=f'X{panelb_width}c/{panelA_height}c', frame=['WSne', 'xafg2', 'yafg1.0'])   ##### ENSURE HEIGHT OF NEW PANEL MATCHES PANEL A

Are you willing to help implement and maintain this feature? Yes

Activity

  1. seisman commented on Nov 12, 2025

    @seisman
    Member

    I guess this is a feature request to PyGMT, so transfering it to the pygmt repository.

  2. github-actions commented on Nov 12, 2025

    @github-actions
    Contributor

    👋 Thanks for opening your first issue here!

    Please make sure you filled out the template with as much detail as possible. You might also want to take a look at our contributing guidelines and code of conduct.

  3. seisman commented on Nov 13, 2025

    @seisman
    Member

    Currently, PyGMT does not provide this functionality and also does not store map dimensions.

    The example below demonstrates how this can be achieved using the GMT command-line interface (CLI):

    gmt begin map png
    	gmt coast -R-90/20/-65.5327/29.4248+r -JOc280/20/22/69/2i -Dl -Glightbrown -Wthinnest -Ba10g5 -BWsEN
    	read -r width height <<< $(echo -65.5327 29.4248 | gmt mapproject -R-90/20/-65.5327/29.4248+r -JOc280/20/22/69/2i)
    	echo "$width $height"
    	gmt basemap -R0/10/0/10 -JX${width}/${height} -Xw+2c -Baf
    gmt end show
    

    As illustrated in the script, the mapproject module can be used to calculate the distance (in centimeters) from any point to the plotting origin. In this example, the point corresponds to the top-right corner of the basemap. The output from mapproject is stored in the variables width and height, which can then be used to position additional map elements.

    The resulting image is shown below:

    Image

    Although it remains unclear whether the feature proposed by @JackDMcGrath should be implemented in PyGMT, it would at least be beneficial to wrap the mapproject module, as first requested in #1986.

    @JackDMcGrath Please let us know if you'd like to help wrapping the mapproject module in PyGMT.

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

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions