Skip to content

Commit 9b11099

Browse files
Tutorial "Plotting data points": Add legend for size coding in scatter plot (#4214)
1 parent 930ffd7 commit 9b11099

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

examples/tutorials/basics/plot.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
"""
1111

1212
# %%
13+
import io
14+
1315
import pygmt
1416

1517
# %%
@@ -49,6 +51,11 @@
4951
# array to the ``size`` parameter. Because the magnitude is on a logarithmic
5052
# scale, it helps to show the differences by scaling the values using a power
5153
# law.
54+
#
55+
# A legend for the size of the circles can not be added automatically. But users can
56+
# create an :class:`io.StringIO` object, which can be passed to the ``spec`` parameter
57+
# of :meth:`pygmt.Figure.legend`. For details on creating legends, see the tutorial
58+
# :doc:`multiple-column legend </tutorials/advanced/legends>`.
5259

5360
fig = pygmt.Figure()
5461
fig.basemap(region=region, projection="M15c", frame=True)
@@ -61,6 +68,10 @@
6168
fill="white",
6269
pen="black",
6370
)
71+
legend = io.StringIO(
72+
"\n".join(f"S 0.4 c {0.02 * 2**m:.2f} - 1p 1 Mw {m}" for m in [3, 4, 5])
73+
)
74+
fig.legend(spec=legend, position="jBR+o0.2c+l2", box=True)
6475
fig.show()
6576

6677
# %%
@@ -75,7 +86,6 @@
7586
# the earthquakes using :func:`pygmt.makecpt`, then set ``cmap=True`` in
7687
# :meth:`pygmt.Figure.plot` to use the colormap. At the end of the plot, we
7788
# also plot a colorbar showing the colormap used in the plot.
78-
#
7989

8090
fig = pygmt.Figure()
8191
fig.basemap(region=region, projection="M15c", frame=True)
@@ -91,6 +101,7 @@
91101
pen="black",
92102
)
93103
fig.colorbar(frame="xaf+lDepth (km)")
104+
fig.legend(spec=legend, position="jBR+o0.2c+l2", box=True)
94105
fig.show()
95106

96107
# sphinx_gallery_thumbnail_number = 3

0 commit comments

Comments
 (0)