Skip to content

Commit 94b23a2

Browse files
authored
Add an example for scatter plots with auto legends (#607)
The example is modified from https://matplotlib.org/gallery/lines_bars_and_markers/scatter_with_legend.html
1 parent 7bedf48 commit 94b23a2

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

examples/gallery/plot/scatter.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
Scatter plots with a legend
3+
---------------------------
4+
5+
To create a scatter plot with a legend one may use a loop and create one scatter
6+
plot per item to appear in the legend and set the label accordingly.
7+
8+
Modified from the matplotlib example:
9+
https://matplotlib.org/gallery/lines_bars_and_markers/scatter_with_legend.html
10+
"""
11+
12+
import numpy as np
13+
import pygmt
14+
15+
np.random.seed(19680801)
16+
n = 200 # number of random data points
17+
18+
fig = pygmt.Figure()
19+
fig.basemap(
20+
region=[-0.1, 1.1, -0.1, 1.1],
21+
projection="X10c/10c",
22+
frame=["xa0.2fg", "ya0.2fg", "WSrt"],
23+
)
24+
for color in ["blue", "orange", "green"]:
25+
x, y = np.random.rand(2, n) # random X and Y data in [0,1]
26+
sizes = np.random.rand(n) * 0.5 # random size [0,0.5], in cm
27+
# plot data points as circles (style="c"), with different sizes
28+
fig.plot(
29+
x=x,
30+
y=y,
31+
style="c",
32+
sizes=sizes,
33+
color=color,
34+
# Set the legend label,
35+
# and set the circle size to be 0.25 cm (+S0.25c) in legend
36+
label=f"{color}+S0.25c",
37+
transparency=70, # set transparency level for all symbols
38+
)
39+
40+
fig.legend(transparency=30) # set transparency level for legends
41+
fig.show()

0 commit comments

Comments
 (0)