pygmt
pygmt copied to clipboard
Auto-generated legend doesn't work with "custom" (k) symbols
Description of the problem
When calling Figure.plot, you can specify a custom symbol by specifying the symbol with a k followed by the custom symbol name/file, such as kvolcano/16p. However, doing so breaks the auto-generated legend, failing with the following output:
legend [INFORMATION]: Processing input text table data
legend [INFORMATION]: Processing hidden legend specification file /Users/israel/.gmt/sessions/gmt_session.21374/gmt.1.legend
legend [INFORMATION]: Reading Data Table from File /Users/israel/.gmt/sessions/gmt_session.21374/gmt.1.legend
legend [INFORMATION]: Central meridian not given, default to -179.678
legend [INFORMATION]: Map scale is 9.45374 km per cm or 1:945374.
legend [INFORMATION]: Legend height not given, using estimated height of 1.40367 cm.
plot [INFORMATION]: Processing input table data
plot [INFORMATION]: Map scale is 0.001 km per inch or 1:39.3701.
plot [INFORMATION]: Reading Data Table from memory reference supplied by pointer
plot [ERROR]: Could not find either custom symbol or EPS macro
Looking at the temporary file generated, we find the following content:
# Auto-generated legend information file
# LEGEND_JUSTIFICATION: RT
# LEGEND_SCALING: 1
# LEGEND_FRAME: 1p white 0.2c
S - k 0.222222i red - - Plot1
S - k 0.222222i green - - Plot2
In which we see that it put the "k" for the symbol into the file, but failed to specify the custom symbol to use. In fact, if we modify this autogenerated file to add the symbol name again, like so:
# Auto-generated legend information file
# LEGEND_JUSTIFICATION: RT
# LEGEND_SCALING: 1
# LEGEND_FRAME: 1p white 0.2c
S - kvolcano 0.222222i red - - Plot1
S - kvolcano 0.222222i green - - Plot2
and then remove the label entries from our plots and feed the modified file in as the first parameter to legend (if we don't remove the label= entries from the plot command it always tries to use them, even with a file specified), we get the desired result.
Full code that generated the error
import pygmt
bounds = [-180.7568317475191,
-178.59975221352386,
51.37616035103776,
52.375229777638786]
fig = pygmt.Figure()
fig.basemap(projection="M10i", region=bounds, frame=('WeSn', 'afg'))
sta_x = -179
fig.plot(x = [sta_x, ], y = [52, ], style = 'kvolcano/16p', color = 'red')
sta_x = 179.75
fig.plot(x = [sta_x, ], y = [52, ], style = 'kvolcano/16p', color = 'green')
pos = 'JTR+jTR+o0.2c+l1.5'
#This fails with the above error
fig.legend(position = pos, box = "+gwhite+p1p", verbose = True)
fig.show(method = "external")
NOTE: I have not tried this on straight GMT, only PyGMT.
System information
Please paste the output of python -c "import pygmt; pygmt.show_versions()":
import pygmt; pygmt.show_versions()
PyGMT information:
version: v0.5.0
System information:
python: 3.9.5 (v3.9.5:0a7dcbdb13, May 3 2021, 13:17:02) [Clang 6.0 (clang-600.0.57)]
executable: /Applications/Wing Pro.app/Contents/Resources/wingdb
machine: macOS-10.15.7-x86_64-i386-64bit
Dependency information:
numpy: 1.21.2
pandas: 1.3.3
xarray: 0.19.0
netCDF4: 1.5.7
packaging: 21.0
ghostscript: 9.55.0
gmt: 6.3.0
GMT library information:
binary dir: /Library/Frameworks/Python.framework/Versions/3.9/Resources/Python.app/Contents/MacOS
cores: 8
grid layout: rows
library path: /usr/local/Cellar/gmt/6.3.0_2/lib/libgmt.dylib
padding: 2
plugin dir: /usr/local/Cellar/gmt/6.3.0_2/lib/gmt/plugins
share dir: /usr/local/Cellar/gmt/6.3.0_2/share/gmt
version: 6.3.0
👋 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.
GMT does not yet support auto-generated legend items for custom symbols. This issue is being tracked upstream by https://github.com/GenericMappingTools/gmt/issues/1973.