From f37802fe1e497ccc1a8c4d7b42d58bdb454277f3 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 8 Nov 2024 19:35:40 +0800 Subject: [PATCH] Initial design of Figure.scatter --- pygmt/figure.py | 1 + pygmt/src/__init__.py | 1 + pygmt/src/scatter.py | 48 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 pygmt/src/scatter.py diff --git a/pygmt/figure.py b/pygmt/figure.py index 4163ab52eb1..93ec4c5385f 100644 --- a/pygmt/figure.py +++ b/pygmt/figure.py @@ -418,6 +418,7 @@ def _repr_html_(self): plot3d, psconvert, rose, + scatter, set_panel, shift_origin, solar, diff --git a/pygmt/src/__init__.py b/pygmt/src/__init__.py index e4db7321963..aad5f67cd83 100644 --- a/pygmt/src/__init__.py +++ b/pygmt/src/__init__.py @@ -42,6 +42,7 @@ from pygmt.src.project import project from pygmt.src.psconvert import psconvert from pygmt.src.rose import rose +from pygmt.src.scatter import scatter from pygmt.src.select import select from pygmt.src.shift_origin import shift_origin from pygmt.src.solar import solar diff --git a/pygmt/src/scatter.py b/pygmt/src/scatter.py new file mode 100644 index 00000000000..5ff2c7ddb10 --- /dev/null +++ b/pygmt/src/scatter.py @@ -0,0 +1,48 @@ +""" +scatter - Scatter plot. +""" + +from pygmt.helpers import is_nonstr_iter + + +def _parse_symbol_size(symbol, size): + """ + Parse the symbol and size into a style string. + + >>> _parse_symbol_size("c", 0.2) + 'c0.2' + >>> _parse_symbol_size("c", "0.2c") + 'c0.2c' + >>> _parse_symbol_size("c", [0.2, 0.3]) + 'c' + >>> _parse_symbol_size(["c", "t"], "0.2c"]) + '0.2c' + >>> _parse_symbol_size(["c", "t"], [0.2, 0.3]) + '' + """ + return "".join(f"{arg}" for arg in [symbol, size] if not is_nonstr_iter(arg)) + + +def scatter(self, x, y, symbol, size, **kwargs): + """ + Plot scatter points on a map. + + Parameters + ---------- + x, y : array-like + The coordinates of the points to plot. + symbol : str or sequence + The symbol to use for the points. + size : float or sequence + The size of the points. + """ + kwargs = self._preprocess(**kwargs) + + # style is a combination of symbol and size, but only if they are string-like + style = _parse_symbol_size(symbol, size) + if not is_nonstr_iter(symbol): + symbol = None + if not is_nonstr_iter(size): + size = None + + self.plot(x=x, y=y, style=style, symbol=symbol, size=size, **kwargs)