Skip to content

Dynamic legend labels with space in it #1632

Closed

Description

@thomas-romuald

Description of the problem

I would like to display the number of dots in the legend. This number is not static it comes from a data file.
However, I don't understand how to do it with space in the text. The label='"this is my data"' works only for static data.

May I help on this ?

outputFile

Full code that generated the error

Works

# Legend labels
label_restricted= "Data_requering_owner`s_authorization_"+str(data_restricted.size//3)+"_points"
label_open = "Available_data_"+str(data_open.size//3)+"_points"

Does not work

# Legend labels
label_restricted= "Data requering owner`s authorization "+str(data_restricted.size//3)+" points"
label_open = "Available data "+str(data_open.size//3)+" points"

Rest of the code

# Create map with shorelines and borders
fig = pygmt.Figure()
fig.basemap(region=region, frame="ag",  projection="M25c")
fig.coast(shorelines="1/0.5p,black", borders="1/0.5p,black")

# Plot data
if data_restricted.empty == False:
    fig.plot(x=data_restricted.longitude, y=data_restricted.latitude, style="t0.08c", color="red", pen="red", label=f"{label_restricted}+S0.50c")

if data_open.empty == False:
    fig.plot(x=data_open.longitude, y=data_open.latitude, style="t0.08c", color="blue", pen="blue", label=f"{label_open}+S0.50c")

# Add legend outside the map frame
fig.legend(position="JMB+o1.4c", box=True)

Full error message

$ python test.py landata_Iles_Baleares.csv
plot [ERROR]: Cannot find file data
plot [ERROR]: Cannot find file 484
plot [ERROR]: Cannot find file points+S0.50c
Traceback (most recent call last):
  File "/home/tromuald/Documents/BGI/scripts/test.py", line 67, in <module>
    fig.plot(x=data_open.longitude, y=data_open.latitude, style="t0.08c", color="blue", pen="blue", label=f"{label_open}+S0.50c")
  File "/home/tromuald/anaconda3/envs/pygmt/lib/python3.9/site-packages/pygmt/helpers/decorators.py", line 805, in new_module
    return module_func(*args, **kwargs)
  File "/home/tromuald/anaconda3/envs/pygmt/lib/python3.9/site-packages/pygmt/helpers/decorators.py", line 805, in new_module
    return module_func(*args, **kwargs)
  File "/home/tromuald/anaconda3/envs/pygmt/lib/python3.9/site-packages/pygmt/helpers/decorators.py", line 873, in new_module
    return module_func(*args, **kwargs)
  File "/home/tromuald/anaconda3/envs/pygmt/lib/python3.9/site-packages/pygmt/helpers/decorators.py", line 586, in new_module
    return module_func(*args, **kwargs)
  File "/home/tromuald/anaconda3/envs/pygmt/lib/python3.9/site-packages/pygmt/helpers/decorators.py", line 726, in new_module
    return module_func(*args, **kwargs)
  File "/home/tromuald/anaconda3/envs/pygmt/lib/python3.9/site-packages/pygmt/src/plot.py", line 275, in plot
    lib.call_module("plot", arg_str)
  File "/home/tromuald/anaconda3/envs/pygmt/lib/python3.9/site-packages/pygmt/clib/session.py", line 500, in call_module
    raise GMTCLibError(
pygmt.exceptions.GMTCLibError: Module 'plot' failed with status code 72:
plot [ERROR]: Cannot find file data
plot [ERROR]: Cannot find file 484
plot [ERROR]: Cannot find file points+S0.50c

System information

Please paste the output of python -c "import pygmt; pygmt.show_versions()":

$ python -c "import pygmt; pygmt.show_versions()"
PyGMT information:
  version: v0.5.0
System information:
  python: 3.9.7 | packaged by conda-forge | (default, Sep 29 2021, 19:20:46)  [GCC 9.4.0]
  executable: /home/tromuald/anaconda3/envs/pygmt/bin/python
  machine: Linux-4.18.0-147.5.1.el8_1.x86_64-x86_64-with-glibc2.28
Dependency information:
  numpy: 1.21.4
  pandas: 1.3.4
  xarray: 0.20.1
  netCDF4: 1.5.8
  packaging: 21.0
  ghostscript: 9.54.0
  gmt: 6.2.0
GMT library information:
  binary dir: /home/tromuald/anaconda3/envs/pygmt/bin
  cores: 8
  grid layout: rows
  library path: /home/tromuald/anaconda3/envs/pygmt/lib/libgmt.so
  padding: 2
  plugin dir: /home/tromuald/anaconda3/envs/pygmt/lib/gmt/plugins
  share dir: /home/tromuald/anaconda3/envs/pygmt/share/gmt
  version: 6.2.0

Activity

  1. added
    bug
    on Nov 23, 2021
  2. welcome commented on Nov 23, 2021

    @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. weiji14 commented on Nov 26, 2021

    @weiji14
    Member

    Hi @thomas-romuald, this is a problem related to #247, there are two possible workarounds.

    1. Use \040 (the octal code for space) to replace a literal space in your label text as documented in Arguments with spaces in strings #247 (comment). So label="this is my data" would become label="this\040is\040my\040data".

    2. Use double quotes nested inside single quotes properly. E.g. like so:

    import pygmt
    
    # Legend labels
    label_restricted= "Data requering owner`s authorization "+str(10//3)+" points"
    label_open = "Available data "+str(20//3)+" points"
    print(label_restricted)
    print(label_open)
    
    # Create map with shorelines and borders
    fig = pygmt.Figure()
    fig.basemap(region=[12, 56, 34, 78], frame="ag",  projection="M25c")
    fig.coast(shorelines="1/0.5p,black", borders="1/0.5p,black")
    
    # Plot data
    # if data_restricted.empty == False:
    fig.plot(x=12, y=34, style="t0.08c", color="red", pen="red", label=f'"{label_restricted}"+S0.50c')
    
    # if data_open.empty == False:
    fig.plot(x=56, y=78, style="t0.08c", color="blue", pen="blue", label=f'"{label_open}"+S0.50c')
    
    # Add legend outside the map frame
    fig.legend(position="JMB+o1.4c", box=True)
    fig.show()

    produces:

    image

    Let us know if this works for you, or if you have any other questions on usage. We'll try to get this 'space in string' bug fixed in the next version, since it's been an ongoing problem for a while. I'll remind myself to add your failing example as a test in the bugfix at #1487.

  4. thomas-romuald commented on Nov 26, 2021

    @thomas-romuald
    Author

    Hi @weiji14 thank you very much for your support ! It works now!
    And thank you for your great work on this tool. I've just started using it recently and I love it!

  5. weiji14 commented on Nov 26, 2021

    @weiji14
    Member

    Cool, great to hear! The team has done an amazing job over the years, and it's always great to see more people using it 😀.

    If you have any other questions, feel free to ask them on the GMT forum at https://forum.generic-mapping-tools.org (you can use your GitHub login to sign in). I'll keep this bug report open for a while until #1487 is merged.

  6. added this to the 0.6.0 milestone on Mar 13, 2022
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