Skip to content

Dynamic legend labels with space in it #1632

@thomas-romuald

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions