Format/make custom Cartesian axis labels for longitude

Hi,

I have some plots which are Cartesian grdcontour() plots, where the x-axis is longitude, and the y-axis is depth. The longitudes are negative, and labelled as such (e.g, -51.0, -50.5 etc), but I’d like them to be formatted to be 51°W, 50.5°W etc.

This obviously happens automatically for the default Figure if plotting a geographic region, but not for Cartesian plots. Is there anyway to do this in PyGMT?

Many thanks,

Emma

I don’t know PyGMT, but if it is similar to GMT, that would be within the “projection” sub-object :

projection="X15cd/10c"

where X is cartesian projection, 15c is the width of the figure, d is to specify the nature of x-axis data as “degrees”.

(from GMT doc):

While the Cartesian linear projection is primarily designed for regular floating point �,� data, it is sometimes necessary to plot geographical data in a linear projection. This poses a problem since longitudes have a 360° periodicity. GMT therefore needs to be informed that it has been given geographical coordinates even though a linear transformation has been chosen. We do so by adding a g (for geographical) or d (for degrees) directly after -R or by appending a g or d to the end of the -Jx (or -JX ) option.

1 Like

Thank you, that works perfectly!

I’ve posted here as it’s related, I’ll create a new post if that’s preferred.

I’m trying to set the longitude tick annotations to be every 1 degree, but doing it as a frame option of ‘x+a1.0’ isn’t working:

    fig.grdimage(
        data,
        projection='X4.5cd/-2.5c',
        region=region,
        cmap="my_cmap.cpt",
        frame=['WStr', 'x+a1.0'],
        panel=[0,0]
    )

Any suggestions/pointers would be very much appreciated.

Hello @emmaworthington,

the + before the a is not correct here. a sets the annotation step, but +a leads to rotated annotations:

import pygmt

size = 5

fig = pygmt.Figure()

fig.basemap(
    region=[-size, size, -size, size],
    projection="X" + str(size*2) + "c",
    frame=["WSNE", "xa1"],  # Set annotation step
)

fig.shift_origin("w+1c")

fig.basemap(
    region=[-size, size, -size, size],
    projection="X" + str(size*2) + "c",
    frame=["WSNE", "x+a30"],  # Force rotated annotations
)

fig.show()
# fig.savefig(fname="a_vs_plusa.png")

Output figure

1 Like

Thank you, I wouldn’t have spotted that!

My very last question (plot is almost complete!) is if I use a ‘xa0.5’ for a half-degree longitude annotation, I get a ‘47°30’W’ tick label. Is there anyway to change this to be ‘47.5°W’?

This is very much a ‘nice to have’ option, so not a problem if it’s not a simple change.

many thanks again,

Emma

Here you need to adjust FORMAT_GEO_MAP:

# https://docs.generic-mapping-tools.org/6.4/gmt.conf.html#term-FORMAT_GEO_MAP
# For details see the table under https://docs.generic-mapping-tools.org/6.4/gmt.conf.html#term-FORMAT_GEO_OUT
pygmt.config(FORMAT_GEO_MAP="ddd.x")  # Use as many x as digits
1 Like

Thank you once more, FORMAT_GEO_MAP=“FD.x” gave me exactly what I needed, e.g., “47.5°W”, in case anyone else is ever looking for the solution.