pythonpygmt

Is there a way to customize PyGMT legend font size?


I'm trying to change the font size of the legend in this PyGMT program, but I can't seem to find how.

Here are the lines for the legend:

#read Seismic stations from txt file
ETNA=pd.read_csv("/Users/user/Desktop/PyGmt/ETNA.txt", sep='\s+')
fig.plot(x=ETNA.LONG, y=ETNA.LAT, style="i0.15c", fill='red', pen="gray", label='81 ETNA')

# Legend
fig.legend(position='g-2.6/32+w2.4/1.5',box='+gwhite+p1p+i', S=1.3)

Solution

  • Generally, the fontsize of all primary annotations can be adjusted in a pygmt.config() call by FONT_ANNOT_PRIMARY (see also here). To adjust the fontsize only within the legend you need to put the call into a with statement:

    import pygmt
    
    fig = pygmt.Figure()
    
    fig.basemap(projection="x2c", region=[0, 7, 3, 7], frame=True)
    
    fig.plot(
        data="@Table_5_11.txt",
        style="c0.40c",
        fill="lightgreen",
        pen="faint",
        label="Apples",
    )
    fig.plot(data="@Table_5_11.txt", pen="1.5p,gray", label="My lines")
    fig.plot(data="@Table_5_11.txt", style="t0.40c", fill="orange", label="Oranges")
    
    # adjust font size only for legend
    with pygmt.config(FONT_ANNOT_PRIMARY="20p"):
        fig.legend(position="JTR+jTR+o0.2c", box="+gwhite+pblack")
    
    fig.show()
    

    enter image description here