How to draw a text box in a map?

My goal is to plot a text box like the below onto my map:
A - Name1
B - Name2
C - Name3

I stored the above info in a text file named “text.rtf”, and below is the code to draw the text box:
text!("/Path/text.rtf", font="5p,Times-Bold,black", x=195, y=45);

Unfortunately, I got a ton of errors like the below. I wonder if anyone could tell me what I did wrong? Many thanks!

pstext [WARNING]: Mismatch between actual (1) and expected (2) fields near line 1 in file /Path/text.rtf
pstext [WARNING]: Mismatch between actual (1) and expected (2) fields near line 5 in file /Path/text.rtf
pstext [WARNING]: Mismatch between actual (1) and expected (2) fields near line 6 in file /Path/text.rtf
Warning: the following options were not consumed in pstext => [:y, :x]
psxy [WARNING]: 2 external clip operations were not terminated!
Error: /syntaxerror in /----nostringval----
Operand stack:

Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push   1990   1   3   %oparray_pop   1989   1   3   %oparray_pop   1977   1   3   %oparray_pop   1833   1   3   %oparray_pop   --nostringval--   %errorexec_pop   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push
Dictionary stack:
   --dict:768/1123(ro)(G)--   --dict:0/20(G)--   --dict:75/200(L)--   --dict:162/250(L)--
Current allocation mode is local
psconvert [ERROR]: System call [gs -q -dNOSAFER -dNOPAUSE -dBATCH -sDEVICE=bbox -DPSL_no_pagefill -dMaxBitmap=2147483647 -dUseFastColor=true '/var/folders/28/fc899mm50s3dn8f971z3vbwm0000gn/T/' 2> './'] returned error 256.
ERROR: LoadError: Something went wrong when calling the module. GMT error number = 79
 [1] error(s::String)
   @ Base ./error.jl:33
 [2] gmt(::String)
   @ GMT ~/.julia/packages/GMT/TPV4a/src/gmt_main.jl:288
 [3] showfig(d::Dict{Symbol, Any}, fname_ps::String, fname_ext::String, opt_T::String, K::Bool, fname::String)
   @ GMT ~/.julia/packages/GMT/TPV4a/src/common_options.jl:3273
 [4] finish_PS_module(::Dict{Symbol, Any}, ::Vector{String}, ::String, ::Bool, ::Bool, ::Bool, ::Vector{GMTdataset}, ::Vararg{Any})
   @ GMT ~/.julia/packages/GMT/TPV4a/src/common_options.jl:3428
 [5] common_plot_xyz(::String, ::Vector{GMTdataset}, ::String, ::Bool, ::Bool, ::Pair{Symbol, Any}, ::Vararg{Pair{Symbol, Any}})
   @ GMT ~/.julia/packages/GMT/TPV4a/src/psxy.jl:215
 [6] #plot#296
   @ ~/.julia/packages/GMT/TPV4a/src/plot.jl:112 [inlined]
 [7] #plot!#297
   @ ~/.julia/packages/GMT/TPV4a/src/plot.jl:114 [inlined]
 [8] top-level scope

Really? Are you really expecting that GMT can read the Microsoft rtf format?

But your command wouldn’t work anyway even if the file was in plain ASCII. Maybe you are after the plot(...., legend=(label="BlaBla",)) option. Otherwise you must read the multi-paragraph mode of text (probably must check the GMT text manual).

1 Like

@leon6j, try this input file for legend.
legend_example_input.dat (289 Bytes)

Then use:

position=“jTR+o0.3c+w5.5c”, box=True)

Follow also @Truyenpt suggestions.
Stay safe.

1 Like

Thank you so much! :+1:This works.

I had to make some small changes for Julia:
legend!("legend_input.dat", position="jTR+o0.3c+w5.5c", box=1);

Would you please help me with another question? How do I specify the font size and line spacing of my legend?

Many thanks.

Dear @leon6j,
To avoid confusing you I send you the following link:

You should increase / decrease the number after the “G” letter (on the input file) for the space between lines

For the font, you can try changing the number 0.7 ( “0.7c El-Alto”)
However you may see the example at the end of the link I sent, I hope this answer your question.
Stay safe and best regards

Thank you for the reply. Based on the menu, the number before the text is the distance from the left boundary of the legend.

The manual says to use FONT_ANNOT_PRIMARY to control and font and size. I put the below before the text in the dat file, but it did not work. GMT simply show them as if they were intended to be part of the text.
FONT_ANNOT_PRIMARY = [12p,Helvetica,black]

Dear @leon6j,
Maybe this link can help you:
Hope this help, stay safe and best regards.

1 Like

Since when can we do this? There are over 150 parameters we can configure and there is obvious no checking for them individually in the wrapper. The syntax, as it appears in several examples nd in the docs, is par=(FONT_ANNOT_PRIMARY = "12p,Helvetica,black",)

Tonino, this not pygmt but GMT.jl :slight_smile:

1 Like

pslegend is not the friendliest of the modules at the moment (hard to verbalize all those codes) but it doesn’t take much to reproduce the example in the man page (GMT man page).

makecpt("-Cpanoply -T-8/8 > tt.cpt")

T = text_record(["G -0.1i",
       "H 24p,Times-Roman My Map Legend",
       "D 0.2i 1p",
       "N 2",
       "V 0 1p",
       "S 0.1i c 0.15i p300/12 0.25p 0.3i This circle is hachured",
       "S 0.1i e 0.15i yellow 0.25p 0.3i This ellipse is yellow",
       "S 0.1i w 0.15i green 0.25p 0.3i This wedge is green",
       "S 0.1i f 0.25i blue 0.25p 0.3i This is a fault",
       "S 0.1i - 0.15i - 0.25p,- 0.3i A contour",
       "S 0.1i v 0.25i magenta 0.5p 0.3i This is a vector",
       "S 0.1i i 0.15i cyan 0.25p 0.3i This triangle is boring",
       "D 0.2i 1p",
       "V 0 1p",
       "N 1",
       "M 5 5 600+u+f",
       "G 0.05i",
       "I @SOEST_block4.png 3i CT",
       "G 0.05i",
       "B tt.cpt 0.2i 0.2i -B0",
       "G 0.05i",
       "L 9p,Times-Roman R Smith et al., @%5%J. Geophys. Res., 99@%%, 2000",
       "G 0.1i",
       "T Let us just try some simple text that can go on a few lines.",
       "T There is no easy way to predetermine how many lines may be required",
       "T so we may have to adjust the height to get the right size box."]);

legend(T, region=(0,10,0,10), pos=(paper=true, anchor=(1.25,1.25), width=14,
            justify=:BL, spacing=1.2), clearance=(0.25,0.25), box=(pen=0.5, fill=:azure1),
           show=true, proj=:Mercator)

Note, one could also have used the more cryptic

R="0/10/0/10", J="M6i", D="x0.5i/0.5i+w5i+jBL+l1.2",  C="0.1i/0.1i",  F="+p+gazure1+r", ...


Thanks, Joaquim.

It seems that the font size is by default in this example?