Skip to content

The fig.text function in PyGMT does not plot text if the text is purely numeric in pygmt 0.14.1 #3803

Closed
weiji14/pygmt
#1253

Description

@naveenragur

Description of the problem

Problem: The fig.text function in PyGMT does not plot text if the text is purely numeric.

Steps to Reproduce:
Create a PyGMT figure.
Use the fig.text function to plot a numeric string (e.g., "12345").
Observe that the numeric text does not appear on the plot.

Expected Behavior: The numeric text should be plotted on the figure.

Actual Behavior: The numeric text does not appear on the figure.

Minimal Complete Verifiable Example

import pygmt
fig = pygmt.Figure()
fig.basemap(region=[0, 10, 0, 10], projection="X10c/10c", frame=True)
fig.text(x=5, y=5, text="12345")  # Numeric text
fig.show()
#doesnt plot text and raises error message

import pygmt
fig = pygmt.Figure()
fig.basemap(region=[0, 10, 0, 10], projection="X10c/10c", frame=True)
fig.text(x=5, y=5, text="12345a")  # Numeric text
fig.show()
#plots text as expected

Full error message

text [ERROR]: Text record line 0 is NULL! Skipped but this is trouble)

System information

PyGMT information:
  version: v0.14.1
System information:
  python: 3.13.1 | packaged by conda-forge | (main, Jan 13 2025, 09:53:10) [GCC 13.3.0]
  executable: /home/nrr/miniconda3/envs/pygmt/bin/python
  machine: Linux-6.8.0-52-generic-x86_64-with-glibc2.35
Dependency information:
  numpy: 2.2.2
  pandas: 2.2.3
  xarray: 2025.1.2
  netCDF4: 1.7.2
  packaging: 24.2
  contextily: 1.6.2
  geopandas: 1.0.1
  IPython: 8.32.0
  pyarrow: 19.0.0
  rioxarray: 0.18.2
  gdal: 3.10.1
  ghostscript: 10.04.0
GMT library information:
  version: 6.5.0
  padding: 2
  share dir: /home/nrr/miniconda3/envs/pygmt/share/gmt
  plugin dir: /home/nrr/miniconda3/envs/pygmt/lib/gmt/plugins
  library path: /home/nrr/miniconda3/envs/pygmt/lib/libgmt.so
  cores: 20
  grid layout: rows
  image layout: 
  binary version: 6.5.0

Activity

  1. added
    bug
    on Feb 12, 2025
  2. welcome commented on Feb 12, 2025

    @welcome

    👋 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. yvonnefroehlich commented on Feb 12, 2025

    @yvonnefroehlich
    Member

    Hi @naveenragur. Thanks for your clear report!

    For now, I just tried a few examples (Windows, PyGMT dev, GMT 6.5.0) and I can confirm this issue. I found that adding a white space to the pure "numeric string" may work as an workaround for now. This problem is probably related to changes in PR #3760 for the datetime bug-fix.

    import pygmt
    
    args_map = {"region": [-1, 1, -1, 1], "projection": "X5c", "frame": 1}
    
    fig = pygmt.Figure()
    fig.basemap(**args_map)
    fig.text(x=0, y=0, text=42, pen=True)  # fails
    fig.show()
    
    fig = pygmt.Figure()
    fig.basemap(**args_map)
    fig.text(x=0, y=0, text="42", pen=True)  # fails
    fig.show()
    
    fig = pygmt.Figure()
    fig.basemap(**args_map)
    fig.text(x=0, y=0, text="42 ", pen=True)  # works
    fig.show()
    
    fig = pygmt.Figure()
    fig.basemap(**args_map)
    fig.text(x=0, y=0, text="42a", pen=True)  # works
    fig.show()
    text=42 text="42" text="42 " text="42a"
    Image Image Image Image
  4. seisman commented on Feb 12, 2025

    @seisman
    Member

    I can reproduce it. It's caused by changes in #3760. Need some time to find a fix.

  5. seisman commented on Feb 12, 2025

    @seisman
    Member

    I can confirm that the bug was introduced by #3760. In that PR, we tried to convert any np.object/np.str_ arrays into a np.datetime64 array. However, prior to v0.14.0, we only tried to convert np.object arrays.

    In v0.14.0:

    pygmt/pygmt/clib/session.py

    Lines 937 to 940 in 2a84b5e

    # For 1-D arrays, try to convert unknown object type to np.datetime64.
    if ndim == 1 and array.dtype.type is np.object_:
    with contextlib.suppress(ValueError):
    array = array_to_datetime(array)

    In v0.13.0 and before:

    pygmt/pygmt/clib/session.py

    Lines 874 to 878 in 1ff03a3

    if array.dtype.type is np.object_:
    # Try to convert unknown object type to np.datetime64
    array = array_to_datetime(array)
    else:
    raise ValueError

    With codes in #3760, datetimes given as strings in ISO datetime format can be converted to a np.datetime64 array before passing to GMT. It works well for strings like ["2012-01", "2012-01-02", "2012-02-03T01:00:02"], but it causes the issue reported here for strings that contain numeric values only, e.g., ["12345", "67890"]:

    >>> np.array(["12345", "67890"], dtype=np.datetime64)
    array(['12345', '67890'], dtype='datetime64[Y]')
    

    Actually, it's impossible to know if a string array like ["12345", "67890"] contains numeric values or date years.

    There are two possible solutions:

    1. Before converting np.object/np.str_ array to np.datetime64, check if a np.str_ array contains numeric values only. If yes, then we can't know if they're numeric values or date years. We should return the original string array directly. The following codes implement this idea:
        # Check if a string array contains only numeric values. If so, return the original
        # string array. Addressing https://github.com/GenericMappingTools/pygmt/issues/3803.
        if array.dtype.type == np.str_:
            try:
                _ = array.astype(float)
            except (TypeError, ValueError):
                pass
            else:
                return array
    
    1. Revert back to the behavior prior to v0.14.0, i.e., do NOT try to convert np.str_ arrays into np.datetime64. It works well because GMT API function GMT_Put_Vector is clever enough to parse datetime-like strings.

    I prefer to solution 2 and it's implemented in #3804.

  6. added this to the 0.14.2 milestone on Feb 14, 2025
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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions