|
3 | 3 | ======
|
4 | 4 |
|
5 | 5 | The :meth:`pygmt.Figure.legend` method can automatically create a legend for
|
6 |
| -symbols plotted using :meth:`pygmt.Figure.plot`. Legend entries are only |
7 |
| -created when the ``label`` parameter is used. For more complicated legends, |
8 |
| -including legends with multiple columns, users have to write an ASCII file |
9 |
| -with instructions for the layout of the legend items and pass it |
10 |
| -to the ``spec`` parameter of :meth:`pygmt.Figure.legend`. For details on |
11 |
| -how to set up such a file, please see the GMT documentation at |
12 |
| -:gmt-docs:`legend.html#legend-codes`. |
| 6 | +symbols plotted using :meth:`pygmt.Figure.plot`. A legend entry is only added |
| 7 | +when the ``label`` parameter is used to state the desired text. Optionally, |
| 8 | +to adjust the legend, users can append different modifiers. A list of all |
| 9 | +available modifiers can be found at :gmt-docs:`gmt.html#l-full`. To create a |
| 10 | +multiple-column legend **+N** is used with the desired number of columns. |
| 11 | +For more complicated legends, users may want to write an ASCII file with |
| 12 | +instructions for the layout of the legend items and pass it to the ``spec`` |
| 13 | +parameter of :meth:`pygmt.Figure.legend`. For details on how to set up such a |
| 14 | +file, please see the GMT documentation at :gmt-docs:`legend.html#legend-codes`. |
13 | 15 | """
|
14 | 16 |
|
15 | 17 | # %%
|
| 18 | +import numpy as np |
16 | 19 | import pygmt
|
17 | 20 |
|
18 |
| -fig = pygmt.Figure() |
| 21 | +# Set up some test data |
| 22 | +x = np.arange(-10, 10.2, 0.2) |
| 23 | +y1 = np.sin(x) + 1.1 |
| 24 | +y2 = np.cos(x) + 1.1 |
| 25 | +y3 = np.sin(x / 2) - 1.1 |
| 26 | +y4 = np.cos(x / 2) - 1.1 |
19 | 27 |
|
20 |
| -fig.basemap(projection="x2c", region=[0, 7, 3, 7], frame=True) |
| 28 | +# Create new Figure() object |
| 29 | +fig = pygmt.Figure() |
21 | 30 |
|
22 |
| -fig.plot( |
23 |
| - data="@Table_5_11.txt", |
24 |
| - style="c0.40c", |
25 |
| - fill="lightgreen", |
26 |
| - pen="faint", |
27 |
| - label="Apples", |
| 31 | +fig.basemap( |
| 32 | + projection="X10c/7c", |
| 33 | + region=[-10, 10, -3.5, 3.5], |
| 34 | + frame=["WSne", "xaf+lx", "ya1f0.5+ly"], |
28 | 35 | )
|
29 |
| -fig.plot(data="@Table_5_11.txt", pen="1.5p,gray", label="My lines") |
30 |
| -fig.plot(data="@Table_5_11.txt", style="t0.40c", fill="orange", label="Oranges") |
31 | 36 |
|
| 37 | +# ----------------------------------------------------------------------------- |
| 38 | +# Top: Vertical legend (one column, default) |
| 39 | + |
| 40 | +# Use the label parameter to state the text label for the legend entry |
| 41 | +fig.plot(x=x, y=y1, pen="1p,green3", label="sin(x)+1.1") |
| 42 | + |
| 43 | +fig.plot(x=x, y=y2, style="c0.07c", fill="dodgerblue", label="cos(x)+1.1") |
| 44 | + |
| 45 | +# Add a legend to the plot; place it within the plot bounding box with both |
| 46 | +# reference ("J") and anchor ("+j") points being TopRight and with an offset |
| 47 | +# of 0.2 centimeters in x and y directions; surround the legend with a box |
32 | 48 | fig.legend(position="JTR+jTR+o0.2c", box=True)
|
33 | 49 |
|
| 50 | +# ----------------------------------------------------------------------------- |
| 51 | +# Bottom: Horizontal legend (here two columns) |
| 52 | + |
| 53 | +# +N sets the number of columns corresponding to the given number, here 2 |
| 54 | +fig.plot(x=x, y=y3, pen="1p,darkred,-", label="sin(x/2)-1.1+N2") |
| 55 | + |
| 56 | +fig.plot(x=x, y=y4, style="s0.07c", fill="orange", label="cos(x/2)-1.1") |
| 57 | + |
| 58 | +# For a multi-column legend, users have to provide the width via "+w", here it |
| 59 | +# is set to 6 centimeters; reference and anchor points are set to BottomRight |
| 60 | +fig.legend(position="JBR+jBR+o0.2c+w6c", box=True) |
| 61 | + |
34 | 62 | fig.show()
|
0 commit comments