Warning and error when adding a legend

Hi! I’m adding some legends for my map. These include lines(faults, trenches) and volcanoes. The plotted lines came from some shp file then I converted to .gmt file.

fig.plot(
data=“AF.gmt”,
pen=“red”,
label = ‘Fault’
)

fig.plot(
data=“trench.gmt”,
Sf = ‘+.3i/.1i+l+t’,
pen=“.8p,blue”,
label = ‘Trench’
)

fig.plot(
x =volLon,
y =volLat,
Sk = ‘volcano/0.2i’,
color = ‘red’,
label = ‘Volcano’
)

fig.legend(position=“JTR+jTR+o0.2c”, box=‘+gwhite+p1p’)

When I tried to add these to a legend box, it threw a warning and an error.

plot [WARNING]: Cannot use auto-legend -l for selected feature. Option -l ignored.

plot [ERROR]: Could not find either custom symbol or EPS macro

Hi @dorotdot, thanks for trying out PyGMT! Looks like we’ll need some more information to help you out though. Since there’s 3 fig.plot commands, could you tell us which one is the one reporting the warning or error? Also, if it’s ok with you, it will help to provide us with a sample of your “AF.gmt” and “trench.gmt” files (either make a *.zip file or change *.gmt to *.txt so that you can upload it).

Hi @weiji14, thank you for the reply.

I think the

plot [WARNING]: Cannot use auto-legend -l for selected feature. Option -l ignored.

is the result of putting the plot of .gmt(AF and trench.gmt) in the legends

and the

plot [ERROR]: Could not find either custom symbol or EPS macro

is caused by the label volcano.

I put a link to a google drive to these files. Sorry, I can’t attach the files caused it’s big.
https://drive.google.com/drive/folders/103g28D-SS1qNvQSFHxk6PuW46vm6vtUI?usp=sharing

This my whole code

import os, pygmt
import pandas as pd

region=[119.166,123.681,16.948,20.411]
volLat = [19.52408,18.22116,18.83037,19.07533,19.53915]
volLon = [121.95005,122.1163,121.86280,122.20147,121.91367]
fig = pygmt.Figure()
fig.coast(
	region = region,
	projection= 'M15c',
	shorelines=True,
	water = 'skyblue',
	land = '112/128/144',
	frame= 'a1f1',
	resolution = 'f'
)

fig.plot(
    data="AF.gmt",
    pen="red",
    label = 'Fault'
)

fig.plot(
    data="trench.gmt",
    Sf = '+.3i/.1i+l+t',
    pen=".8p,blue",
    label = 'Trench'
)

fig.plot(
	x =volLon,
	y =volLat,
	Sk = 'volcano/0.2i',
	color = 'red',
	label = 'Volcano'
)

fig.legend(position="JTR+jTR+o0.2c", box='+gwhite+p1p')
fig.savefig("out.png")

I met the same question. I wonder whether fig.legend supports the custom symbols?

I have a similar problem. I’ve created my own custom symbol using a .def file, which plots find, but it messes up the legend

Hello @JonathonLeonard,

Welcome to the GMT forum :slightly_smiling_face:!

Unfortunately, custom symbols are not supported in auto-legend yet.

I see! It’s probably about time I start doing my own legends with basemap anyway. Thanks for replying so fast.

To include custom symbols into a legend, you have to write a GMT-specific legend file and pass this file to the spec parameter of Figure.legend. Please see the legend codes at legend — GMT 6.5.0 documentation for a detailed explanation of the syntax.

Below you find an example using the volcano symbol:
Legend file (please place it in your working directory): test_legend.txt (101 Bytes)

import pygmt

size = 5

fig = pygmt.Figure()
fig.basemap(region=[-size, size, -size, size], projection=f"X{size*2}c", frame=True)

fig.plot(x=0, y=0, style="kvolcano/1c")

fig.legend(spec="test_legend.txt")

fig.show()
# fig.savefig(fname="legend_cust_symb_from_file.png")

2 Likes

Thank you!