I love GMT & pygmt. However, I’ve run into a strange bug many times, and it surprises me to see no mention yet on this forum. The problem seems to be that in parsing of arguments in pygmt, string arguments containing spaces are broken up into separate arguments which are then causing errors when passed on to the GMT library.
As a sample, it does not seem possible to implement the pygmt version of this cookbook example for customising date/time annotation, where by using the “o dd” format the expected result is to be able to see both the month and day of the month (July 21, etc.). Now only the month is shown:
import pygmt
fig = pygmt.Figure()
pygmt.config(GMT_THEME="cookbook")
pygmt.config(FORMAT_DATE_MAP="o dd", FORMAT_CLOCK_MAP="hh:mm", FONT_ANNOT_PRIMARY="+9p")
fig.basemap(region="1969-7-21T/1969-7-23T/0/1", projection="X12c/0.5c", frame=["pxa6Hf1h", "sxa1K", "S"])
fig.basemap(frame=["pxa6Hf1h", "sxa1D", "S"], yshift="1.6c")
fig.show()
This code only throws this cryptic warning: “gmtset [WARNING]: Last GMT Defaults parameter from command options had no value”. I think this is because the “dd” is interpreted as a separate argument, which is not understood.
Here is another, perhaps more common example, when setting labels with spaces in them (which works fine in command-line GMT):
import pygmt
import numpy as np
x=np.linspace(0,10,101)
y1=np.sin(x)
y2=np.cos(x)
fig = pygmt.Figure()
fig.plot(x=x, y=y1, pen='1p,indianred', label='The function sin(x)')
fig.plot(x=x, y=y2, pen='1p,skyblue', label='The function cos(x)')
fig.basemap(frame=True)
fig.legend()
fig.show()
This results in a GMTCLibError and the following error messages:
gmtinfo [ERROR]: Cannot find file function
gmtinfo [ERROR]: Cannot find file sin(x)
plot [ERROR]: Cannot find file function
plot [ERROR]: Cannot find file sin(x)
plot [ERROR]: Must specify -R option
Removing the spaces from the label works fine, or replacing them by underscores. But I often need spaces in my labels. I’ve just found a workaround for this case with the labels: wrapping the argument in both single and double quotes, label=’“The function sin(x)”’. Another workaround I used was to construct this annotation with pstext, which handles spaces in strings fine. However, those workarounds do not seem to work for the pygmt.config problem. So I hope the issue can be fixed at the root level in a future version of pygmt.