Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic legend labels with space in it #1632

Closed
thomas-romuald opened this issue Nov 23, 2021 · 4 comments · Fixed by #1487
Closed

Dynamic legend labels with space in it #1632

thomas-romuald opened this issue Nov 23, 2021 · 4 comments · Fixed by #1487
Labels
bug Something isn't working
Milestone

Comments

@thomas-romuald
Copy link

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
@thomas-romuald thomas-romuald added the bug Something isn't working label Nov 23, 2021
@welcome
Copy link

welcome bot commented Nov 23, 2021

👋 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.

@weiji14
Copy link
Member

weiji14 commented Nov 26, 2021

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.

@weiji14 weiji14 linked a pull request Nov 26, 2021 that will close this issue
8 tasks
@thomas-romuald
Copy link
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!

@weiji14
Copy link
Member

weiji14 commented Nov 26, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants